954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need Help In C++

I have some menu problems. So this is my first time, and I need someone to please solve the problem for me.
This is my program. When I ask it to enter on the home menu, it takes me back to the second menu and asks me again if I would like to exit. Could someone help me. Get rid of this problem. Please someone help me.

#include <iostream>
using namespace std;
int main()
{

    int choice;
    bool gameOn = true;
    while (gameOn != false) {
        cout << "\n\t\tWelcome To Zoheb.I.Currimbhoy's Age Calculator\n";
        cout << " \n\n1 - Start the Questionaire.\n";
        cout << " \n2 - Exit.\n";
        cout << " \nEnter your choice and press return: ";
        cin >> choice;

        switch (choice) {
        case 1:
            cout << "\n\n\t\t\t\tStart The Questionaire\n";
            int birthmonth,birthyear;
            int currentmonth,currentyear;
            int agey,agem;
            cout<<"\n\n\t\tUsing C++, Find Your Age By Answering The Questions\n\n";
            cout<<"\n\nEnter Your Birth Year(Eg:1989):";
            cin>>birthyear;
            cout<<"\n\nEnter Your Birth Month(Eg:7):";
            cin>>birthmonth;
            cout<<"\n\nEnter The Current Month(Eg:7):";
            cin>>currentmonth;
            cout<<"\n\nEnter The Current Year(Eg:2010):";
            cin>>currentyear;
            agey=currentyear-birthyear;
            agem=currentmonth-birthmonth;
            cout<<"\n\n\t\t\tYour Age is "<<agey<<" Years And "<<agem<<" Months ";
            printf("\n\n\n\t Thanks for using the age calculator. Have a great day.\n");
            break;
        case 2:
            cout << "End of Program.\n";
            gameOn = false;
            break;
        default:
            cout << "Not a Valid Choice. \n";
            cout << "Choose again.\n";
            cin >> choice;
            while (gameOn!=false);

            bool gameOn = true;
            while (gameOn != false) {
                cout << " \n\n1- Re-Start the Questionaire.\n";
                cout << " \n\n2- Exit.\n";
                cout << " \nEnter your choice and press return: ";
                cin >> choice;
                switch (choice) {
                case 1:
                    cout << "\n\n\t\t\t\tStart The Questionaire\n";
                    int birthmonth,birthyear;
                    int currentmonth,currentyear;
                    int agey,agem;
                    cout<<"\n\n\t\tUsing C++, Find Your Age By Answering The Questions\n\n";
                    cout<<"\n\nEnter Your Birth Year(Eg:1989):";
                    cin>>birthyear;
                    cout<<"\n\nEnter Your Birth Month(Eg:7):";
                    cin>>birthmonth;
                    cout<<"\n\nEnter The Current Month(Eg:7):";
                    cin>>currentmonth;
                    cout<<"\n\nEnter The Current Year(Eg:2010):";
                    cin>>currentyear;
                    agey=currentyear-birthyear;
                    agem=currentmonth-birthmonth;
                    cout<<"\n\n\t\t\tYour Age is "<<agey<<" Years And "<<agem<<" Months ";
                    printf("\n\n\n\t Thanks for using the age calculator. Have a great day.\n");
                    break;
                case 2:
                    cout << "End of Program.\n";
                    gameOn = false;
                    break;
                default:
                    cout << "Not a Valid Choice. \n";
                    cout << "Choose again.\n";
                    cin >> choice;
                    break;
                    while(gameOn!=false);
                    return 0;
                }
                while (gameOn!=false);
            }
        }
        return (0);
    }
}


This is my program. When I ask it to enter on the home menu, it takes me back to the second menu and asks me again if I would like to exit. Could someone help me. Get rid of this problem.

Zoe123
Newbie Poster
2 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

Why do you have a nested while() and then another nested switch()? Why not just loop again after detecting the first invalid input? I suspect this is why you're receiving the same prompt, twice. You're only returning from the nested while(), not the overall while().

Consider changing the default case in your first switch() to re-prompt the user ... I'm betting this will get you what you're looking for.

>>Could someone help me. Get rid of this problem.
This isn't what we do.

>>Could someone help me get rid of this problem?
This is what we do.

Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
 

Hey it still doesnt work.

