Grateful for help

This is a discussion on Grateful for help within the C++ Programming forums, part of the General Programming Boards category; Hi, I begun the work on this game for some minutes ago: Code: #include <windows.h> #include <iostream.h> #include <stdio.h> #define ...

  1. #1
    Banned
    Join Date
    May 2004
    Posts
    55

    Grateful for help

    Hi, I begun the work on this game for some minutes ago:
    Code:
    #include <windows.h>
    #include <iostream.h>
    #include <stdio.h>
    #define WMAX 6
    #define PMAX 60
    using namespace std;
    inline void start(void)
    {
        char wayinput[WMAX];
        char pname[PMAX];
        cout << "What do you want to be called?\n";
        cin.getline (pname, 60);
        cout << "\nOk, hello " << pname << ", right now you are in Hilstone Village.";
    }
    inline void about(void)
    {
        cout << "\n                   FT-Adventure is a text-based game";
        cout << "\n                   Made by Marcus Nordh / Sweden in C++.";
    }
    int main()
    {
        HANDLE hOut;
        int selection;
        hOut = GetStdHandle (STD_OUTPUT_HANDLE);
        SetConsoleTextAttribute(hOut,
                            FOREGROUND_RED |
                            FOREGROUND_INTENSITY);
    
    
     
        cout << "\n                   **Welcome to a text-based game made in C++!**"<<endl;
        SetConsoleTextAttribute(hOut,
                        FOREGROUND_RED |
                        FOREGROUND_GREEN |
                        FOREGROUND_BLUE |
                        FOREGROUND_INTENSITY);
        cout << "\n                   1. Start\n";
        cout << "\n                   2. Exit\n";
        cout << "\n                   3. About";
        cin>>selection;
        switch(selection)
        {
            case 1:
            start();
            break;
            case 2:
            exit(0);
            break;
            case 3:
            about();
            break;
            default:
            MessageBox(NULL, "Wrong input, quitting", "Error", MB_OK | MB_ICONERROR);
            exit(0);
            
        }
        getchar();
        getchar();
    }
    Somewhere here its wrong:
    Code:
    inline void start(void)
    {
        char wayinput[WMAX];
        char pname[PMAX];
        cout << "What do you want to be called?\n";
        cin.getline (pname, 60);
        cout << "\nOk, hello " << pname << ", right now you are in Hilstone Village.";
    }
    cause I can compile it but when I press 1 (start the game) it doesnt let me input my name, instead it says "Ok, hello , right now you..."

    Many thanks for answer

  2. #2
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    Make it this::
    cin.getline(pname, 60, '\n')
    The '\n' tells it to wait until the person hits enter(or should)

  3. #3
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    you still have a newline in the input stream from when you hit enter after the 1
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

  4. #4
    Banned
    Join Date
    May 2004
    Posts
    55
    nope Lloy it didn't worked

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    Noxir i use this function for my clearing input try this
    Code:
    void clearInput()
    {
    	cin.clear();
    	cin.ignore(numeric_limits<int>::max(),'\n');
    }
    heres what yours would look like
    Code:
    #include <windows.h>
    #include <iostream>
    #include <limits>//added this
    using namespace std;
    
    void clearInput()
    {
    	cin.clear();
    	cin.ignore(numeric_limits<int>::max(),'\n');//for this :)
    }
    inline void start(void)
    {
        clearInput();
        char wayinput[WMAX];
        char pname[PMAX];
        cout << "What do you want to be called?\n";
        cin.getline (pname, 60);
        cout << "\nOk, hello " << pname << ", right now you are in Hilstone Village.";
    }
    //rest of your code
    Woop?

  6. #6
    Banned
    Join Date
    May 2004
    Posts
    55
    thnx a lot

  7. #7
    I'm less than sure.... abyssphobia's Avatar
    Join Date
    Aug 2004
    Posts
    112
    I dont understood how does for example (cin.getaline) works, why it work like cin>> ????
    Please Im a begginer thx in advance love, abyssphobia

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    getline gets a string up to a new line character up to a certain length. The standard >> operator just gets up to the first whitespace character.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I believe >> transfers data from the input buffer into the variable until it encounters a char that is inconsistent with the variable. To test it yourself try this:

    Code:
    #include <iostream>
    using namespace std;
     
    int main()
    {
      int num;
      char ch;
      cout << "enter 123#";
     
      //enter 123# yourself
     
      cin >> num >> ch;
      cout << num << endl;
      cout << ch;
    }
    Strings are a little different. Any char is valid for a string. Therefore, the default for >> is to stop at the first whitespace char(space, tab, newline, etc). Therefore, to allow you to put whitespace in a string, you need to use getline(). There are two forms of getline()---one for C style strings and one for STL strings. The C style version has three parameters:

    cin.getline(stringToUse, maxNumChar, terminatingChar);

    The third parameter defaults to newline, but you can override that by indicating whatever terminating char you wish. The STL string version also has three parameters and use of the terminating char is the same, but the syntax is a bit different:

    getline(streamToUse, stringToUse, terminatingChar);

    Since an STL string can be of any length desired, there is no need to indicate a maximal length as in the C style string version.

  10. #10
    Banned
    Join Date
    May 2004
    Posts
    55
    Many asks "why cin.getline instead of cin>>?" the answer is:

    You can't write sentences in cin>>

    You can write sentences in cin.getline

    x)

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    I make some change in your code ,and it 's work OK!
    I think it's important that after "cout<<????" statement ADDing a "<<endl",
    endl not only make a change line ,
    but also "kick off" the content in the BUFFER ' room,
    so the content will be immediately send to display .

    Code:
    #include <windows.h>
    #include <iostream.h>
    #include <stdio.h>
    #define WMAX 6
    #define PMAX 60
    //using namespace std;      //this statement not supportted in my vc++.
    inline void start(void)
    {
        char wayinput[WMAX];
        char pname[PMAX];
        cout << "What do you want to be called?\n";   
        //in the end ,must add "<<endl;",if or not , this message dont display!
    
        cin>>pname;    //instead of "cin.getline (pname, 60);", 
                                //I donnt know reason,but it's wrok in my computer!
        
        cout << "\nOk, hello " << pname << ", right now you are in Hilstone Village.";
          //add "<<endl;" in the end.
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I would be very grateful for some help!
    By mysterymeat in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-26-2002, 11:57 AM

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