Thread: Magneto's journey to becoming a C++ expert.

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    11

    Magneto's journey to becoming a C++ expert.

    OK, maybe not an expert but I'm going to give it a good try. Right the purpose of this thread is to consolidate all of my little problems/questions of which don't need a new thread each time.

    #1
    The problem - The program closes before I have time to see it in action.

    The code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char Matt;
        cout << "Please enter something for me to say: ";
        cin >> Matt;
        cout << "\n" << "Matt says: ";
        cin.get();
        cout << Matt;
        return 0;
    }
    So far I know cin.get() can be used to stop a program and it requires the enter, to allow it to carry on. I've used cin.get() in a simpler program (Just a text output) and it worked great. I've tried moving it around in the code, but nothing seems to work.

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    im not a cin.get() fan but i think you have to clear the stream:
    Code:
    cin.get();
    cin.ignore();
    or this:
    Code:
    //replace those two with this:
    system("PAUSE");

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    11
    Your first suggestion doesn't seem to chage the program at all.

    Your second suggestion works to an extent; I run the program and type something then hit enter. It then displays the first letter of the "Matt" variable only, followed by "Press any key to continue. . .". I've tryed ending the
    Code:
    cout << Matt;
    Line >
    Code:
    cout << Matt << endl;
    Still no luck.

  4. #4
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    because its a char, chars only store 1 letter..

    use
    Code:
    #include <iostream>
    #include <string>   // needed for string
    using namespace std;
    
    int main()
    {
       string st; //string can store a lot more than just a single letter
       cout << "INPUT: ";
      getline(cin, st);
       cout << st << endl;
       system("PAUSE");
       return 0;
    }
    Last edited by mrafcho001; 07-16-2005 at 03:01 PM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Quote Originally Posted by mrafcho001
    im not a cin.get() fan but i think you have to clear the stream:
    Code:
    cin.get();
    cin.ignore();
    Those are backward. You clear the newline from the stream first, then use cin.get() to do a blocking read. That's assuming, of course, that the next character on the stream is a newline.
    Quote Originally Posted by mrafcho001
    or this:
    Code:
    //replace those two with this:
    system("PAUSE");
    This is not a better option. cin.get() is the best portable option as long as you make sure to clear the stream state and discard any extraneous characters:
    Code:
    #include <iostream>
    #include <limits>
    #include <string>
    
    using namespace std;
    
    void clear_stream ( istream& in )
    {
      in.clear();
      in.ignore ( numeric_limits<streamsize>::max(), '\n' );
    }
    
    int main()
    {
      string Matt;
    
      cout << "Please enter something for me to say: ";
      getline ( cin, Matt );
      cout << "\n" << "Matt says: ";
      cout << Matt;
    
      // Clear first, then read
      clear_stream ( cin );
      cin.get();
    
      return 0;
    }
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Since Matt is a char variable cin>>Matt reads a single character from the stream.
    When you use cin.get() to make your program wait before closing it if you have entered more than one character it will find stuff already waiting for it in the stream and don't block. system("PAUSE") will block correctly but since only 1 letter has been read it will only print a single letter.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string Matt;
        cout << "Please enter something for me to say: ";
        cin >> Matt;
        cout << "\n" << "Matt says: ";
        //cin.get(); // what's this for? 
        cout << Matt;
        cin.ignore();
        cin.get();
        return 0;
    }
    The above should work fine.
    Notice the type change for Matt from char to string and the cin.ignore(); cin.get(); to make the program block after showing the input

    [edit] bah beaten to it by 2 people

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    11
    Thanks everyone, you've been a great help. Isn't learning fun?

  8. #8
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    Quote Originally Posted by Magneto
    OK, maybe not an expert but I'm going to give it a good try. Right the purpose of this thread is to consolidate all of my little problems/questions of which don't need a new thread each time.

    #1
    The problem - The program closes before I have time to see it in action.

    The code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char Matt;
        cout << "Please enter something for me to say: ";
        cin >> Matt;
        cout << "\n" << "Matt says: ";
        cin.get();
        cout << Matt;
        return 0;
    }
    So far I know cin.get() can be used to stop a program and it requires the enter, to allow it to carry on. I've used cin.get() in a simpler program (Just a text output) and it worked great. I've tried moving it around in the code, but nothing seems to work.

    -"Matt" is a char variable, meaning it will only hold one letter. Change it into an array, or a string.

    -Try not to use namespace std. It's a bad habit to form. use only things you will use. In your case that would be

    Code:
    using std::cin;
    using std::cout;
    -If you're on win32, just run your programs in command prompt, saves you the hassle of accidentaly putting cin.get() in the wrong place

  9. #9
    *this
    Join Date
    Mar 2005
    Posts
    498
    Try not to use namespace std. It's a bad habit to form. use only things you will use. In your case that would be
    It's not a bad habit, it's preference.

  10. #10
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by JoshR
    It's not a bad habit, it's preference.
    The only problem is that when teaching C++ most books/tutorials dont give you the information on why to use std::cin, or using namespace std, or using std::cin. Of course thats for good reason, being a little much for the first 10 pages of a book. Which means that most newbies dont prefer either, because they dont know which mean what, hence.. not a preference but a habit at the most.

    I'd have to say it doesnt matter if you use 'using namespace std' since if you cant learn to change your habit to use 'std::cin/std::cout' later, then you're going to have problems learning anything in C++. Also.. its doubtful any occasion where you are going to have problems of duplicate cin/cout/etc., but still possible.. only if you make it possible though, so not a big deal.. at all.

    Though the point is not that its a big deal, but that it is a deal.. its better to use std::cout to limit possibilities of problems, and thats what a lot of C++ is, eliminating problems, so its more correct to use std::cin in my eyes. Though doubtful it matters either way, in that sense.

    In the other sense it is bad habit to use 'using namespace' becuase when making your own namespaces you will want to use name::name in order to know where your names are coming from, that are in that file. Thats preference, but I would also consider it bad habit not using "name::name" since its better to know where things are coming from in complex programs.
    Last edited by Dae; 07-16-2005 at 07:35 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  11. #11
    Banned
    Join Date
    Jun 2005
    Posts
    594
    I agree, i dont think its bad habit or bad form

    Just out fo curiosity Dae when did oyu start preferring it,
    because in most of your post you dont use std:: ?
    Last edited by ILoveVectors; 07-16-2005 at 07:36 PM.

  12. #12
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    i was always told never to use
    Code:
    system("PAUSE");
    stick with
    Code:
    cin.ignore();
    cin.get();

  13. #13
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Quote Originally Posted by digdug4life
    i was always told never to use
    Code:
    system("PAUSE");
    stick with
    Code:
    cin.ignore();
    cin.get();
    I didn't learn about the 2nd way until i was really used to system("PAUSE"); and well i dont really see whats wrong with it..

  14. #14
    Banned
    Join Date
    Jun 2005
    Posts
    594
    the program pause could be replaced with a malicious program,

    anyways i had always used getch();

    but i learned of cin.get() so i use that all the time now.

  15. #15
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by ILoveVectors
    I agree, i dont think its bad habit or bad form

    Just out fo curiosity Dae when did oyu start preferring it,
    because in most of your post you dont use std:: ?
    I use it in my projects, where I actually think about my code and layout. Just like how I use Dev-Cpp for tests, and MSVC6 for projects. I use 'using namespace std' in any other code because its either just a test, or helping, which I dont really want to spend much time on, or explain std::cout if the person happens to have not gotten to the namespace topic yet and confuse them. See... 'using namespace' to 'name::' to 'using name::name', so I dont really consider it that hard to change habits so its really not something to worry about.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Performance Expert - New York, NY
    By txaggie94 in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-28-2007, 07:33 PM
  2. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 03:41 AM
  3. Expert system?
    By FloatingPoint in forum Tech Board
    Replies: 1
    Last Post: 06-14-2003, 07:23 AM
  4. c++ expert
    By febrian81 in forum C++ Programming
    Replies: 2
    Last Post: 07-01-2002, 10:27 PM
  5. Replies: 1
    Last Post: 09-30-2001, 07:45 AM