try this
Code:#include <iostream> using namespace std; int main() { char name [30]; cout <<"Welcome, please enter your name: "; cin.getline(name,30,'\n'); cout <<"Hello " << name << ", how are things going"; cin.get(); }
try this
Code:#include <iostream> using namespace std; int main() { char name [30]; cout <<"Welcome, please enter your name: "; cin.getline(name,30,'\n'); cout <<"Hello " << name << ", how are things going"; cin.get(); }
'endl' is in the std namespace as well... that is, use "std::endl" instead or put in (a) "using" declaration(s).
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
Oops, sorry about that.
Code:#include <cstdlib> #include <iostream> int main() { char name [30]; std::cout <<"Welcome, please enter your name: "; std::cin.get(name,30,'\n'); std::cout <<"Hello " << name << ", how are things going" << std::endl; std::cin.get(); }
or, you know, to resolve a headache, just put:
Right after your headers.Code:using namespace std;
Code:#include <cstdlib> #include <iostream> using namespace std; int main() { char name [30]; std::cout <<"Welcome, please enter your name: "; std::cin.get(name,30,'\n'); std::cout <<"Hello " << name << ", how are things going" <<endl; int k; cin>>k; }
Thanks the first code on this page worked fine, thanks
Verbal Irony >>
"I love english homework!" When really nobody like english homework.
-Mrs. Jennifer Lenz (English Teacher)
Try that, the "Using namespace" declarations means that you dont need to put "std::" before anything used from the standard library, which also includes the "endl" function. Also you didnt need the <cstdlib> library, and finally try using "cin.ignore(2)". I use this because for some reason my compiler (Dec C++) doesnt like "cin.get()", so you may also be having the same problem.Code:#include <iostream> using namespace std; int main() { char name [30]; cout <<"Welcome, please enter your name: "; cin.get(name,30,'\n'); cout <<"Hello " << name << ", how are things going" << endl; cin.ignore(2); return 0; }
When a user hits return after entering input, an invisible newline character(\n) is entered as part of the input. For instance,I use this because for some reason my compiler (Dec C++) doesnt like "cin.get()", so you may also be having the same problem.
Welshy\n
When you apply cin>> to that input, cin>> is programmed to read input until it encounters a trailing whitespace character, which can be a space, tab, or newline(\n). In addition, cin>> is programmed to leave the \n in the input. So, after you use cin>> on that input, the input looks like this:
\n
You should also note that input is not tied to any specific line of code. Input forms a que which is available to any line in your program. That means as input from the user comes in, it is stored one word behind the other. Then, after one line of your program reads from the input, any subsequent lines in your program will read from where the previous read left off.
For instance, just because you didn't read in the \n character that was entered after your name, doesn't mean it goes away. Instead, it becomes the first thing encountered by any subsequent read. Furthermore, any additional user input is put in line behind the \n.
So, if you have cin.get() as the last line of your program--in an attempt to hold the console window open--cin.get() takes a look at the remaining input:
\n
Since, cin.get() is defined to read a single character from the input, no matter what the character, cin.get() does not have to wait for the user to enter any more input--there is already some input it can happily read. So, cin.get() reads in the \n that is next in line in the input, which finishes its operations, and therefore your program ends and the console window closes immediately.
Did you ever wonder why you had to use cin.ignore(2) and not cin.ignore(1)? It's because cin.ignore() operates on the same input as cin.get(), namely:
\n
cin.ignore(1) can ignore the one character left in the input and finish its operation, which means once again the console window closes immediately. On the other hand, cin.ignore(2) cannot finish its operation because it can't find a second character to ignore(). Therefore, cin.ignore(2) has to wait for the user to type in a character, which means the console window will stay open.
Last edited by 7stud; 05-17-2005 at 12:11 PM.