Thread: name variable

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    4

    name variable

    okay so im almost completely new to C++ my trouble is one of the first parts of it for storing variables. (feel free to correct me if i say anything that is incorrect this is just what i understand).

    there are different types of syntaxes for storing variables int is for a number without a decimal char is for a letter and theres another one but i dont understand it i think its called float.

    im trying to make my first simple program where it would give the "illusion" that your talking to me for example i'd ask what your name is then youd type it in and i'd give a response to it.

    i guess im not using the right syntax or something because when the user is entering their name it closes the program like when it's done loading everything.

    here is my code if someone could point out what to change:

    Code:
    #include <iostream>	
    
    using namespace std;
    		
    int main()                           
    {
      char name; 
      int age;
      
      cout<<"hi my name is daniel, whats yours?\n";    
      cin>> name;                          
      cin.ignore();                                         
      cout<<"pleasure to meet you "<< name <<"\n";
      cout<<"im 15, how old are you?\n";
      cin>> age;
      cin.ignore();
      cout<<" "<< age <<" years old? awesome!\n";
      cin.get();
      return 0;
    }
    originally i had:

    Code:
    char name;
    as:
    Code:
    int name;
    when i realized that int was a syntax for a number not a word so i tried looking for what the syntax for storing a word variable would be but couldnt find anything specificly saying use this one =p so then i guessed at char

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    use a string variable, ie:

    #include <string>
    string name; //initialize
    getline(cin, name); //get input

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    4

    thanks

    thanks it works really well now but i have 1 question that tags onto this one.

    when using a number if i want to discard the enter i do:

    Code:
    cin.ignore();
    what what do i do to discard the enter for a string variable?

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    cin.ignore() works with strings too. but personally i use
    cin.ignore(cin.rdbuf()->in_avail() + 1);

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The best method might be this (I've never seen your version before): http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    hmm... after a bit of searching, i found that this this post seems to imply that you can use either one, i wonder which one is the best? i'm very curious because i use the method numerous times in my programs.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    In my opinion (this is all based on guesswork, you might want to find another source), this
    Code:
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    is better than this
    Code:
    cin.ignore(std::cin.rdbuf()->in_avail() +1);
    Why?

    Two reasons. First of all, because the former uses the same value over and over again, the compiler would be able to optimise it a lot better. In fact, std::numeric_limits<std::streamsize>::max() might even be a compile-time constant.

    Secondly, since you're adding 1 to the latter, there's a very remote chance that you might cause a buffer overflow and have the value wrap around to zero when the buffer is at its absolute maximum size, causing you to call
    Code:
    cin.ignore(0);
    which wouldn't clear anything from the stream, which might cause problems later on in your program. (The former may suffer from this problem as well, but I find it less likely.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> what what do i do to discard the enter for a string variable?
    Always use ignore() to discard the newline character from the user hitting enter after you use cin >>. This is true if you are reading in strings or numbers.

    If you are using getline to read in strings, the newline from the enter is ignored automatically, and you should not use ignore().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM