Complete Beginner [Need Help]

This is a discussion on Complete Beginner [Need Help] within the C++ Programming forums, part of the General Programming Boards category; Hi, I just recently started learning C++ and I bought a book called: Beginning C++ Game Programming By Michael Dawson ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    6

    Question Complete Beginner [Need Help]

    Hi,

    I just recently started learning C++ and I bought a book called:

    Beginning C++ Game Programming
    By Michael Dawson

    I've been learning quite a bit, but mostly on my dad's mac. Now at my mother's I don't have a mac with i-code (not sure how the ide is called), so I downloaded the BloodShed Dev-C++ IDE which came on the cd with the book. I seemed to like it, I found it a bit worse, but managable, and I came to a problem. I did a helloWorld to test it out, so my code:

    Code:
    //HelloWorld
    
    #include <iostream>
    using namespace std;
    
    int main()
    {
    cout << "Hello, World" << endl;
    }
    What happened apparently is that the program did it, but it just closed right after.. So in the book they give a solution:

    Code:
    std::cout << "Press Enter to exit" << endl;
    std::cin.ignore(std::cin.rdbuf()->in_avail()+1);
    I put that in, and it worked fine. Then I went on to do the stuff where I left off in the book. So I copied up a menu (using switch) example they had in the book.

    So here it is:

    Code:
    //Difficulty Menu
    
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
    cout << "Difficulty Levels\n\n";
    cout << "1 - Easy\n";
    cout << "2 - Normal\n";
    cout << "3 - Hard\n";
    
    int choice;
    cout << "Choice: ";
    cin >> choice;
    
    switch (choice)
    {
    case 1:
        cout << "You picked Easy.\n";
        break;
    case 2:
        cout << "You picked Normal.\n";
        break;
    case 3:
        cout << "You picked Hard.\n";
        break;
    default:
        cout << "You made an illegal choice.\n";
    }
    
    cout << "Press Enter to exit" << endl;
    cin.ignore(cin.rdbuf()->in_avail()+1);
    
    return 0;    
    }
    As you can see at the end I used the enter to exit thing agian, but I realised when I ran the program, I pressed 2 to choose normal for example, and it just closed it (so I presumed since I'm pressing enter to confirm the difficulty, I thought perhaps I should try using some other key for the exit. Is that possible? And how would I do that? I'm not quite sure how the
    Code:
    cout << "Press Enter to exit" << endl;
    cin.ignore(cin.rdbuf()->in_avail()+1);
    works either. If someone could explain that, it would be awesome

    Thanks,
    -Vic

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    The cin.rdbuf()->in_avail() function returns the number of characters available in the input stream. Sometimes, when the user types in an answer, your program will only read in part of the answer. That happens here. The user types 2<enter>. That puts the character '2' and the newline character '\n' into the stream. So when you read in the character '2' with cin >> choice, the newline is still in the stream. The call to cin.rdbuf()->in_avail() will return 1, since there is still one character left in the stream.

    Then, cin.ignore() is called with that value plus one. In this case that's two. It means to remove and ignore two characters from the input stream. So basically that full line of code removes and ignores all the characters in the input stream plus one more. That is why the program sits there waiting for you to hit enter, it needs another character to remove and ignore from the stream and it cannot get it until the user sends input to it by pressing <enter>.

    I don't know why you are having a problem, but that code works for me on a different compiler. The window closes if I type something other than a number, but that's because cin goes into a fail state which throws everthing off.

    Either you accidentally typed a letter instead of 2, or your compiler is not working the same as mine (I'm not sure which is correct according to the standard). You could switch it to the following which is basically equivalent and generally works on all compilers:
    Code:
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin.get();
    You have to #include <limits> to use that. It does basically the same thing - ignores everything in the input stream up to the newline frorm the user, then waits for one more character.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    6
    Thanks, it worked! I was wondering, is there a way to set it so I don't have to do that with every program. It's kind of a pain to remember that, but I'll try if I have to, and once I use it quite abit I guess I'll remember it still nice to know if there's a way around that.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    You can open a console window and change directories to the one that contains the executable. Then type in the executable name and hit enter. That console window won't close immediately. If you create a shortcut to the console prompt at the correct directory, then anytime you want to run your program you just open the shortcut and type in the exe name (or hit the up arrow if you've already typed it in once).

    Some IDE's have a feature that keeps the console window open when you run it, but that is just something extra and normally the console closes when the program it opened for is done. If you have a debugger you can set a breakpoin at the last line of main(), which break the program just before it finishes so you can look at the console window.

  5. #5
    -JM
    -JM is offline
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    I'm getting in the habit of adding cin.get() to everything, preceded by cin.ignore() if I've used cin for anything. Not a big deal.

  6. #6
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    A possible quick alternative is getch()

    Code:
    #include <conio.h.>
    // code and such
    ....
    // end of program
       getch();
    The getch() function will not return until the user hits a button (Press any key to continue). Also, the return value of getch() is the value of the key pressed, in case you want to only exit on a certain key.
    Code:
    while(getch()!=<key number>);
    And to test out what different keys return:

    Code:
    while(1)
    {
       cout << getch() << endl;
    }
    Last edited by jverkoey; 08-15-2005 at 03:48 PM. Reason: wrong header

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    getch() is compiler dependent, and is usually found in conio.h. It might not work on your mac.

  8. #8
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Ah, yes, my mistake. Should've paid a bit more attention to the OP.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    6
    Hmm I don't really want to start a new thread, but I got a new problem, I started working on this.

    Sorry about messy code

    Code:
    // Designers Network
    // Demonstrates logical operators
    
    #include <iostream>
    #include <string>
    #include <limits>
    
    using namespace std;
    
    int main() 
    {
    int security = 0;
    int exit = 0;
    do
    {
    	cout << "\tGame Designer's Network\n";
    	
    
    	string username;
    	cout << "\nUsername: ";
    	cin >> username; 
    
        string password;
    	cout << "Password: ";
    	cin >> password; 
    
    	if (username == "Lev" && password == "888")
    	{
    		cout << "\nHey ," << username <<".";
    		security = 5;
    	}
    
    	if (username == "Vic" && password == "333")
    	{
    		cout << "\nWhat's up ," << username <<"?";
    		security = 5;
    	}
    
    	if (username == "guest" || password == "guest")
    	{
    		cout << "\nWelcome, " << username << ".";
    		security = 1;
    	}
    	if (!security)
    		{
            cout << "\nYour login failed.\n";
            cout << "To try login again, press 0, then enter.";
    		cout << " To exit, press 1, then enter.\n\n\n";
            cin >> exit;
      } while((security == 0) && (exit = 0));
    
    cout << "\nThank you, press enter to exit.";
    
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin.get();
    return = 0;
    }
    It said I had an error with the
    Code:
     return = 0;
    I don't know why. And would this work? I want the user to login for example, and then if he logs in wrongly, I want him to say if he wants to try logging in again, or if not close the window. So I set up an exit variable, if he enters anything other than 0 he exits.

    So a couple of questions.

    1. Would this work considering I solve the return problem?
    2. How would I make it exit on a specific key? (I do want to use my mac when I'm at my dad's so, I guess getch wouldn't work?)

    Edit: I just realised my return was wrong, I accidently typed return = 0; and now remembered there shouldn't be an = but even fixed now, it's not working.

    Thanks for all your help, I really appreciate it,
    -Vic
    Last edited by Vintik; 08-15-2005 at 05:07 PM.

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    you dont need that std::
    Code:
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin.get();

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    There are three minor typos in your program ('=' instead of '==', return = 0 instead of return 0, and a missing closing brace), otherwise it should work fine.

    For your second question, I'm not sure what you mean. As you have it now, it will exit when you ask the user if they want to exit and they type anything other than 0. If you want to exit only if they type 1, then you would change your loop control to be exit != 1 instead of exit == 0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [NEED HELP] Project
    By James245 in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 10-15-2007, 07:08 AM
  2. [NEED HELP] I just can't solve this program problem!
    By frodonet in forum C Programming
    Replies: 7
    Last Post: 09-23-2007, 02:44 PM
  3. Help out a complete beginner
    By Jmcrofts in forum C++ Programming
    Replies: 7
    Last Post: 07-28-2007, 12:09 PM
  4. [Need Help] serial port programming on freeBSD
    By Hermisky in forum Networking/Device Communication
    Replies: 2
    Last Post: 07-13-2006, 06:51 PM
  5. Complete Beginner, HELP!!
    By britneyspy in forum C++ Programming
    Replies: 19
    Last Post: 06-12-2003, 11:01 AM

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