C ++程序在调用函数时提前退出

时间:2016-10-04 00:31:21

标签: c++

为什么我的课程提前退出?它还没有完成,还有两个功能,一个用于平均数字,一个用于显示平均值。

当我通过visual studio运行程序时,它会在显示第一个“Score1:”后退出程序。为什么不进入getScore()函数?

当我将程序放在在线编译器中时,它就起作用了,而不是在我从visual studio编译和运行时。

 // Lab 8-1.cpp : Defines the entry point for the console application.
 //


 #include <iostream>
 using namespace std;

void printInfo();       // printInfo prototype
int getScore();         // getScore prototype

int main()
{
    int score1 = 0, score2 = 0, score3 = 0;     // declare variables

     printInfo();       // call printInfo function

    cout << endl << "Score 1: ";        // output: ask for first score
    score1 = getScore();                // assign value to integer through getScore function
    cout << endl << "Score 2: ";
    score2 = getScore();
    cout << endl << "Score 3: ";
    score3 = getScore();

    return 0;
}

// ******************************************************
// * The printInfo function displays a message about    *
// * what the program does                              *
// ******************************************************
void printInfo()
{
    cout << "This program accepts 3 integer scores between\n";
    cout << "0 and 50 (inclusive), and displays the average\n";
    cout << "of the three tests.\n\n";
}

// **********************************************************
// * The getScore function takes in a value from the user,  *
// * validates the input, and returns an int value          *
// **********************************************************
int getScore()
{
    int score;
    cout << "Enter a number: ";
    cin >> score;
    while (score < 0 || score > 50)
    {
        cout << "\nScore must be >= 0 or <= 50." << endl;
        cout << "Please enter a new score:";
        cin >> score;
    }
    return score;
}

0 个答案:

没有答案