开发者

Loop not working

开发者 https://www.devze.com 2023-02-28 07:26 出处:网络
When I run the program it ends up averages it out but it wont loop! Does anyone see what I am doing wrong and point me in the right direction

When I run the program it ends up averages it out but it wont loop! Does anyone see what I am doing wrong and point me in the right direction

#include <iostream>
#include <iomanip> using namespace std;

//function prototypes 

void getTestScores(double &score1, double &score2, double &score3); 
double calcAverage(double &score1, double &score2, double &score3); 
void displayAverage(double avg);

int main() 
{   
    //declare variables
    double score1 = 0.0; 
    double score2 =0.0;     
    double score3 = 0.0; 
    double avg    = 0.0;

    //display average in fixed-point notation   
    getTestScores(score1,score2,score3);    
    calcAverage(avg);
    displayAverage(avg); 

    //enter scores
    while (avg != -1 ); 
    {
        cout << "score 1 (negative number to stop): ";
        cin >> score1;
        cout << "Enter score 2(negative number to stop): ";
        cin >> score2;
        cout << "Enter score 3(negative number to stop): ";
        cin >> s开发者_如何学运维core3;

        calcAverage(score1, score2, score3);
        displayAverage(avg); 

        return 0;
    }
} //end of main function

//*****function prototypes***** 
void getTestScores(double &score1, double &score2, double &score3) 
{
   cout << "Enter score 1(negative numberto stop): ";
   cin >> score1;
   cout << "Enter score 2(negative number to stop): ";
   cin >> score2;
   cout << "Enter score 3(negative number to stop): ";
   cin >> score3;
}  //end

double calcAverage (double &score1, double &score2, double &score3)
{
    double average = 0.0; 
    average = calcAverage(score1, score2, score3);

    return average;
} //end of clacAverage

void displayAverage(double avg)     
{
    cout << fixed << setprecision(1);
    cout << "Average: " << avg << endl;
} //end


There is a semicolon after your while() loop. Try removing it.


You have return in you while block. Return stops everything, including loops. Only Python (AFAIK) allows to put return in cycle and get it cycling.


@Katie: Does your code even compile? Your calcAverage(avg); function call is different from how you have defined it. Did you forget to add code for an overloaded function with the (same name) signature calcAverage(double )???

BTW, remove the semicolon after the while() and it might run fine. Try this after you check my question above.


Return will end the loop and return control back to where the function is called.. Also it doesn't seem like you re initialize avg again in the loop body? you return the avg but do not store it should be

avg = calcaAverage(1,2,3)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号