-
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:
as:
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
-
use a string variable, ie:
#include <string>
string name; //initialize
getline(cin, name); //get input
-
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:
what what do i do to discard the enter for a string variable?
-
cin.ignore() works with strings too. but personally i use
cin.ignore(cin.rdbuf()->in_avail() + 1);
-
The best method might be this (I've never seen your version before): http://faq.cprogramming.com/cgi-bin/...&id=1043284392
-
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.
-
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
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.)
-
>> 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().