为什么std :: cout在以下代码中无法正常工作?

时间:2017-04-15 10:27:03

标签: c++ xcode macos c++11 stl

我只是在c ++中练习一些编程练习,我编写了以下代码。问题是," std :: cout"在标记的部分中没有正常工作(我用" *"标记了问题的行),一切正常,但">"符号只出现在" std :: cin"之后,而不是在我写之前(std :: cout<<" \ n引入学生数量\ n>&#34 ;;)。 我正在使用xcode 8.3.1,我不知道我的xcode是否发生错误。谢谢你们,我希望每个人都能理解这个问题,我真的很想回答一下。

PD:对不起我的英语,我不是英语母语人士,我也要尽力提及我写的#include iostream和iomanip,这不是问题,但我不是知道怎么把它放在评论中,因为我是新来的。

这是控制台屏幕上的输出:

介绍学生人数

2

>

介绍学生成绩

3.3

>

介绍学生成绩

5

>

Nota media de los alumnos:4.15

Porcentaje de aprobados:0%

Porcentaje de suspensos:0%

====================

int main()
{

    const unsigned short sizeOfarray (100);
    int  numberOfstudents (0);
    const unsigned short minimunNumberOfstudents (1);

    //double notasAlumnos[sizeof(sizeOfarray)];

    double studentsGrade[sizeOfarray];

    do{

        std::cout << "\nIntroduce number of students\n>"; //* here the sign ">" is printed after std::cin >> numberOfstudetns.


      std::cin >>  numberOfstudents;  

// here ,after i introduce the number of students the cout prints the symbol">"



    }

    while( numberOfstudents < minimunNumberOfstudents ||  numberOfstudents > sizeOfarray);

    for(unsigned short i = 0 ; i < numberOfstudents;i++){


        std::cout <<  "\nIntroduce students grade\n>>";
        std::cin >> studentsGrade[i];


        }

    float minimumGradeTopass = {5};

    unsigned short fails ={0};
    unsigned short passed = {0};



    for(unsigned short i = 0 ; i < numberOfstudents;i++) {


        std::cout << "students grade ["<< i+1 << "]: "<< studentsGrade[i] <<std::endl;




        if( studentsGrade[i] < minimumGradeTopass){


           fails++;

        }else if (studentsGrade[i] >= minimumGradeTopass){

            passed++;



        }


    }
    float gradeAverage(0);
    const unsigned short percent(100);

    for(unsigned short i = 0 ; i < numberOfstudents;i++) {


        float grade = studentsGrade[i];

        gradeAverage  += grade;



    }
   std::cout << "\nNota media de los alumnos: " << gradeAverage/numberOfstudents << std::endl;
std::cout << "\nPorcentaje de aprobados: " << (passed/numberOfstudents)*percent <<"%" << std::endl;
std::cout << "\nPorcentaje de suspensos: " <<(fails/numberOfstudents)*percent<<"%" << std::endl;


    return 0;
}

1 个答案:

答案 0 :(得分:1)

您应该使用std :: flush强制ostream将其缓冲区写入屏幕,如:

std::cout << "\nIntroduce number of students\n>" << std::flush;