Thread: i cannot figure out whats wrong with this!!

  1. #1
    Registered User MOH123's Avatar
    Join Date
    Aug 2005
    Posts
    20

    i cannot figure out whats wrong with this!!

    ok, im making a text-adventure game and i have just started but i already have problems!
    this is the code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int a,b,c,d;
        char name;
        
        cout << "Hello, and welcome to Hatred. This is a text-base RPG so get your\n";
        cout << "notepad ready! Press enter when you are ready to begin.\n";
        cin.get();
        cout << "What would you like to be called? ";
        cin >> name;
        cin.ignore();
        cout << "OK then, " << name << "press enter when you are ready to begin your adventure\n";
        cin.get();
    }
    and what its supposed to do is let me type the name and it will instert it in the next text. but when i press enter after i type the name it just closes. any ideas why?
    Games by Me:
    Using C++:
    Text Adventure game - No Name [||||||||||] - =0% -
    -----------------------------------------------
    Using RPG Maker 2000 and 2003:
    - No Games as of now -

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yeah, you're variable is a character, and I'm willing to bet you're typing a full name. So if you typed something like "Dave" the cin would take 'D' the ignore() would take 'a' and the get() would take 'v' and the program would close.

    Use the string datatype. Include <string> and read up on the tutorial on C++ style strings. There is alot you'll need to know about them.
    Sent from my iPadŽ

  3. #3
    Registered User MOH123's Avatar
    Join Date
    Aug 2005
    Posts
    20
    what if i did
    int name; ? will that make a difference? i remember doing names and stuff before but i kinda forgot how, obviously.
    Games by Me:
    Using C++:
    Text Adventure game - No Name [||||||||||] - =0% -
    -----------------------------------------------
    Using RPG Maker 2000 and 2003:
    - No Games as of now -

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yeah it would make a difference. You'd type in a character and a newline, it'd look for an integer, won't find it, the stream would fail and all cin statements would be ignored for the rest of the program. You need to use a string variable.

    Code:
    int main() {
       string foo = "Hello World!";
    
       cout << foo;
       cin.get();
       
       return 0;
    }
    Sent from my iPadŽ

  5. #5
    Registered User MOH123's Avatar
    Join Date
    Aug 2005
    Posts
    20
    ok i figured out how to do it. thanks :P
    Games by Me:
    Using C++:
    Text Adventure game - No Name [||||||||||] - =0% -
    -----------------------------------------------
    Using RPG Maker 2000 and 2003:
    - No Games as of now -

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! I cannot figure out what I'm doing wrong here!
    By chsindian595 in forum C Programming
    Replies: 2
    Last Post: 08-02-2008, 03:18 AM
  2. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM
  3. please help, i can't figure out what's wrong
    By Leeman_s in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2002, 09:13 PM
  4. What is wrong here?????
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2002, 10:51 AM
  5. What did i do wrong? Code inside
    By The Rookie in forum C Programming
    Replies: 2
    Last Post: 05-18-2002, 08:14 AM