简体   繁体   English

为什么我的while语句没有结束?

[英]Why doesn't my while statement end?

I've created this program to calculate scores of peoples tests and count the number of grades. 我创建了这个程序来计算人员测试的分数并计算分数。 What i want to happen is that when the user enters "//" when the program asks "Enter student Name", the while statement should end, or that's what i assume should happen. 我想发生的是,当程序要求“输入学生姓名”时,当用户输入“ //”时,while语句应结束,否则我认为应该发生这种情况。 What actually happens is that i type "//" then the program asks for the score, then the while statement ends. 实际发生的情况是,我键入“ //”,然后程序要求获得分数,然后while语句结束。 Why is this and what should i do to fix it? 为什么会这样,我应该怎么解决?

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name, grade;
    int score;
    int count1 = 0;
    int acount = 0;
    int bcount = 0;
    int ccount = 0;
    int dcount = 0;
    int fcount = 0;
    while (name != "//") {
        cout << "Enter Student Name \n";
        cin >> name;
        cout << "Enter Student Score \n";
        cin >> score;
        count1 += 1;
        if (score >= 90) {
            grade = "A";
            acount += 1;
            cout << name << " " << score << " " << grade << endl;
        }
        else if (score >= 80) {
            grade = "B";
            bcount += 1;
            cout << name << " " << score << " " << grade << endl;
        }
        else if (score >= 70) {
            grade = "C";
            ccount += 1;
            cout << name << " " << score << " " << grade << endl;
        }
        else if (score >= 60) {
            grade = "D";
            dcount += 1;
            cout << name << " " << score << " " << grade << endl;
        }
        else if (score <= 59) {
            grade = "F";
            fcount += 1;
            cout << name << " " << score << " " << grade << endl;
        }
    };
    cout << "Summary Report \n" << "Total Students Count: " << count1 << endl;
    cout << "A student count: " << acount << endl;
    cout << "B student count: " << bcount << endl;
    cout << "C student count: " << ccount << endl;
    cout << "D student count: " << dcount << endl;
    cout << "F student count: " << fcount << endl;
    return 0;
}

Most of the things have been already said in the comments: 评论中已经提到了大多数事情:

Your problem is, that you expect the loop to check its condition continuously, which it does not. 您的问题是,您希望循环不断地检查其状况,而实际上不会。

while(name != "//") // here the condition is evaluated
{
    name = "//";    // here it is not 
     // the following code is executed, independent of the current name value
} // here the program jumps back to the loop condition, which is now false and it will not enter another time.

Like mentioned in the comments, a working alternative could be: 就像评论中提到的那样,可行的替代方法可能是:

while(true)
{
    std::cin >> name;
    if(name == "//")
        break;
    else
        evaluateScore();
}

Try this: 尝试这个:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name, grade;
    int score;
    int count1 = 0;
    int acount = 0;
    int bcount = 0;
    int ccount = 0;
    int dcount = 0;
    int fcount = 0;
    while (true) {  // keep running the loop
        cout << "Enter Student Name \n";
        cin >> name;
        if(name == "//")  // if "//" found, break the loop
            break;
        cout << "Enter Student Score \n";
        cin >> score;
        count1 += 1;
        if (score >= 90) {
            grade = "A";
            acount += 1;
            cout << name << " " << score << " " << grade << endl;
        }
        else if (score >= 80) {
            grade = "B";
            bcount += 1;
            cout << name << " " << score << " " << grade << endl;
        }
        else if (score >= 70) {
            grade = "C";
            ccount += 1;
            cout << name << " " << score << " " << grade << endl;
        }
        else if (score >= 60) {
            grade = "D";
            dcount += 1;
            cout << name << " " << score << " " << grade << endl;
        }
        else if (score <= 59) {
            grade = "F";
            fcount += 1;
            cout << name << " " << score << " " << grade << endl;
        }
    };
    cout << "Summary Report \n" << "Total Students Count: " << count1 << endl;
    cout << "A student count: " << acount << endl;
    cout << "B student count: " << bcount << endl;
    cout << "C student count: " << ccount << endl;
    cout << "D student count: " << dcount << endl;
    cout << "F student count: " << fcount << endl;
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM