...

View Full Version : C++ yes or no



MindTheGap
09-18-2007, 11:08 PM
Ok. What I want to do is make it so that the user has the option to run the program again by pressing 'y' for yes and 'n'. My teacher said it could be done with a while loop but I have no idea what to do.

Here's what I got so far:


#include <iostream.h>

int main()
{
int y, a, b, c, d, e, f, g, h, i, k, r, m, n, p;

cout << "Enter the year: ";
cin >> y;

while ( y < 1583)
{
cout << "The year must be subsequent to 1583." << endl;
cout << "Enter the year: ";
cin >> y;
}

a = y % 19;
b = y / 100;
c = y % 100;
d = b / 4;
e = b % 4;
f = (b + 8) / 25;
g = (b - f + 1)/3;
h = (19 * a + b - d - g + 15) % 30;
i = c / 4;
k = c % 4;
r = (32 + 2 * e + 2 * i - h - k) % 7;
m = (a + 11 * h + 22 * r) / 451;
n = (h + r - 7 * m + 114) / 31;
p = (h + r - 7 * m + 114) % 31;

cout << "a= " << a << endl;
cout << "b= " << b << endl;
cout << "c= " << c << endl;
cout << "d= " << d << endl;
cout << "e= " << e << endl;
cout << "f= " << f << endl;
cout << "g= " << g << endl;
cout << "h= " << h << endl;
cout << "i= " << i << endl;
cout << "k= " << k << endl;
cout << "r= " << r << endl;
cout << "m= " << m << endl;
cout << "n= " << n << endl;
cout << "p= " << p << endl;
cout << "Easter in " << y << " falls on " << n << "/" << p + 1 << "\n" << endl;
cout << "Would you like calculate another year? (Y or N): ";

return 0;
}

Spookster
09-18-2007, 11:20 PM
Well you would need to read in the users input again just like you did for the year and determine what to do based on what they entered. And yes you could do this with a while loop. Think about it for a second. If the user enters no what should happen? So while(yes) do something.

MindTheGap
09-18-2007, 11:28 PM
Ok. Here's what I got now.



char mychar
....other code here...

cout << Run program again? << endl;
cin >> mychar;
while ( mychar == y )
{
}
return 0;


I just dont know what to put inside the while brackets.

Spookster
09-18-2007, 11:37 PM
What you will need to do is to enclose the logic you already have inside that new while loop that is checking for a yes answer. Obviously you will need to add a bit more logic to your checking of course but since this is a homework assignment that part is up to you to figure out. That is typically the point of introductory assignments like this to figure out some basic logic and implement it with code.

MindTheGap
09-18-2007, 11:40 PM
I'm really not sure what to put in the while brackets at all. Is there any command that can take you up to the top of the program or something along those lines?

Antoniohawk
09-19-2007, 06:23 AM
Read Spookster's second post carefully, he has pretty much laid it out for you. Put simply by the aforementioned,
enclose the logic you already have inside that new while loop that is checking for a yes answer

felgall
09-19-2007, 07:40 AM
int y, a, b, c, d, e, f, g, h, i, k, r, m, n, p;
int loop = 'Y';
while (loop == 'Y') {
cout << "Enter the year: ";
...

...
cout << "Would you like calculate another year? (Y or N): ";
cin >> loop;
}

return 0;
}

Scrumpy.Gums
09-22-2007, 12:14 AM
Would a 'do..while' loop not serve the purpose better? (If such a thing exists in C++ :-))

thanks,
Scrumpy

sage45
09-29-2007, 11:48 PM
do while is a better option as it will run at least once...

while is a good option if you are controlling the conditional going into the loop...

In your case either will do but it is good to know the difference...

-saige-



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum