cin.get() aint working.

This is a discussion on cin.get() aint working. within the C++ Programming forums, part of the General Programming Boards category; I have no idea what im doing wrong, cin.get() used to stop my programs but for somereason they nolonger do. ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    16

    Exclamation cin.get() aint working.

    I have no idea what im doing wrong, cin.get() used to stop my programs but for somereason they nolonger do. Here is an example of when the cin.get() hasnt been stopping my code :

    #include <iostream>

    using namespace std;

    int main() {
    int x;
    int y;
    int box[2][2];
    cout<<"Please enter a number between 1 and 2\n";
    cin>>x;
    cout<<"Please enter another number between 1 and 2\n";
    cin>>y;
    box[0][0] = x*y;
    box[0][1] = x/y;
    box[1][1] = x+y;
    cout<<box[0][0]<<" "<<box[0][1]<<" "<<box[1][1];
    cin.get();
    }

    i know its simple but after it runs it just quits by its self.
    Last edited by Blips; 01-08-2005 at 10:28 AM.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    try adding return 0; after cin.get( ) and see if that works.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    i added return 0; afterwards and it still closes right away. Its wierd that it used to work....
    im using microsoft visual studio .net as my compiler if it matters.
    Last edited by Blips; 01-08-2005 at 10:38 AM.

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    try flushing your buffer with an << endl; before using cin.get( )
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    still doesnt work...
    so now the code looks like this:

    #include <iostream>

    using namespace std;

    int main() {
    int x;
    int y;
    int box[2][2];
    cout<<"Please enter a number between 1 and 2\n";
    cin>>x;
    cout<<"Please enter another number between 1 and 2\n";
    cin>>y;
    box[0][0] = x*y;
    box[0][1] = x/y;
    box[1][1] = x+y;
    cout<<box[0][0]<<" "<<box[0][1]<<" "<<box[1][1]<<endl;
    cin.get();
    return 0;
    }


    and what is wierd is that the cin.get() in this code works fine :

    #include <iostream>

    using namespace std;

    int main() {
    cout<<"hello";
    cin.get();
    }

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    oh ok.

  9. #9
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    box[0][1] = x/y; might cause a problem when y = 0.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    doesnt work:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
    int x;
    int y;
    int box[2][2];
    cout<<"Please enter a number between 1 and 2\n";
    cin>>x;
    cout<<"Please enter another number between 1 and 2\n";
    cin>>y;
    box[0][0] = x*y;
    box[0][1] = x/y;
    box[1][1] = x+y;
    cout<<box[0][0]<<" "<<box[0][1]<<" "<<box[1][1]<<endl;
    cin.get();
    return 0;
    }
    does work:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
    cout<<"hello";
    cin.get();
    }

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    it doesnt matter if i set y to 0 or not , the same thing keeps happening.....

  12. #12
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209
    Ok ill be happy with this for now , but next time try to indent code, its much easier to read.

    Now on to your problem; the problem is that cin leaves the trailing newline character in the buffer. This means that the program basicly skips the cin.get call because cin.get() gets the newline already in the buffer from previous calls to cin >> [whatever variable you have here]. to fix this add this code after every cin >> [whatever]; : cin.ignore();
    Last edited by Shakti; 01-08-2005 at 10:52 AM.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    Ah, thank you that did it. I had forgoten all about the cin.ignore() function

    now back to the tutorials.
    Last edited by Blips; 01-08-2005 at 10:56 AM.

  14. #14
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    The problem here is cin.ignore() with no arguments simply ignores the next character in the input stream. While usually the only item left is the newline from the previous input, sometimes there can be more than one character in the input stream. Usually when cin is used to input a variable, but the user input was delimited with whitespaces and extra chars.

    Using while ( cin.get() != '\n' ); will flush all characters up to the newline out of the input buffer.
    Last edited by Scribbler; 01-08-2005 at 12:08 PM.

  15. #15
    Self-Taught Noob
    Join Date
    Jan 2005
    Location
    Ohio
    Posts
    38
    im not sure the header file but where your cin.get() is try a getch().

    it will pause the screen until a user hits a button.
    No one died when Clinton lied.

    Compiler: Borland C++ Builder
    OS: Windows XP

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

Similar Threads

  1. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 08:47 PM
  2. cin.get(); doesn't seem to be working as expected
    By Diablo84 in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2005, 07:00 PM
  3. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  4. Problems using while loop with cin.get()
    By Arooj in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2004, 01:58 AM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 05:08 AM

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