problem with a variable

This is a discussion on problem with a variable within the C++ Programming forums, part of the General Programming Boards category; ok, i used to be able to do this code but it wont work now!! can someone please tell me ...

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

    problem with a variable

    ok, i used to be able to do this code but it wont work now!! can someone please tell me what is wrong?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int name;
        
        cout<<"hello there, what is your name?\n";
        cin>>name;
        cout<<"welcome " << name << ", how are you today?\n";
        cin.ignore();
    }
    its supposed to put the name in the message (you probable know that) but all it does it abort the program. i know i have to be doing something VERY wrong in there but...what?

    -thanks in advance
    Games by Me:
    Using C++:
    Text Adventure game - No Name [||||||||||] - =0% -
    -----------------------------------------------
    Using RPG Maker 2000 and 2003:
    - No Games as of now -

  2. #2

  3. #3
    Registered User MOH123's Avatar
    Join Date
    Aug 2005
    Posts
    20
    hmm...that didnt help at all...
    Games by Me:
    Using C++:
    Text Adventure game - No Name [||||||||||] - =0% -
    -----------------------------------------------
    Using RPG Maker 2000 and 2003:
    - No Games as of now -

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Maybe try this:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string name;
        
        cout<<"hello there, what is your name?\n";
        cin>>name;
        cout<<"welcome " << name << ", how are you today?\n";
        cin.ignore();
        cin.get();
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    i know i have to be doing something VERY wrong in there but...what?
    You are trying to put a string of characters into a 'int' (something that only holds whole numbers...1..2..3...4029, ect).

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    33
    yea, lke the post above, instead of using an integer, which can only work with numbers, you must use a string. Also, you must use cin.get(); to keep the window from disappearing. cin.get(); waits for a key press before completing its operations.

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> but all it does it abort the program

    Yes. And by abort, you mean, the program finished and there was know further reason for windows to hold open the console. Instead, you could use any number of the solutions mentioned in the FAQ I gave, or JaWiB's code.

    >> You are trying to put a string of characters into a 'int'

    Nope, the >> operator is overloaded for int What he did is fine. Again, JaWiB's code is perfect.

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    33
    but the data still will not show on the screen like he was wanting, thus rendering the use of 'int' useless. But i agree with the first statement -- programs are told what to do and follow orders as written in the code -- they dont know what you want, they just do as told.

  9. #9
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You can't just disagree with obvious facts

    The >> overload is much more functional than just reading strings. Feel free to check the reference page too! http://www.cplusplus.com/ref/iostrea...ratorgtgt.html And if you are worried about things being displayed improperly, check out our own ostream's << operator reference too: http://www.cplusplus.com/ref/iostrea...ratorltlt.html

  10. #10
    *this
    Join Date
    Mar 2005
    Posts
    498
    When an integer is given characters, the program goes haywire because as you guessed wrong data type. The program will have logically the wrong output, if no imidiate error occured.

    I suggest waiting for strings once you can handle char's, int's, double's, etc... Try making a calculator for practice.

  11. #11
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Nope, the >> operator is overloaded for int What he did is fine. Again, JaWiB's code is perfect.
    I was responding to the original question, not to JaWib's code. JaWib was not trying to put character into type int, but into type string .

  12. #12
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Forget everything that I said, because I'm a fool. Enachs wins! (I didn't see he was trying to read a name into an int, how foolish!)

  13. #13
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69
    You could use this:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char name[10];
        
        cout<<"hello there, what is your name?\n";
        cin>>name;
        cout<<"welcome " << name << ", how are you today?\n";
        cin.ignore();
        cin.get();
    }
    good luck,
    Adam

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    JaWiB's version is preferred. In C++ use the C++ string class for strings unless you have specific, advanced reasons not to.

  15. #15
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69
    What if the reason is this?
    I suggest waiting for strings once you can handle char's, int's, double's, etc... Try making a calculator for practice.
    Besides, I use this way for all my code and I don't think that I have advanced specific reasons.
    Adam

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

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. problem with variable passed as reference
    By crash88 in forum C Programming
    Replies: 1
    Last Post: 05-16-2006, 07:16 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. problem with the const variable
    By ssharish in forum C Programming
    Replies: 2
    Last Post: 01-28-2005, 08:53 AM
  5. Peculiar Problem with char variable in C Language
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-31-2001, 03:06 PM

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