C ++告诉我如何在无限循环时停止此操作

时间:2015-02-13 14:45:45

标签: c++

当用户没有输入数字时,我得到一个无限循环。我已经尝试了几个小时而且无法让它停下来我是新手,所以如果你能帮我停止无限循环,我会感激不尽。无限循环似乎只发生在用户输入非数字的东西时。

// libraries to add that are required for this program with 
// identification of what library gives what objects
#include <iostream>             // provides:  cout, cin
#include <iomanip>              // provides:  setw setprecision(n)
#include <limits>


// setting up the environment
using namespace std;

int main()
{

    // usimg Window.h
    // Print out title of program
    cout << " - GPA Calculation Program - \n";

    string str; // Gets User inpit

    // Give 2 lines spacing
    cout << endl;
    cout << endl;
    // list of variables needed
    const double max = 100;   // constant variable for max grade = 100
    const double min = 0;     // constant variable for min grade = 0
    const double Hours = 3;  // constant variable for Credit Hours = 3

    int gradeNum = 0;         //  variable for Grade #
    double grade;             //  variable for Grade input by user 
    double GPA;               //  variable for value of GPA points varys by user inputs for grade

    bool True1 = true;        //  bool statement for True
    bool True2 = true;        //  bool statement for True
    bool True3 = true;        //  bool statement for True
    bool True4 = true;        //  bool statement for True

    double Total = 0.0;       //  variable for adding all of the        Total_GPA_multiply_HOURS together
    double  TotalGpa = 0.0;   //  variable for adding all of gpa together
    double gpa = 0.0;         //  variable for value of GPA being incremented 
    double  Total_GPA_multiply_HOURS = 0.0;   //  variable for value of results of  GPA * Hours

    // start a while loop  Asking user for input and give them response
    while (True1 == true)
    {
        gradeNum++;              // Grade number is increased by 1 for each user entry
        True4 = true;             // Turn switch 4 on

        while (True4 == true)
        { // start of 4th while loop

            bool True2 = true;        //  bool statement for True

            //Prints   to the console the question  the the user will answer with a number that represents their grade

            cout << "Enter Grade " << gradeNum << " (From 0 to 100): ";

            cin >> grade;

            while (True2 == true)
            {// start of 2nd while loop 

                if (grade >= 90 && grade <= max)
                { //  start 1st if

                    // prints Letter grade for user input if statements true
                    //  and does calculations to get the user GPA

                    cout << "Your letter grade is A \n";
                    GPA = 4;
                    gpa += 4;
                    Total_GPA_multiply_HOURS = GPA * Hours;
                    True2 = false;
                    True4 = false;
                    True3 = true;
                    True3 = true;
                }

                else if (grade >= 80 && grade <90)
                {
                    // prints Letter grade for user input if statements true
                    //  and does calculations to get the user GPA

                    cout << "Your letter grade is B \n";
                    GPA = 3;
                    gpa += 3;
                    Total_GPA_multiply_HOURS = GPA * Hours;
                    True2 = false;
                    True4 = false;
                    True3 = true;
                    True3 = true;
                }

                else if (grade >= 70 && grade <80)
                {
                    // prints Letter grade for user input if statements true
                    //  and does calculations to get the user GPA

                    cout << "Your letter grade is C \n\n";
                    GPA = 3;
                    gpa += 3;
                    Total_GPA_multiply_HOURS = GPA * Hours;
                    True2 = false;
                    True4 = false;
                    True3 = true;
                    True3 = true;
                }

                else if (grade >= 60 && grade <70)
                {
                    // prints Letter grade for user input if statements true
                    //  and does calculations to get the user GPA     

                    cout << "Your letter grade is D \n\n";
                    GPA = 2;
                    gpa += 2;
                    Total_GPA_multiply_HOURS = GPA * Hours;
                    True2 = false;
                    True4 = false;
                    True3 = true;
                    True3 = true;
                }

                else  if (grade >= min && grade <60)
                {
                    // prints Letter grade for user input if statements true
                    //  and does calculations to get the user GPA

                    cout << "Your letter grade is F \n\n";

                    GPA = 0;
                    gpa += 0;
                    Total_GPA_multiply_HOURS = GPA * Hours;
                    True2 = false;
                    True4 = false;
                    True3 = true;
                    True3 = true;
                }

                else if ((grade < min || grade > max) && (grade > -100000000 && grade < 100000000))
                {
                    // print error for invalid entry   

                    cout << "*** ERROR: " << grade << " is INVALID grade\n" << " Grade must be from 0 to 100! \n \n PLease enter a Grade and has to be from 0 to 100!\n\n\n";


                    True2 = false;
                    True4 = true;
                }
                else
                {
                    // print error for invalid entry   

                    cout << "*** ERROR: The input you have provided is not a number!\n Please enter a number from 1 to 100!\n\n\n";

                    cin.fail();
                    cin.clear();
                    cin.ignore(numeric_limits<streamsize>::max(), '\n');

                    True1 = false;
                    True2 = false;
                    True3 = false;
                    True4 = false;


                } //  end of 1st if


            } // end of 2nd while loop


        }// end  4th while loop
    }// end 1st while loop

    system("pause");
    return 0;
}

