Whats wrong with this...

This is a discussion on Whats wrong with this... within the C++ Programming forums, part of the General Programming Boards category; This is my code: Code: #include "backtobasics.h" #include "baic2.h" using namespace std; int menu() { int choice; cout << " ...

  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
    21,720
    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    407
    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....

  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
    407
    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?

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    407
    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.

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, 09: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, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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