cin question

This is a discussion on cin question within the C++ Programming forums, part of the General Programming Boards category; I am using the book "C++ programming 101." The question is: Write a number guessing game. At the beginging of ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    27

    Question cin question

    I am using the book "C++ programming 101."
    The question is:
    Write a number guessing game. At the beginging of the program, assign a value to a variable called number. Give a prompt that asks for five guesses. Recieve the user's five guesses with a single cin so that you can practice with cin. Determine whether any of the guesses matches the number, and print an appropriate message if one does.

    SO this is what I did: The problem is after the user types 5 numbers, nothing happens...

    Thank you for all your help

    Code:
    #include <iostream.h>
    main()
    {
    int my_num = 3;
    int num[5];
    
       cout << "I chose a number\n";
       cout << "I want you to try to guess it\n";
       cout << "you have five chances\n";
       cout << "please type 5 numbers, no spaces please.  Press Enter when you are done\n";
    
       cin >> num[0]>> num[1] >> num[2] >> num[3] >> num[4];
    
    if ((num[0]== my_num) | (num[1]== my_num) |(num[2]== my_num) |(num[3]== my_num) |(num[4]== my_num))
    {
       cout << "You guessed right!" << "The number is " << my_num << "\n";
    }
    else
    {cout << "You didn't guess right!";}
    
    return 0;}

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    cout<< "please type 5 numbers, no spaces please.  Press Enter when you are done\n";
    
    cin >> num[0]>> num[1] >> num[2] >> num[3] >> num[4];
    When you use >> to read input, the input has to be separated by spaces. The operator>> stops reading input when it comes to a space, tab, or newline. If you don't separate the expected 5 pieces of input by spaces, the operator>> reads all the input into num[0] and waits for you to enter the other 4 numbers it is expecting.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    27
    Thank you so much for your answer... so how can I change it and still use one cin?
    would this work? :
    Code:
    cin >> num[0]>> " " >>num[1] >> " ">>num[2] >> " ">>num[3] >> " ">>num[4];
    (I tried to add spaces)

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Your cin>> statement is fine. You just need to enter the input with spaces between the numbers.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    27
    It works now! So when using cin, the user has to type spaces, or else it won't work... ?
    Thank you so much, can I ask you questions when I run into a problem? great forum!

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You have some other problems:

    1) No standard header file name, e.g.:

    #include <iostream.h>

    should end in ".h".

    The correct statement is:

    #include <iostream>

    It sounds like your book may be old and out of date.

    2) Don't worry about the meaning of this now, but you need to always include this line at the top of your file:

    using namespace std;

    So, at the top of all your programs, always put these two lines:

    #include <iostream>
    using namespace std;

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    It works now! So when using cin, the user has to type spaces, or else it won't work... ?
    It's actually this thing: >> that requires the input have spaces between it. Otherwise, it doesn't know when the input ends. If you think about it a little bit, what should the computer do if the user enters:

    156

    Is that supposed to be three guesses of 1, 5, 6; or is that two guesses of 15 and 6 or 1 and 56; or is that one guess of 156. The computer doesn't know. The computer needs some signal to tell it when the digits of the guess end. So, if you do this:

    1 65 5 133

    the >> thing uses the spaces to determine when the digits representing a guess end. It interprets that input to mean the guesses are 1, 65, 5, and 133.
    Last edited by 7stud; 06-09-2005 at 05:14 PM.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    27
    I see... I get it. Thank you so much!

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    27
    I have one more question... last one for now.
    Write a program (by using a single compund if statement) to determine whether the user enters an odd positive number.

    Here is what I did :
    Code:
    #include <iostream.h>
    main()
    {
    int a;
    while(1)
    {
    cout << "please enter a number\n";
    cin >>(int)a;
    
    if((a>0)&&(a%2))
    {
    	cout << "You have entered an odd positive number\n";}
    }
    return 0;
    
    }
    The problem is that when the user enters a float then it crashes...

  10. #10
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    #include <iostream.h>
    main()

    It seems you haven't read 7stud's post. You are using old style headers. You should change to:


    Code:
    #include <iostream>
    
    using namespace std;

    as indeed already mentioned.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple cin question
    By verbity in forum C++ Programming
    Replies: 25
    Last Post: 05-09-2007, 03:02 PM
  2. CIN Input count question?
    By kamran in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2006, 04:06 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  4. cin Help...Sort of a noob Question?
    By Krak in forum C++ Programming
    Replies: 9
    Last Post: 01-26-2003, 12:23 PM
  5. cin question
    By joebudden in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 06:38 PM

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