Thread: Whats wrong with this...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    12

    Whats wrong with this...

    This is my code:
    Code:
    #include "backtobasics.h"
    #include "baic2.h"
    
    using namespace std;
    
    int menu()
    {
        int choice;
    
        cout << " **** Menu ****" << endl << endl;  //States the options on the menu 
        cout << "(1) Play Game." << endl;
        cout << "(2) Options." << endl;
        cout << "(3) Multiplayer." << endl;
        cout << "(4) Quit." << endl << endl;
    
        cout << ": ";
        cin >> choice;  //enter there choice, 1,2,3 or 4
        return choice;
    	return 0;
    } 
      int main()
      {
        int choice = menu(), i, second;  
    	string firstName, lastName;
        cout << "You chose option " << choice << endl;  //tells the user their choice
    	cout << "Is this correct? If yes click 1, if no click 2. (2 mistakes Remaining)" << endl;  //asks them if it is correct (1) or not (2) 
    		cout << " (1)"<<endl;
    		cout << " (2)"<<endl;
    		cin>>second;  //they enter their choice 1 or 2
    if (second==1){ // if they enter 1, it says that they can continue
    			cout<< "Click to continue."<<endl;
    			cin.get();
    			return 0;
    }
    		else if (second==2) // if they enter 2, it gives them the menu again
    		{
    			cout << " **** Menu ****" << endl << endl;  //States the options on the menu 
        cout << "(1) Play Game." << endl;
        cout << "(2) Options." << endl;
        cout << "(3) Multiplayer." << endl;
        cout << "(4) Quit." << endl << endl;
    
        cout << ": ";
        cin >> choice;  //enter there choice, 1,2,3 or 4
        int menu(), i, second;  
    
        cout << "You chose option " << choice << endl;  //tells the user their choice
    	cout << "Is this correct? If yes click 1, if no click 2. (1 mistake Remaining)" << endl;  //asks them if it is correct (1) or not (2) 
    		cout << " (1)"<<endl;
    		cout << " (2)"<<endl;
    		cin>>second;  //they enter their choice 1 or 2
    if (second==1){ // if they enter 1, it says that they can continue
    			cout<< "Click to continue."<<endl;
    			cin>>i
    				;
    			return 1;
    		}
    		else if (second==2) // if they enter 2, it gives them the menu again
    		{
    			while (second==2)
    				second++;
    				cout << " **** Menu ****" << endl << endl;  //States the options on the menu 
        cout << "(1) Play Game." << endl;
        cout << "(2) Options." << endl;
        cout << "(3) Multiplayer." << endl;
        cout << "(4) Quit." << endl << endl;
    
        cout << ": ";
        cin >> choice;  //enter there choice, 1,2,3 or 4
        return choice;
    
    
    		}
    }
    		else (second>2);  // if they enter a number bigger than 2, it tells them that it is invalid
    		{
    			cout<<"You have not entered a valid choice."<<endl;
    			cin>>i;
    			return 1;
    		}
    		{
    	cout<<"Welcome, what is your name? First and Last. "<<endl;
    		cin>>firstName>>lastName;
    	cout<<endl<<"Hello " << firstName << lastName;
    	cin.get ();
    	return 0;
      }
    		return 0;
      }
    The bottom part does not play. Does anyone know why?
    Code:
    {
    	cout<<"Welcome, what is your name? First and Last. "<<endl;
    		cin>>firstName>>lastName;
    	cout<<endl<<"Hello " << firstName << lastName;
    	cin.get ();
    	return 0;
      }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It would help if you indented your code properly, e.g.,
    Code:
    #include "backtobasics.h"
    #include "baic2.h"
    
    using namespace std;
    
    int menu()
    {
        int choice;
    
        cout << " **** Menu ****" << endl << endl;  //States the options on the menu
        cout << "(1) Play Game." << endl;
        cout << "(2) Options." << endl;
        cout << "(3) Multiplayer." << endl;
        cout << "(4) Quit." << endl << endl;
    
        cout << ": ";
        cin >> choice;  //enter there choice, 1,2,3 or 4
        return choice;
        return 0;
    }
    
    int main()
    {
        int choice = menu(), i, second;
        string firstName, lastName;
        cout << "You chose option " << choice << endl;  //tells the user their choice
        cout << "Is this correct? If yes click 1, if no click 2. (2 mistakes Remaining)" << endl;  //asks them if it is correct (1) or not (2)
        cout << " (1)"<<endl;
        cout << " (2)"<<endl;
        cin>>second;  //they enter their choice 1 or 2
        if (second==1){ // if they enter 1, it says that they can continue
            cout<< "Click to continue."<<endl;
            cin.get();
            return 0;
        }
        else if (second==2) // if they enter 2, it gives them the menu again
        {
            cout << " **** Menu ****" << endl << endl;  //States the options on the menu
            cout << "(1) Play Game." << endl;
            cout << "(2) Options." << endl;
            cout << "(3) Multiplayer." << endl;
            cout << "(4) Quit." << endl << endl;
    
            cout << ": ";
            cin >> choice;  //enter there choice, 1,2,3 or 4
            int menu(), i, second;
    
            cout << "You chose option " << choice << endl;  //tells the user their choice
            cout << "Is this correct? If yes click 1, if no click 2. (1 mistake Remaining)" << endl;  //asks them if it is correct (1) or not (2)
            cout << " (1)"<<endl;
            cout << " (2)"<<endl;
            cin>>second;  //they enter their choice 1 or 2
            if (second==1){ // if they enter 1, it says that they can continue
                cout<< "Click to continue."<<endl;
                cin>>i
                ;
                return 1;
            }
            else if (second==2) // if they enter 2, it gives them the menu again
            {
                while (second==2)
                    second++;
                cout << " **** Menu ****" << endl << endl;  //States the options on the menu
                cout << "(1) Play Game." << endl;
                cout << "(2) Options." << endl;
                cout << "(3) Multiplayer." << endl;
                cout << "(4) Quit." << endl << endl;
    
                cout << ": ";
                cin >> choice;  //enter there choice, 1,2,3 or 4
                return choice;
            }
        }
        else
            (second>2);  // if they enter a number bigger than 2, it tells them that it is invalid
    
        {
            cout<<"You have not entered a valid choice."<<endl;
            cin>>i;
            return 1;
        }
        {
            cout<<"Welcome, what is your name? First and Last. "<<endl;
            cin>>firstName>>lastName;
            cout<<endl<<"Hello " << firstName << lastName;
            cin.get ();
            return 0;
        }
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    if you look at lazerlights example of proper indentation, then it becomes easier to notice the problem. Focus on your opening and closing braces... ask yourself why are those braces around the code that isn't running....
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    12
    i have tried laserlights code and this doesn't work either?

  5. #5
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Lol he didn't change your code content he indented it(changed format). I gave you a hint... did you look at what I said?
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    12
    its all gobldy gook to me. I'm new, what does your code mean? i've never seen anything like it before.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It's just YOUR code reformatted so the rest of us can read it without going blind.

    Here's one free tip.
    > else
    > (second>2); // if they enter a number bigger than 2, it tells them that it is invalid
    else has no conditional exression, it is simply everything left over after all the if and else if conditions.

    Here's another tip
    A development process
    Write the code in small parts you CAN understand.
    Like for example, print the menu and get the input WITHOUT doing anything other than printing "You chose option 1".

    What isn't going to work is for you to write the whole thing in one edit session, find that inevitably, it won't compile, then dump the whole mess on a forum for someone else to fix.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by ftball22 View Post
    its all gobldy gook to me. I'm new, what does your code mean? i've never seen anything like it before.
    I lol'd

    I mean, shouldn't you recognize your own code, even if a few spaces are added? I can't imagine why anyone wouldn't; unless it's not your own code in the first place of course.

  9. #9
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Sadly I think he was refering to my signature thinking it was code I was posting to help him, rather than reading my actual suggestion.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM