User Input

This is a discussion on User Input within the C++ Programming forums, part of the General Programming Boards category; Say this is my code: Code: #include <iostream.h> int main() { int x; cout<<"Please enter a number: "; cin>>x; cout<<"You ...

  1. #1
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269

    Question User Input

    Say this is my code:

    Code:
    #include <iostream.h>
    int main()
    {
      int x;
      cout<<"Please enter a number: ";
      cin>>x;
      cout<<"You entered: "<<x;
      return 0;
    }
    When it says Please enter a number, how do I enter it? Say I put in a 1, then what do I press? If I press enter, the program will close... How do I fix it? Do I press something else?
    I'm compiling with Dev-C++...

    Needing Help,
    SirCrono6
    Last edited by SirCrono6; 11-22-2003 at 10:48 PM.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    while(cin.get() != '\n'); is a cheap way for pausing console apps. Keep in mind that the newline char may be present from your last call to cin, so you can also use cin.ignore(1); after cin >> x;
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  4. #4
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Thanks for the help, but it didn't work. Although, I did find a code that did work:

    Code:
    #include <iostream>
    using namespace std;
    int main()
    { 
      char quit;  
      int x;
      while (quit!='q')
      {
        cout<<"Please enter a number: "<<endl;
        cin>>x;
        cout<<"You entered: "<<x<<endl;
        cout<<"Press q to quit "<<endl;
        cin>>quit;
      }
    
      return 0;
    }
    So, that works...

  5. #5
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Originally posted by ronin
    while(cin.get() != '\n'); is a cheap way for pausing console apps. Keep in mind that the newline char may be present from your last call to cin, so you can also use cin.ignore(1); after cin >> x;
    Canīt I predict when <b>get</b> will not remove the read character?
    Nothing more to tell about me...
    Happy day =)

  6. #6
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Originally posted by SirCrono6
    Thanks for the help, but it didn't work. Although, I did find a code that did work:

    Hmm... works for me using dev-c.

    Code:
    #include <iostream>
    using namespace std;
    int main()
    { 
      char quit;  
      int x;
      while (quit!='q')
      {
        cout<<"Please enter a number: "<<endl;
        cin>>x;
        cout<<"You entered: "<<x<<endl;
        cout<<"Press q to quit "<<endl;
        cin>>quit;
      }
    
      return 0;
    }
    So, that works...
    It's always a sense of accomplishment when you can find your own solution. Good work!
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    8
    You can add system("pause"); to your code before return 0;. This will pause console programs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  2. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  3. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM
  4. Getting user input for filename loop
    By jpchand in forum C++ Programming
    Replies: 1
    Last Post: 09-16-2003, 07:37 AM
  5. comparing user input
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 12-15-2002, 10:28 AM

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