A Very Basic Question... I think.

This is a discussion on A Very Basic Question... I think. within the C++ Programming forums, part of the General Programming Boards category; Ok, well hello everyone! I just created my account this morning, so you are honored with reading my first ever ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    2

    A Very Basic Question... I think.

    Ok, well hello everyone! I just created my account this morning, so you are honored with reading my first ever post on this forum! I bet you are all excited! My name is Colin, and I'm a poor speller, for that I apologize.

    I used to program back in High School, 6 or so years ago. Turing, and Visual Basic. For whatever reason, I got interested in trying to learn C++ a couple days ago, so I found the tutorial on cprogramming.com, grabbed Bloodshed dev-c++ 4 and I hit the ground running!

    Here is the program I'm working on right now, just to entertain me.

    Code:
    #include <iostream> 
    
    using namespace std;
    
    char usrName;       // stores the users name
    int usrAge;         // stores the users age
    
    int main()          // the main important bit
    {
      
      cout<<"Hello!  My name is CompColin!\n";                                       
      cout<<"I am a computer based personality designed by Colin Gilbert\n";
      cout<<"to entertain the really bored and/or lonely.\n";                        
      cout<<"Also I demonstrate my creatores superior intelect!\n\n";                
    
      cout<<"Obviously I will need to get to know you better in order to\n";
      cout<<"entertain you good and proper like I said I would!\n";                  
      cout<<"Please, tell me your name!\n";
    
      cin>> usrName;         // gets the users name and stores it
      cin.ignore();          // ignores there enter press
    
      cout<<"Ahh!  Well thats an alright name!  Hello "<< usrName <<" Its nice\n";
      cout<<"to meet you!\n";
    
     cin.get();
    }
    The program is still in its beginnings, but anyway. The problem is this. when I run the program it runs fine. All the dialog shows up and it asks for your name. you then enter your name and when you press enter, it flashes the text after but then promptly closes the program. I just don't understand why the line cin.get(); doesn't catch the program and make it wait. I was under the impression that, that is what it did.

    Anyway, I've looked into some of the FAQ's and what not and it seems like there are some ways around this, using different technics that I don't fully understand yet. but! the part I truely don't understand is that this program.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int thisisanumber;
    
      cout<<"Please enter a number: ";
      cin>> thisisanumber;
      cin.ignore();
      cout<<"You entered: "<< thisisanumber <<"\n";
      cin.get();
    }
    Is taken directly from the tutorial on cprogramming.com and it works just fine when I compile it. And to be honest with you guys, it looks pretty much exactly the same as the one I wrote.

    So why does cin.get(); catch the tutorials program, but not my program?

    Thanks a lot for any help, and I'm sorry if its a really obvious answer, but I've been looking at it for a few hours now, and its not jumping out at me.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    char usrName;       // stores the users name
    // ...
      cin>> usrName;         // gets the users name and stores it
      cin.ignore();          // ignores there enter press
    You're reading in a single character, and then ignoring a single character. Type "C<enter>" and it should work.

    If you want to fix it, make usrName an array (and possibly put it in main(), too):
    Code:
    char usrName[100];       // stores the users name (put at the beginning of main())
    // ...
      cin>> usrName;         // gets the users name and stores it
      cin.ignore();          // ignores there enter press
      cout << "Hello, " << usrName << ". Good day.\n";  // print the user's name
    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.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    As you are just learning might as well start off with good habits and use the std::string type. #include<string> and change char usrName to string usrName.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    2
    Interesting!
    I missunderstood the use of 'char' then. The totorials I've read so far have only delt with variables that were integers. I assumed that 'char' worked the same as other programs I've used. It makes good sence now.

    Thanks for your help dwks

  5. #5
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    #include <iostream> 
    
    using namespace std;
    
    char usrName[81];       // stores the users name
    int usrAge;            // stores the users age
    
    int main()          // the main important bit
    {
      
      cout<<"Hello!  My name is CompColin!\n";                                       
      cout<<"I am a computer based personality designed by Colin Gilbert\n";
      cout<<"to entertain the really bored and/or lonely.\n";                        
      cout<<"Also I demonstrate my creatores superior intelect!\n\n";                
    
      cout<<"Obviously I will need to get to know you better in order to\n";
      cout<<"entertain you good and proper like I said I would!\n";                  
      cout<<"Please, tell me your name!\n";
    
      cin>> usrName;         // gets the users name and stores it
      //cin.ignore();           ignores there enter press
    
      cout<<"Ahh!  Well thats an alright name!  Hello "<< usrName <<" Its nice\n";
      cout<<"to meet you!\n";
    
      cin.get();
      cin.get();
     
    }

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    As stoned_coder said, learn the C++ string class for strings:
    Code:
     #include <iostream> 
    #include <string>
    
    using namespace std;
    
    string usrName;       // stores the users name
    int usrAge;         // stores the users age
    
    int main()          // the main important bit
    {
      cout<<"Hello!  My name is CompColin!\n";                                       
      cout<<"I am a computer based personality designed by Colin Gilbert\n";
      cout<<"to entertain the really bored and/or lonely.\n";                        
      cout<<"Also I demonstrate my creatores superior intelect!\n\n";                
    
      cout<<"Obviously I will need to get to know you better in order to\n";
      cout<<"entertain you good and proper like I said I would!\n";                  
      cout<<"Please, tell me your name!\n";
    
      cin>> usrName;         // gets the users name and stores it
      cin.ignore();          // ignores there enter press
    
      cout<<"Ahh!  Well thats an alright name!  Hello "<< usrName <<" Its nice\n";
      cout<<"to meet you!\n";
    
      cin.get();
    }

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    And you should put a return 0; at the end of main(), although it isn't required.
    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
    Sep 2004
    Posts
    36
    I prefer this.

    Code:
    #include <iostream> 
    #include<string.h>   //not needed if using DEV C++ IDE
    using namespace std;
    
    string usrName;       // stores the users name
    
    int main()          // the main important bit
    {
      
      cout<<"Hello!  My name is CompColin!\n";                                       
      cout<<"I am a computer based personality designed by Colin Gilbert\n";
      cout<<"to entertain the really bored and/or lonely.\n";                        
      cout<<"Also I demonstrate my creatores superior intelect!\n\n";                
    
      cout<<"Obviously I will need to get to know you better in order to\n";
      cout<<"entertain you good and proper like I said I would!\n";                  
      cout<<"Please, tell me your name!\n";
    
      cin>> usrName;         // gets the users name and stores it
    //The ignore is not necessary. it is used often only after a getline()
    
      cout<<"Ahh!  Well thats an alright name!  Hello "<< usrName <<" Its nice\n";
      cout<<"to meet you!\n";
    
      cin.get();
      //cin.get(); Only one is needed. otherwise, it is a pain for the user
    
    return 0;
    }

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> I prefer this.
    Your version has mistakes.

    It should be <string>, not <string.h> if you are using the string class, and it is needed no matter what your IDE, since you should always include the headers that declare what you're using. Don't count on getting lucky with the includes.

    The cin.ignore() is required before a call to getline() if operator>> was called before it. It is perfectly safe to just always call ignore() after using cin >>, and you should not use it after getline() unless you want the user to hit enter twice.

    In this case, to keep the console from closing you either need to leave the ignore(), or you need two calls to cin.get(). You have neither, so the lone cin.get() will get the newline from after the user enters the name and the console will close before the output can be read (for those who have the console closing problem).

    I'd also prefer to make the usrName string local to the main function and declare it just before it was used, but I didn't change that since the point was that the string class is better than C style strings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic math programming question
    By hebali in forum C Programming
    Replies: 38
    Last Post: 02-25-2008, 03:18 PM
  2. Basic question about GSL ODE func RK4
    By cosmich in forum Game Programming
    Replies: 1
    Last Post: 05-07-2007, 02:27 AM
  3. Basic question about RK4
    By cosmich in forum C++ Programming
    Replies: 0
    Last Post: 05-07-2007, 02:24 AM
  4. A very basic question
    By AshFooYoung in forum C Programming
    Replies: 8
    Last Post: 10-07-2001, 03:37 PM

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