Thread: Easy but do not understand XD.

  1. #1
    Registered User GamerProduction's Avatar
    Join Date
    Apr 2007
    Location
    http://chillxvillex.miniville.fr/ plzz help my village
    Posts
    9

    Easy but do not understand XD.

    Well, i'm making my first rpg text game.

    I'm getting a problem. While it's asking my name in the program, it closes.

    Code:
    #include <iostream>
    #include <string>
    #include <windows.h>
    #include <time.h>
    
    using namespace std;
    
    int main (void)
    {
        int name;
        int nation;
        
        
        cout << " Version 0.0.1 " << endl;
        cin.get();
        
        
        
    
        cout << "What's your name? \n\n\n"<<endl;
        cin.get();
        cin >> name;
        
        cin.get();
        
        
        cout <<"Which nation will you choose\n\n\n\n"<<endl;
        cin.get();
        
        cout <<" 1 = Orc "<<endl;
        cout <<" 2 = Human "<<endl;
        
        cin.get();
        
        switch (nation){
               
               case 1:
                    cout << " So you're an orc.. "<<endl;
                    cin.get();
                    
               case 2: 
                    
                       cout << " So you're a Human.. "<<endl; 
                       cin.get();
                    
        
    }
        
        
        
        
        
        cin.get();
        
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int name;
    Typing in "barney" isn't going to work.
    Pick a more appropriate data type for a name, say a std::string
    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.

  3. #3
    Registered User GamerProduction's Avatar
    Join Date
    Apr 2007
    Location
    http://chillxvillex.miniville.fr/ plzz help my village
    Posts
    9
    XD Thanks, But after changing it into a string, How can i store the name people enter. Example: cin >> name, doesn't work anymore with string.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You mean, a name with spaces like "Barney Rubble" ?

    1. Use std::string

    2. Use getline() for reading ALL input, then use something like a string stream to extract the data you need.

    Then you don't have to keep littering your code with random get() to strip out all the unexpected newlines as you flip from one input style to another.
    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.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Example: cin >> name, doesn't work anymore with string.
    Why not? What problems are you having with it? It should work, although as Salem pointed out it will only grab a single word.



    >> Then you don't have to keep littering your code with random get() to strip out all the unexpected newlines as you flip from one input style to another.

    But then you would have to litter your code with usage of the stringstream. For more complex input I might choose that route, but for simple stuff I think the use of ignore() is the better alternative.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg fault in easy, easy code
    By lisa1901 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2007, 05:28 AM
  2. Java vs C to make an OS
    By WOP in forum Tech Board
    Replies: 59
    Last Post: 05-27-2007, 03:56 AM
  3. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM
  4. Replies: 20
    Last Post: 05-25-2002, 07:14 PM
  5. EASY GUI development.. like with Qt?
    By rezonax in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2001, 01:18 PM