2017-10-10 60 views
-4
#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 if语句是在循环内,似乎只工作当它在循环之后放置在外部时为什么会出现这种情况?难以理解while循环执行

+0

在while循环,如果count = 0,这是不是这样的,因为你必须正确之前它++算,所以你goint开始与数= 1,则需要if语句只执行在if语句之后放置该行。 –

+0

不用担心,如果输入的数字足够多,并且编译器支持数字溢出 – grek40

回答

2

你永远不会回到零。

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