Need help with simple code. Very new to C++

This is a discussion on Need help with simple code. Very new to C++ within the C++ Programming forums, part of the General Programming Boards category; OK, I am very new to C++ so if anything is incorrect or there is an easier way or whatever, ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    20

    Need help with simple code. Very new to C++

    OK, I am very new to C++ so if anything is incorrect or there is an easier way or whatever, please let me know.
    Here is some code I wrote just to mess around and try to start a program. I was trying to create a simple menu.
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int Selection;
    	cout << "Welcome to BlackJack! Choose an option below to continue:\n";
    	cout << "\n";
    	cout << "1)New Game\n";
    	cout << "2)Restore Game\n";
    	cout << "3)Exit\n";
    	cout << "\n";
    	cout << "Selection: ";
    	cin >> Selection;
    	switch (Selection)
    	{
    	case 1:
    		system("cls");
    		break;
    	case 2:
    		system("cls");
    		break;
    	case 3:
    		system("cls");
    		break;
    	default:
    		system("cls");
    		cout << "Invalid Selection";
    		break;
    	}
    	system("pause");
    	return 0;
    }
    As you can see, my 3 cases don't do anything besides clear the screen. I was going to make each case go to another submenu after clearing the screen but I haven't gotten to that yet. What I was trying to figure out is... on my default case, I want to make it return to the beginning and ask for the selection again after displaying it was an Invalid Selection. how can I do this?

    Also, I tried making line 13 say:
    Code:
    while(cout << "Selection: ");
    But that just made the program keep displaying Selection over and over again in an infinite loop? can someone explain why this is please?

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Also, I tried making line 13 say:
    Code:
    while(cout << "Selection: ");
    But that just made the program keep displaying Selection over and over again in an infinite loop? can someone explain why this is please?
    A while loop tests a condition inside the parentheses and evaluates it to true or false, since its evaluating it to true or false, it expects a boolean expression.

    so the following expression

    Code:
    int x = 5;
    while(x == 5) 
    {
       cout << "Hello" << endl;
       x = 6;
    }
    will on only run while x eqals 5.

    note that if you put a semi colon after the while loop like you did
    Code:
     while(x == 5);
    the program will run into an infinite loop as x will always remain 5.
    this translates to while x equals five, do nothing!

    Also, here is a really simple way of testing for the default case
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int Selection = 0;
    	while(Selection < 1 || Selection > 3)
    	{
    		cout << "Welcome to BlackJack! Choose an option below to continue:\n";
    		cout << "\n";
    		cout << "1)New Game\n";
    		cout << "2)Restore Game\n";
    		cout << "3)Exit\n";
    		cout << "\n";
    	
    		cout << "Selection: ";
    		cin >> Selection;
    	
    		switch (Selection)
    		{
    		case 1:
    			system("cls");
    			break;
    		case 2:
    			system("cls");
    			break;
    		case 3:
    			system("cls");
    			break;
    		default:
    			system("cls");
    			cout << "Invalid Selection\n";
    			break;
    		}
    	}
    	system("pause");
    	return 0;
    }
    the line
    Code:
    while(Selection < 1 || Selection > 3)
    translates to while the selection is less than 1 or greater than 3, do what is inside the curly braces. This code is guaranteed to run once as Selection is assigned to 0 zero in the start and will only execute again if an invalid input is entered.

    There are other ways of testing for invalid input, but this is a really simple one.

    Hope this helps.
    Last edited by Spidey; 07-13-2009 at 01:22 AM.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    OK Thank you, what my problem was that I didnt understand is that when I put the semi colon after the while loop it would make it an infinite loop

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    So here is my new updated code and it does what I want it to do pretty much:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int Selection = 0;
    	cout << "Welcome to BlackJack! Choose an option below to continue:\n";
    	cout << "\n";
    	cout << "1)New Game\n";
    	cout << "2)Restore Game\n";
    	cout << "3)Exit\n";
    	cout << "\n";
    	while(Selection < 1 || Selection > 3)
    	{
    		cout << "Selection: ";
    		cin >> Selection;
    		switch (Selection)
    		{
    		case 1:
    			system("cls");
    			break;
    		case 2:
    			system("cls");
    			break;
    		case 3:
    			system("cls");
    			break;
    		default:
    			cout << "Invalid Selection\n";
    			cout << "\n";
    			break;
    		}
    	}
    	system("pause");
    	return 0;
    }
    Now the next thing I want to do is make case 1, case 2, and case 3 go to another submenu
    I was planning on having New Game go to New Game 1, New Game 2, New Game 3, etc.
    and Restore game go to Restore Game 1, Restore Game 2, Restore game 3, etc.
    I was wondering if there is a way to make this more oganised by maybe having something like:
    newgame(); after the system("cls"); under case 1
    restoregame(); after the system("cls"); under case 2
    And then having defining what newgame() and restoregame() do at the bottom of the program I think?
    I think this is called functions? Sorry I forgot how they work if someone could explain please.
    And also sorry for being such a nooby coder ;(

  5. #5
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    I was wondering if there is a way to make this more oganised by maybe having something like:
    newgame(); after the system("cls"); under case 1
    restoregame(); after the system("cls"); under case 2
    And then having defining what newgame() and restoregame() do at the bottom of the program I think?
    I think this is called functions? Sorry I forgot how they work if someone could explain please.
    And also sorry for being such a nooby coder ;(
    Yep, thats a good way to handle it. A function is basically a group of statements which is executed when called from a certain part in a program.

    Here is how you declare a simple function

    Code:
    type name (parameters)
    {
      //...do stuff
    }
    So, your new game function could look like this

    Code:
    void NewGame()
    {
      //..initialize a new game
    }
    This declares NewGame as a function which returns type void(nothing) and takes nothing as a parameter. Your main() is an example of a function which takes nothing as its parameters and returns and integer.

    Then you could call it like this
    Code:
          case 1:
    		system("cls");
                    NewGame();
    		break;
    Note: The compiler must be able to see the function declaration before it is called. There are two ways to handle this-

    a) define the function before main()
    b) declare a function prototype before main and define the function after main.
    This basically tells the compiler that the function is defined somewhere in the code even though it is being called earlier in the program. A prototype is declared exactly like the function itself with a semicolon instead of the curly braces.

    eg:
    Code:
    void NewGame();
    This prototype must be placed before the function is called by your program. In your case, before main().

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    Well here is my new updated progarm:
    Code:
    #include <iostream>
    using namespace std;
    
    int newgame();
    int restoregame();
    
    int main()
    {
    	int WelcomeSelection = 0;
    	cout << "Welcome to BlackJack! Choose an option below to continue:\n";
    	cout << "\n";
    	cout << "1)New Game\n";
    	cout << "2)Restore Game\n";
    	cout << "3)Exit\n";
    	cout << "\n";
    	while(WelcomeSelection < 1 || WelcomeSelection > 3)
    	{
    		cout << "Selection: ";
    		cin >> WelcomeSelection;
    		switch (WelcomeSelection)
    		{
    		case 1:
    			system("cls");
    			newgame();
    			break;
    		case 2:
    			system("cls");
    			restoregame();
    			break;
    		case 3:
    			system("cls");
    			break;
    		default:
    			cout << "Invalid Selection\n";
    			cout << "\n";
    			break;
    		}
    	}
    	system("pause");
    	return 0;
    }
    int newgame()
    {
    	int NewGameSelection = 0;
    	cout << "Create a game\n";
    	cout << "1)New Game 1\n";
    	cout << "2)New Game 2\n";
    	cout << "3)New Game 3\n";
    	cout << "\n";
    	while(NewGameSelection < 1 || NewGameSelection > 3)
    	{
    		cout << "Selection: ";
    		cin >> NewGameSelection;
    		switch (NewGameSelection)
    		{
    		case 1:
    			system("cls");
    			break;
    		case 2:
    			system("cls");
    			break;
    		case 3:
    			system("cls");
    			break;
    		default:
    			cout << "Invalid Selection\n";
    			cout << "\n";
    			break;
    		}
    	}
    	return 0;
    }
    int restoregame()
    {
    	int RestoreGameSelection = 0;
    	cout << "Restore a game\n";
    	cout << "1)Restore Game 1\n";
    	cout << "2)Restore Game 2\n";
    	cout << "3)Restore Game 3\n";
    	cout << "\n";
    	while(RestoreGameSelection < 1 || RestoreGameSelection > 3)
    	{
    		cout << "Selection: ";
    		cin >> RestoreGameSelection;
    		switch (RestoreGameSelection)
    		{
    		case 1:
    			system("cls");
    			break;
    		case 2:
    			system("cls");
    			break;
    		case 3:
    			system("cls");
    			break;
    		default:
    			cout << "Invalid Selection\n";
    			cout << "\n";
    			break;
    		}
    	}
    	return 0;
    }
    I actually figured it out before you posted that, that's why I am replying so fast in case you were wondering how I coded that so fast LoL.
    Anyways...
    I don't really get the void thing?
    Also, please let me know anything I am doing that is not as efficient or anything as other ways because I am just trying to learn and get better at C++ right now.
    Thanks for all your help Spidey by the way, you are really helpful.

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    Oh OK, so basically instead of having this at the end of my defined function:
    Code:
    return 0;
    I can just remove that and change the function from int NewGame() to void NewGame()
    correct?
    Also, in my main function, I do not need the return 0; either because it automatically does it, right?
    Alsooo, if I wanted to, I could just declare the functions right after defining them above the int main() instead of declaring them at the bottom of the program?
    Sorry for so many questions, I really do appreciate your help tho!

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    And to define and declare a function about the int main() I can just put it like this without the semicolon:
    Code:
    void NewGame()
    {
       // type code here
    }
    Or would I have to do it like this:
    Code:
    void NewGame();
    void NewGame()
    {
       // type code here
    }
    Last edited by PersianStyle; 07-13-2009 at 03:14 AM.

  9. #9
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Oh OK, so basically instead of having this at the end of my defined function:
    Code:
    return 0;
    I can just remove that and change the function from int NewGame() to void NewGame()
    correct?
    Also, in my main function, I do not need the return 0; either because it automatically does it, right?
    Alsooo, if I wanted to, I could just declare the functions right after defining them above the int main() instead of declaring them at the bottom of the program?
    Sorry for so many questions, I really do appreciate your help tho!
    Correct, you can make it type void and just write
    Code:
    return ;
    instead of
    Code:
    return 0;
    You do not need the return 0; in main but it is bad practice not to put it. Some compilers may give you an error about this. If you dont want main to return a value you can declare it as
    Code:
    void main()
    {
    
      return;
    }
    instead.

    Yes, if you define your functions above main you do not need the prototype.
    like this -

    Code:
    void NewGame()
    {
     //... do stuff
    }
    
    int main()
    {
       NewGame();
       return 0;
    }

  10. #10
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    By the way, I don't know why your newgame() is branching into 3 more new games. I'm assuming your trying to write a program which allows you to save and load games stored on disk ?
    That is great but since your just starting C++, I would recommend just have one new game and make it start the blackjack program when it is called.
    After you've completed that you can add more features and save your data on disk and have the restore game etc.

  11. #11
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Quote Originally Posted by Spidey View Post
    If you dont want main to return a value you can declare it as
    Code:
    void main()
    {
    
      return;
    }
    instead.
    Incorrect. That is not standard C++.

  12. #12
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    why do you say to have return; instead of return 0;
    I tried it without putting return; at all and just completely removing that line and it worked fine?

    Yes I know I should start out with having just one game. I was just messing around and trying stuff for practice.
    For starting the blackjack game, I don't even know how to assign random cards or anything yet.

  13. #13
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    Quote Originally Posted by Tux0r View Post
    Incorrect. That is not standard C++.
    When you say that is not standard C++ do you mean the code won't work or you just mean it is not good form or w\e

  14. #14
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Quote Originally Posted by PersianStyle View Post
    When you say that is not standard C++ do you mean the code won't work or you just mean it is not good form or w\e
    It means you should keep the hell away from it because it's not guaranteed to compile.

  15. #15
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    Quote Originally Posted by Tux0r View Post
    It means you should keep the hell away from it because it's not guaranteed to compile.
    well I tried to put return; just to see and it compiled
    But I just dont put a return statement at all and remove the line completely.
    Is this correct?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 11-23-2005, 07:53 AM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM
  4. Simple Code, looking for input.
    By Alien_Freak in forum C Programming
    Replies: 3
    Last Post: 03-03-2002, 10:34 AM

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