Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9

Thread: C++ yes or no

  1. #1
    New Coder
    Join Date
    Jul 2007
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts

    C++ yes or no

    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:
    Code:
    #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;
    }

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    New Coder
    Join Date
    Jul 2007
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok. Here's what I got now.

    Code:
    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.

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #5
    New Coder
    Join Date
    Jul 2007
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Code:
    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;
    }
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #8
    New Coder
    Join Date
    Aug 2007
    Posts
    14
    Thanks
    3
    Thanked 1 Time in 1 Post
    Would a 'do..while' loop not serve the purpose better? (If such a thing exists in C++ :-))

    thanks,
    Scrumpy

  • #9
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,060
    Thanks
    0
    Thanked 13 Times in 13 Posts
    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-
    HTML & CSS Forum Moderator

    "If you don't know what you think you know, then what do you know."
    R.I.P. Derrick Thomas #58
    1/1/1967 - 2/8/2000


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •