User-input not working

This is a discussion on User-input not working within the C++ Programming forums, part of the General Programming Boards category; I'm trying to do the tutorials, but I can't get past the first part because my input variables aren't working. ...

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

    User-input not working

    I'm trying to do the tutorials, but I can't get past the first part because my input variables aren't working. Here's my code:

    Code:
    #include <iostream.h> 
    int
    main() 
    { 
      int thisisanumber; 
      cout<<"Please enter a number:";
      cin>>thisisanumber; 
      cout<<"You entered: "<<thisisanumber;
      return 0; 
    }
    I've looked at it a thousand times... it's character for character with what's on the website. But when I run the program, it prompts for a number, I input one, press enter, and it closes. What am I doing wrong?
    Last edited by Serejai; 08-22-2004 at 04:07 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    2

    ok

    Quote Originally Posted by Serejai
    I'm trying to do the tutorials, but I can't get past the first part because my input variables aren't working. Here's my code:

    Code:
    #include <iostream.h> 
    int
    main() 
    { 
      int thisisanumber; 
      cout<<"Please enter a number:";
      cin>>thisisanumber; 
      cout<<"You entered: "<<thisisanumber;
      return 0; 
    }
    I've looked at it a thousand times... it's character for character with what's on the website. But when I run the program, it prompts for a number, I input one, press enter, and it closes. What am I doing wrong?

    try...

    Code:
    #include <iostream.h> 
    #include <stdio.h>
      int thisisanumber;
      main()
      {
      cout<<"Please enter a number:";
      cin>>thisisanumber; 
      cout<<"You entered: "<<thisisanumber;
      getchar();
      return 0; 
      }
    That worked for me.. (Using Dev C++)

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    2
    It closes because there's nothing telling it to stop after it finishes the last cout line. Simply place a system("PAUSE"); right before return 0; and after cout<<"You entered "<<thisisanumber;.

    So it would be:
    Code:
    #include <iostream>
    int main ()
    {
        int thisisanumber;
        cout<<"Enter a number:";
        cin>>thisisanumber;
        cout<<"You entered: "<<thisisanumber;
        system("PAUSE");
        return 0;
    }
    You could also place a few endl's after the cout's to make it look more organized. For example:
    Code:
    cout<<"Enter a number:"<<endl;
    So instead of running the program to show:
    Enter a number: _
    (the underscore is the little blinky thingamajig*

    It would look like this with endl:
    Enter a number:
    _
    Last edited by Chrisso; 08-22-2004 at 05:14 PM.

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    system("PAUSE") has several drawbacks, and does not offer any advantage over the other solutions.

    Since this is the C++ forums, we'll be assuming Serejai is using C++ He should use this method, as suggested by the FAQ.
    Code:
    #include <iostream> 
     
    int main(void)
    {
    std::cout <<"Press [Enter] to continue" <<std::endl;
    std::cin.get();
    return(0);
    }
    Apoc's method also works, but it is more for the C audience.

    edit: And to add my personal opinion, it is better to have the cursor immediately in front of the prompt. That's the way most console apps do it.
    Last edited by sean; 08-22-2004 at 05:26 PM.

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    2
    Ty ^_^:; Sorry about not searching FAQs and such. I don't like searching for "related" topics, because my exact problem/experience probably isn't the same as somebody elses. Appreciate all the input.

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I don't like searching for "related" topics, because my exact problem/experience probably isn't the same as somebody elses.
    This couldn't be further from the truth. Every time a beginner comes to these forums, they ask the exact same questions. I have seen this particular question maybe 30-40 times. Always search the forums first.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to limit user input to a certain number of digits?
    By NewbGuy in forum C Programming
    Replies: 7
    Last Post: 05-08-2009, 09:57 PM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  3. newbie question regarding user input
    By cantore in forum C Programming
    Replies: 4
    Last Post: 03-05-2006, 07:57 PM
  4. comparing user input
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 12-15-2002, 09:28 AM

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