C++;两个标准::cout,只有一个输出 我刚开始C++编程。完成课程后,我正在尝试一些项目。我有一个程序,它接受10个人的输入,然后应该计算出吃煎饼最多的人和吃煎饼最少的人。我试过: std::endl;在每一个角落的尽头 标准:冲洗;最后 这在我的机器上不起作用后,我将代码放入repl.it,但它仍然不起作用。这(根据我对repl.it工作原理的理解)排除了编译器/ide(即g++和visual studio 2019)的问题

C++;两个标准::cout,只有一个输出 我刚开始C++编程。完成课程后,我正在尝试一些项目。我有一个程序,它接受10个人的输入,然后应该计算出吃煎饼最多的人和吃煎饼最少的人。我试过: std::endl;在每一个角落的尽头 标准:冲洗;最后 这在我的机器上不起作用后,我将代码放入repl.it,但它仍然不起作用。这(根据我对repl.it工作原理的理解)排除了编译器/ide(即g++和visual studio 2019)的问题,c++,cout,C++,Cout,我的代码: main.cpp: #include <iostream> #include <string> #include <vector> #include "header.h" std::vector<int> person = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; std::vector<int> num_eaten_pancakes(10);

我的代码:

main.cpp:

    #include <iostream>
    #include <string>
    #include <vector>
    #include "header.h"

    std::vector<int> person = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    std::vector<int> num_eaten_pancakes(10);

    int most_pancakes = 0;
    int least_pancakes = 0;
    int person_who_ate_most = 0;
    int person_who_ate_least = 0;

     int main() {
       get_nums_of_pancakes();
       person_who_ate_most = who_ate_most();
       std::cout << "Person " << who_ate_most() << " ate the most pancakes with " << num_eaten_pancakes[person_who_ate_most - 1] << " eaten." << std::flush;

       person_who_ate_least = who_ate_least();
       std::cout << "Person " << who_ate_least() << " ate the least pancakes with " << num_eaten_pancakes[person_who_ate_least - 1] << " eaten." << std::flush;
       return 0;
    }
#包括
#包括
#包括
#包括“header.h”
病媒人={1,2,3,4,5,6,7,8,9,10};
标准:吃的煎饼的病媒数量(10);
int most_煎饼=0;
int-least_煎饼=0;
吃得最多的整数=0;
至少吃过东西的整数=0;
int main(){
吃几块煎饼();
吃得最多的人=吃得最多的人;

std::cout你在
谁吃得最少()
中有一个无限的do-while循环:如果
i==0
你永远不会改变
i
,也永远不会使条件变为假。

你在这一部分有一个无限循环,所以函数
谁吃得最少
阻塞了

     do
        least_pancakes = num_eaten_pancakes[i];
     while (i == 0);
我认为您应该将“who_ate_least()”函数更改如下:

int who_ate_least() {
  for (int i = 0; i < 10; i++) {
     if(i==0)
        least_pancakes = num_eaten_pancakes[i];

     if (num_eaten_pancakes[i] <= least_pancakes) {
         least_pancakes = num_eaten_pancakes[i];
         person_who_ate_least = person[i];
     }
   }
   return person_who_ate_least;
 } 
int谁吃的最少(){
对于(int i=0;i<10;i++){
如果(i==0)
最少的煎饼=吃了多少个煎饼[i];

如果(num_eat_pancakes[i],您是否尝试过使用调试器逐条检查代码语句以查看实际发生的情况?如果没有,那么现在是进行此操作的最佳时机,并学习如何使用编译器本身之外最重要的工具之一。
do least_pancakes=num_eat_pancakes[i];而(i==0)
这不会很快完成。“这不会打印任何内容”!=“我的程序永远不会完成”@Someprogrammerdude我真的不知道如何使用调试器。目前我只是按下按钮检查它是否工作。不过,我真的很想学习如何正确使用它。你有任何资源来学习如何使用它吗?谢谢。
     do
        least_pancakes = num_eaten_pancakes[i];
     while (i == 0);
int who_ate_least() {
  for (int i = 0; i < 10; i++) {
     if(i==0)
        least_pancakes = num_eaten_pancakes[i];

     if (num_eaten_pancakes[i] <= least_pancakes) {
         least_pancakes = num_eaten_pancakes[i];
         person_who_ate_least = person[i];
     }
   }
   return person_who_ate_least;
 }