Stupid Question

This is a discussion on Stupid Question within the C++ Programming forums, part of the General Programming Boards category; try this Code: #include <iostream> using namespace std; int main() { char name [30]; cout <<"Welcome, please enter your name: ...

  1. #16
    Registered User
    Join Date
    Jan 2003
    Posts
    361
    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();
    }

  2. #17
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    '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.

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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();
    }

  4. #19
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    or, you know, to resolve a headache, just put:

    Code:
    using namespace std;
    Right after your headers.

  5. #20
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    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;
    }

  6. #21
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    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)

  7. #22
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    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;
    }
    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.

  8. #23
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I use this because for some reason my compiler (Dec C++) doesnt like "cin.get()", so you may also be having the same problem.
    When a user hits return after entering input, an invisible newline character(\n) is entered as part of the input. For instance,

    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.

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

Similar Threads

  1. Stupid Question
    By Dave3of5 in forum C++ Programming
    Replies: 7
    Last Post: 05-24-2006, 03:52 PM
  2. Stupid Question Probably
    By Kyrin in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 12:51 AM
  3. Replies: 7
    Last Post: 11-03-2005, 11:17 PM
  4. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 07:22 PM
  5. Stupid question: What does Debugger do?
    By napkin111 in forum C++ Programming
    Replies: 6
    Last Post: 05-02-2002, 10:00 PM

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