one more cin question

This is a discussion on one more cin question within the C++ Programming forums, part of the General Programming Boards category; I have one more question... last one for now. Write a program (by using a single compund if statement) to ...

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

    one more cin question

    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...

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1) This is incorrect:
    Code:
    #include <iostream.h>
    Instead, put this at the top of every program:
    Code:
    #include<iostream>
    using namespace std;
    2) The format for your main() function should be like this:
    Code:
    int main()
    {
    
    
    	return 0;
    }
    2) In this line:

    cin >>(int)a;

    (int) is a "cast", but that is C syntax for a cast. If you are learning C++, you should use the C++ syntax for a cast. In your case, that would be:

    static_cast<int>(a);

    3) You need to work on the format of your parentheses and your indenting, so you can easily tell when a block of code begins and ends. For instance, it is not apparent where your while loop ends. Do this instead:
    Code:
    #include<iostream>
    using namespace std;
    
    int 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;
    }
    4)Note that if your program works, you will get caught in an infinite loop. while(1) is always true, so the program will loop indefinitely.

    5) Always "initialize" your variables. That means when you declare a variable assign it a value, e.g.

    int a = 0;

    6) You can solve your problem by reading the input into a variable of type float. Then, cast it to an int and assign the result to an integer variable.
    Last edited by 7stud; 06-09-2005 at 05:49 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    You can also check the return value of cin:
    Code:
    if (!(cin >> a)) {
      cout << "Bad Input... terminating";
      break;
    }

  4. #4
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    try this
    Code:
    #include<iostream>
    #include <limits>
    using namespace std;
    
    int 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";
    		}
    		/*
    		 * The stream will go into an error state if it is expecting an integer
    		 * but it only sees characters for example. To move on, you must first clear
    		 * the error flags by using the clear member function. Then you must get rid of
    		 * the bad data in the input stream by using the ignore member function. 
    		 * After that, you can attempt to get more input.
    		 *
    		 */
    		cin.clear();
    		// include <limits> and then use numeric_limits<streamsize>::max()
    		cin.ignore(numeric_limits<streamsize>::max(), '\n');
    	}
    	
    	return 0;
    }

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

    Re:

    1. about the cast: for some reason, my book used this (int)a;for c++. Thank you for your advice.
    2. The loop was just for me, so I could keep trying numbers without having to keep re-running the solution.
    3. How would I do what you suggested? "You can solve your problem by reading the input into a variable of type float. Then, cast it to an int and assign the result to an integer variable."
    Why didn't mine work?



    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	int a=0;
    	
    	while(1)
    	{
    		cout << "please enter a number\n";
    		cin >>static_cast<int>(a);
    
    		if((a>0)&&(a%2))
    		{
    			cout << "You have entered an odd positive number\n";
    		}
    	}
    	
    	return 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    27
    Hey Antigloss,
    I didn't get so far with programming.. Could you explain the function you used?

    Thank you

  7. #7
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    27
    Antigloss,
    I tried your code and it works beautifully... how can I do what 7stud suggested? seems more simple

  9. #9
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    I think my solution is better. If you use 7stud's, but input chars to the program, it crashes.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    27
    Here is what I did, and now it accepts floats. But like you said, no char. Is there a thing as a general variable that accepts all data...?

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    int a=0;
    float b=0.0;
    while(1)
    	{
    		cout << "please enter a number\n";
    		cin >>b;
    
    		a=(int)b;
    
    		if((a>0)&&(a%2))
    		{
    			cout << "You have entered an odd positive number\n";
    		}
    	}
    return 0;
    
    }

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    There you go. You read in the input first, then cast it. Your book is old and out of date. Use a C++ cast instead:

    a = static_cast<int>(b);

    Is there a thing as a general variable that accepts all data...?
    Yes, it's called a string variable--you can read any data into a string variable. To create a string variable, you have to include the <string> header file, and you declare a string variable like this:


    string myStr;

    You can use cin>> to read data into a string variable. However, to try and extract a number from the input contained in the string variable, you have to do some more complex things.

    After reading data into the string variable, you can use the string variable to create what's called a stringstream. An input stringstream is like a string variable, but you can use the operator>> on the input stringstream to try and read data from it. Here is an example:
    Code:
    #include<iostream>
    #include<string>  //for string variables
    #include<sstream> //for stringstream variables
    
    using namespace std;
    
    int main()
    {
    	string input = "12.3abc";  //create string variable
    	istringstream inStr(input);  //use the string variable to create an input stringstream
    	
    	int a = 0;
    	inStr>>a;  //instead of reading data from cin--which represents input from 
    		//the keyboard--you read in data from inStr.
    		            
    	if(a != 0)
    		cout<<a<<endl;
    	else
    		cout<<"bad input"<<endl;
    	
    	
    
    	string input2 = "abc2.4";
    	istringstream inStr2(input2);
    	
    	a = 0;
    
    	inStr2>>a;
    	if(a != 0)
    		cout<<a<<endl;
    	else
    		cout<<"bad input"<<endl;
    
    	return 0;
    }
    Last edited by 7stud; 06-09-2005 at 10:24 PM.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    27
    I'm a little bit overwhelmed. I can't believe there is a string variable! After my book had conviced me there isn't such a thing. Could you recommend a book that you would use to learn C++?

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    27
    I'm a little bit overwhelmed. I can't believe there is a string variable! After my book had conviced me there isn't such a thing. Could you recommend a book that you would use to learn C++?

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I'm a little bit overwhelmed.
    That's because you are trying to do too much all at once. Don't worry about bad input. Assume the user will enter an int.

    Could you recommend a book that you would use to learn C++?
    Read the current thread on that subject or search the boards. The question comes up frequently. Everyone has a different opinion on what is a good book. You also have to decide how much time you want to commit. Some books are 1,000 pages long. Others are 500 pages long. Accelerated C++ is usually recommended a lot, and it's much shorter, but I looked at it and I thought it was way too complicated for a beginner. Go to a bookstore and take a look at it and see what you think.
    Last edited by 7stud; 06-10-2005 at 02:56 AM.

  15. #15
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    That's because you are trying to do too much all at once. Don't worry about bad input

    I agree. If you're a newbie you shouldn't be worrying about having to flush the input buffer to handle bad input. It's quite unnecessary (at your level) and will probably just confuse you.

    Stick to the basics and try to plan what you want your program to do before coding it. Remember, as a new comer you should never just start hard coding your programs. You should ALWAYS draw a flow chart or write down some pseudo code before to do something. In the long run this will help you enormously.


Page 1 of 2 12 LastLast
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