hi peeps!

This is a discussion on hi peeps! within the C++ Programming forums, part of the General Programming Boards category; I'm wondering that in my program... Heres the code: Code: #include <iostream> using namespace std; int main() { int DOB; ...

  1. #1
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321

    hi peeps!

    I'm wondering that in my program...
    Heres the code:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int DOB;
    	cout << "Please enter your date of birth...\n" << endl;
    	cin >> DOB;
    	cin.ignore();
    	if (DOB < 2006) 
        {
    		cout << "Erh, you were not born in this year!\n" << endl;
    	}
    	else if (DOB == 2006) 
        {
    		cout << "Oioi, howz your first year?\n" << endl;
    	}
    	else 
        {
    		cout << "WTF are you on aboot? You arn't even born yet!\n" << endl;
    	}
    	cin.get();
    }
    How do i go back to the start after i've inputed the year and it outputs the phrase?
    All help will be appreciated

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    13
    I'm no pro, but couldn't you just add a for or while statement at the beginning?

  3. #3
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    hmmmmm...
    I'm not sure that will work.
    Because while and for and do...while only executes the program if it meets the conditions.
    Thanks for the help though.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    You can set the conditions to be whatever you want. If you always want it to go back to the beginning, you can make the condition true. So the while (true) will continue to go back to the start over and over again. You can also later add code to end the program if something happens, for example if the user enters -1 for DOB, then you can exit the loop.

    The while loop is what you want.

  5. #5
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    Could you explain how to do this?

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    13
    Again, I'm no pro, but if my past experience with c serves me well, this is all you should need, no?
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	while (DOB!=-1)
    	{
    		int DOB;
    		cout << "Please enter your date of birth...\n" << endl;
    		cin >> DOB;
    		cin.ignore();
    		if (DOB < 2006) 
    		{
    			cout << "Erh, you were not born in this year!\n" << endl;
    		}
    		else if (DOB == 2006) 
    		{
    			cout << "Oioi, howz your first year?\n" << endl;
    		}
    		else 
    		{
    			cout << "WTF are you on aboot? You arn't even born yet!\n" << endl;
    		}
    		cin.get();
    	}
    }

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    A tutorial would have been nice too:
    http://www.google.com/search?q=looping+in+c%2b%2b

  8. #8
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    it works, thank you very much!

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> while (DOB!=-1)
    With that code you'd have to have DOB declared before the loop, and you'd want to initialize it to some value other than -1. If you leave it unitialized, then it might accidentally be -1 when you run the program and nothing will happen.

  10. #10
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    I know, I Sort of corrected the declaring problem

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    I would use a do..while loop for that sort of thing - another poster already pointed out the problem with DOB being undeclared; a do..while loop also guarantees that the block of code runs at least once.

  12. #12
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    hi bench82, i used the other method and encounted a few probs, but i took your advice and it works just fine, thanks!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer and Arrays
    By big146 in forum C++ Programming
    Replies: 5
    Last Post: 06-14-2004, 06:23 PM
  2. hey peeps, the lord of the nightmares is back
    By Null Shinji in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 11-12-2001, 11:17 PM
  3. Any peeps in here know how to.....
    By bluehead in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-06-2001, 04:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21