循环执行时难以理解

时间:2017-10-10 06:40:08

标签: c++

#include <iostream>
#include <iomanip>
#include <ios>
#include <algorithm>
#include <iomanip>
#include <string>
#include <vector>


using std::cin;             // <iostream>
using std::cout;            // <iostream>
using std::endl;            // <iostream>
using std::setprecision;    // <iomanip>
using std::sort;            // <algorithm>
using std::streamsize;      // <ios>
using std::string;          // <string>
using std::vector;          // <string>


int main()
{
    cout << "Enter your homework grades : " << endl;
    double x;

    vector<double> homework;

    int count = 0;

    while(cin >> x)
    {
        homework.push_back(x);
        ++count;
        if(count == 0)
        {
            cout << "Error, enter a grade" << endl;
            continue;
        }
    }
    return 0;
}

嗨,我想知道为什么我的while循环不会在屏幕上打印消息(错误,输入等级)如果if语句在循环内,并且似乎只有在循环执行后放在外面时才能工作为什么会这样呢?

1 个答案:

答案 0 :(得分:2)

你永远不会回到零。

int count = 0;
//...  
while(cin >> x)
{
    homework.push_back(x);
    ++count;
    if(count == 0)  //count will never be zero