I want my entire program to run again but my loop is won't work the way intend it to

Hi! :D
program runs but one problem is the loop, what i want is for the entire program to run again but apparently only the statement "Do you want program to run again? Yes [Y] or No [N]: " runs again. I tried to use goto but it did not work. Any way I could jump from my do while statement to the beginning of the program so I can run it over and over?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;


void getinp(double* pc, double* p1, double* p2);
void inflationRate(double pc, double p1, double p2, double* rate1, double* rate2);
void showresult(double rate1, double rate2);


#include <iostream>
using namespace std;


void getinp(double* pc, double* p1, double* p2);
void inflationRate(double pc, double p1, double p2, double* rate1, double* rate2);
void showresult(double rate1, double rate2);


int main ()
{
    double pc = 0, p1 = 0, p2 = 0, ra1 = 0, ra2 = 0;
    getinp(&pc, &p1, &p2);
    inflationRate(pc, p1, p2, &ra1, &ra2);
    showresult(ra1, ra2);
    cin.clear();
    cin.ignore(255, '\n');
    cin.get();

    int loop = 1;
    char answer;

    do{
        cout<<"Do you want program to run again? Yes [Y] or No [N]: "<<endl;
        cin>>answer;

        switch (answer)
        {
        case 'Y':
        case 'y':
        loop = 1;
        break;
        case 'N':
        case 'n':
        loop = 0;
        break;
        default:
        cout<<"That is an invalid option, try again with either y or n: "<<endl;
        break;
        }
    }while (loop);
    return 0;

}
void getinp( double* pc, double* p1, double* p2)
{
cout << " please enter the current price: ";
cin >> *pc;
cout <<endl;
cout << " please enter the the price a year ago: ";
cin >> *p1;
cout <<endl;
cout << " please enter the price two years ago: ";
cin >> *p2;
cout <<endl;
}
void inflationRate(double pc, double p1, double p2, double* rate1, double* rate2)
{
*rate1 = (( pc-p1)/p1)*100;
*rate2 = (( p1-p2)/p2)*100;
}
void showresult(double rate1, double rate2)
{
if (rate1 > rate2)
cout << "The inflation rate is increasing" << endl;
else if (rate1 < rate2)
cout << "The inflation rate is decreasing" << endl;
else
cout << "There is no change in the inflation rate" << endl;

cout << "The rate for this year:" << rate1 <<"%"<< endl;
cout << "The rate for last year:" << rate2 <<"%"<< endl;
}

Last edited on
Hi! :D
Please use code tags and edit your post :
www.cplusplus.com/articles/z13hAqkS/


You could create a while loop that loops while a value is true. Typing no breaks the loop and ends the program. Also you forgot to add [/code]
Last edited on
okay, so in which part of my program should I place the while loop? so that the user can loop the program until he/she wishes to exit?

oops okay i'll edit my post thanks! XD
Actually, you don't even need a check variable example:
1
2
3
4
5
6
7
8
9
while(1)
{
     //HANDLE INPUT
    if(NO)
    {
          break;
    }
    //INSERT WHATEVER OTHER FUNCTIONS YOU WANT TO DO HERE
}

so i made a simple while loop but now it gives me an error of 'Y' was no declared in this scope.

int main ()
{
char answer = 'Y';
while(answer == Y) //error: 'Y' was not declared in this scope'
{
double pc = 0, p1 = 0, p2 = 0, ra1 = 0, ra2 = 0;
getinp(&pc, &p1, &p2);
inflationRate(pc, p1, p2, &ra1, &ra2);
showresult(ra1, ra2);
cin.clear();
cin.ignore(255, '\n');
cin.get();

cout<<"Do you want program to run again? Yes [Y] or No [N]: "<<endl;
cin>>answer;
}
cout<<"End of Program."<<endl;
return 0;
}

Y is not 'Y' ! Y is as variable 'Y' is the caracter.
Add single quotes.
Topic archived. No new replies allowed.