2 个答案:

答案 0 :(得分:0)

你说:

  

当用户没有输入数字时,我得到一个无限循环

创建一个打印提示的函数,尝试读取数字,如果读取不成功,则清除输入流并再次调用自身。

double readGrade(int gradeNum)
{
    double grade;
    cout << "Enter Grade " << gradeNum <<" (From 0 to 100): ";
    if ( !(cin >> grade) )
    {
       // Clear the stream.
       cin.clear();

       // Discard everything up to the newline.
       cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

       // Call the function again.
       return readGrade(gradeNum);
    }

    return grade;
}

然后,替换主

中的以下行
cout << "Enter Grade " << gradeNum <<" (From 0 to 100): ";

cin >> grade;

致电readGrade()

grade = readGrade(gradeNum);

答案 1 :(得分:0)

我已经在多个地方调整了你的GPA课程:

  1. 添加了2个变量以跟踪被视为输入的主题并设置最多10个主题。

    int maxSubjects = 10;
    int currentSubject = 0;
    
  2. 然后检查条件(currentSubject < maxSubjects)以确保 不超过科目数。

  3. 将计算从Total_GPA_multiply_HOURS = GPA * Hours;更改为Total_GPA_multiply_HOURS += GPA * Hours;,因为我们需要此值才能在输入所有值后获得人员的实际GPA。

  4. 在内部while循环之后添加了一个条件以确保外部while 输入10个科目时退出循环

    if (currentSubject == maxSubjects){
        break;
    }
    
  5. 添加了一行以打印出计算出的GPA,cout << "GPA: " << (Total_GPA_multiply_HOURS/gpa) << endl;

  6. 这是完整的程序:

    #include <iostream>
    #include <iomanip>
    #include <limits>
    using namespace std;
    
    int main()
    {
        cout << " - GPA Calculation Program -" << endl << endl << endl;
        string str;
        const double max = 100;
        const double min = 0;
        const double Hours = 3;
        int maxSubjects = 10;
        int currentSubject = 0;
    
        int gradeNum = 0;
        double grade;
        double GPA;
    
        bool True1 = true;
        bool True2 = true;
        bool True3 = true;
        bool True4 = true;
    
        double Total = 0.0;
        double  TotalGpa = 0.0;
        double gpa = 0.0;
        double  Total_GPA_multiply_HOURS = 0.0;
    
        while (True1 == true)
        {
            gradeNum++;
            True4 = true;
            currentSubject++;
            while (True4 == true && (currentSubject < maxSubjects))
            {
                True2 = true;
                cout << "Enter Grade " << gradeNum << " (From 0 to 100): ";
                cin >> grade;
                while (True2 == true)
                {
                    if (grade >= 90 && grade <= max)
                    {
                        cout << "Your letter grade is A \n";
                        GPA = 4;
                        gpa += 4;
                        Total_GPA_multiply_HOURS += GPA * Hours;
                        True2 = false;
                        True4 = false;
                        True3 = true;
                    }
                    else if (grade >= 80 && grade <90)
                    {
                        cout << "Your letter grade is B \n";
                        GPA = 3;
                        gpa += 3;
                        Total_GPA_multiply_HOURS += GPA * Hours;
                        True2 = false;
                        True4 = false;
                        True3 = true;
                    }
                    else if (grade >= 70 && grade <80)
                    {
                        cout << "Your letter grade is C \n\n";
                        GPA = 3;
                        gpa += 3;
                        Total_GPA_multiply_HOURS += GPA * Hours;
                        True2 = false;
                        True4 = false;
                        True3 = true;
                    }
                    else if (grade >= 60 && grade <70)
                    {
                        cout << "Your letter grade is D \n\n";
                        GPA = 2;
                        gpa += 2;
                        Total_GPA_multiply_HOURS += GPA * Hours;
                        True2 = false;
                        True4 = false;
                        True3 = true;
                    }
                    else  if (grade >= min && grade <60)
                    {
                        cout << "Your letter grade is F \n\n";
                        GPA = 0;
                        gpa += 0;
                        Total_GPA_multiply_HOURS += GPA * Hours;
                        True2 = false;
                        True4 = false;
                        True3 = true;
                    }
                    else if ((grade < min || grade > max) && (grade > -100000000 && grade < 100000000))
                    {
                        cout << "*** ERROR: " << grade << " is INVALID grade\n" << " Grade must be from 0 to 100! \n \n PLease enter a Grade and has to be from 0 to 100!\n\n\n";
                        True2 = false;
                        True4 = true;
                    }
                    else
                    {
                        cout << "*** ERROR: The input you have provided is not a number!\n Please enter a number from 1 to 100!\n\n\n";
                        cin.fail();
                        cin.clear();
                        cin.ignore(numeric_limits<streamsize>::max(), '\n');
                        True1 = false;
                        True2 = false;
                        True3 = false;
                        True4 = false;
                    }
                }
            }
            if (currentSubject == maxSubjects){
                break;
            }
        }
        cout << "GPA: " << (Total_GPA_multiply_HOURS/gpa) << endl;
        return 0;
    }
    

    希望这有帮助。