#include <iostream>
using namespace std;
int main()
{
 
    int choice;
    bool gameOn = true;
     (gameOn != false) {
        cout << "\n\t\tWelcome To Zoheb.I.Currimbhoy's Age Calculator\n";
        cout << " \n\n1 - Start the Questionaire.\n";
        cout << " \n2 - Exit.\n";
        cout << " \nEnter your choice and press return: ";
        cin >> choice;
 
        switch (choice) {
        case 1:
            cout << "\n\n\t\t\t\tStart The Questionaire\n";
            int birthmonth,birthyear;
            int currentmonth,currentyear;
            int agey,agem;
            cout<<"\n\n\t\tUsing C++, Find Your Age By Answering The Questions\n\n";
            cout<<"\n\nEnter Your Birth Year(Eg:1989):";
            cin>>birthyear;
            cout<<"\n\nEnter Your Birth Month(Eg:7):";
            cin>>birthmonth;
            cout<<"\n\nEnter The Current Month(Eg:7):";
            cin>>currentmonth;
            cout<<"\n\nEnter The Current Year(Eg:2010):";
            cin>>currentyear;
            agey=currentyear-birthyear;
            agem=currentmonth-birthmonth;
            cout<<"\n\n\t\t\tYour Age is "<<agey<<" Years And "<<agem<<" Months ";
            printf("\n\n\n\t Thanks for using the age calculator. Have a great day.\n");
            break;
        case 2:
            cout << "End of Program.\n";
            gameOn = false;
            break;
        default:
            cout << "Not a Valid Choice. \n";
            cout << "Choose again.\n";
            cin >> choice;
             (gameOn!=false);
 
            bool gameOn = true;
             (gameOn != false) {
                cout << " \n\n1- Re-Start the Questionaire.\n";
                cout << " \n\n2- Exit.\n";
                cout << " \nEnter your choice and press return: ";
                cin >> choice;
                switch (choice) {
                case 1:
                    cout << "\n\n\t\t\t\tStart The Questionaire\n";
                    int birthmonth,birthyear;
                    int currentmonth,currentyear;
                    int agey,agem;
                    cout<<"\n\n\t\tUsing C++, Find Your Age By Answering The Questions\n\n";
                    cout<<"\n\nEnter Your Birth Year(Eg:1989):";
                    cin>>birthyear;
                    cout<<"\n\nEnter Your Birth Month(Eg:7):";
                    cin>>birthmonth;
                    cout<<"\n\nEnter The Current Month(Eg:7):";
                    cin>>currentmonth;
                    cout<<"\n\nEnter The Current Year(Eg:2010):";
                    cin>>currentyear;
                    agey=currentyear-birthyear;
                    agem=currentmonth-birthmonth;
                    cout<<"\n\n\t\t\tYour Age is "<<agey<<" Years And "<<agem<<" Months ";
                    printf("\n\n\n\t Thanks for using the age calculator. Have a great day.\n");
                    break;
                case 2:
                    cout << "End of Program.\n";
                    gameOn = false;
                    break;
                default:
                    cout << "Not a Valid Choice. \n";
                    cout << "Choose again.\n";
                    cin >> choice;
                    break;
                    (gameOn!=false);
                    return 0;
                }
                gameOn!=false;
            }
        }
        return (0);
    }
}
Zoe123
Newbie Poster
2 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

Please remember to use code tags. It makes helping you much easier. I think you need to adjust how you're closing your { } throughout your program. What specifically isn't working now?

Edit: What did you change from last time? This looks exactly the same ... ?

Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
 

I may start just deleting posts that fail to use code tags. :@

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Why the need to mix cout and printf?

Chilton
Junior Poster
106 posts since Oct 2009
Reputation Points: 34
Solved Threads: 16
 

Why did you remove all the while keywords in the second post?

Back to your 1st post:
I have some menu problems. So this is my first time, and I need someone to please solve the problem for me.
We don't solve thingsfor you. We help you solve them yourself.

I need someone to please solve the problem for me. Could someone help me. Get rid of this problem. Please someone help me. == code == Could someone help me. Get rid of this problem.


4 times is begging. And if you think about it, isn't posting "HELP" on a help board redundant?


Why did you copy your program into thedefault case of the first switch? Just let the default go without changing gameOn -- two lines. And what's with that last while() loop that does nothing?

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: