c++ 两个std::cout,仅一个输出

vptzau2j  于 2023-01-22  发布在  其他
关注(0)|答案(3)|浏览(160)

我有一个程序,它接受10个人的输入,然后计算出吃煎饼最多的人和吃煎饼最少的人。我试过:

  • std::endl;在每个cout的末尾。
  • std::冲洗;结束时。
  • 在我的机器上不起作用之后,我把代码放到www.example.com上,它仍然不起作用。这(根据我对repl.it工作原理的理解)排除了compiler/ide(它是g 和Visual Studio 2019)的问题。repl.it, and it still didn't work. This (from my understanding of how repl.it works) rules out an issue with compiler/ide (which is g and visual studio 2019).

我的代码:
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;
    }

funcs.cpp

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

void get_nums_of_pancakes() {
//Get the no. of pancakes eaten by person 1, 2, etc. up to person 10.
for (int i = 0; i < 10; i++) {
    person[i] = i + 1;
    std::cout << "Input the number of pancakes entered by person " << person[i] << ": ";
    std::cin >> num_eaten_pancakes[i];
  }
}

//Who ate the most pancakes
int who_ate_most() {
for (int i = 0; i < 10; i++) {
    if (num_eaten_pancakes[i] > most_pancakes) {
        most_pancakes = num_eaten_pancakes[i];
        person_who_ate_most = person[i];
    }
  }
  return person_who_ate_most;
}

//Who ate the least pancakes
int who_ate_least() {
  for (int i = 0; i < 10; i++) {
     do
        least_pancakes = num_eaten_pancakes[i];
     while (i == 0);

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

header.h

#include <vector>
#include <string>

//VARIABLES
//Vectors for 10 people, pancakes
extern std::vector<int> person;
extern std::vector<int> num_eaten_pancakes;

extern int most_pancakes;
extern int least_pancakes;
extern int person_who_ate_most;
extern int person_who_ate_least;

//FUNCTIONS
void get_nums_of_pancakes();
int who_ate_most();
int who_ate_least();

当我输入所吃煎饼的数量时,对于吃得最多的人来说,输出是正确的,但对于吃得最少的人来说,输出是什么都没有。

3hvapo4f

3hvapo4f1#

who_ate_least()中有一个无限的do-while循环:如果i == 0,则永远不要更改i,也不要使条件变为假。

8gsdolmq

8gsdolmq2#

这部分是无限循环,所以函数who_ate_least卡住了。

do
        least_pancakes = num_eaten_pancakes[i];
     while (i == 0);
vyswwuz2

vyswwuz23#

我认为您应该将“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;
 }

相关问题