Thread: little problem

  1. #1
    mrukok
    Guest

    little problem

    using the following code:

    -------------------------------------------------
    #include <iostream>

    using namespace std;

    int main()
    {
    int num = 0;
    cin >> num;

    while (num < 1 || num > 170)
    {
    cout << "You entered an invalid number, try again:" << endl;
    cin >> num;
    }

    cout << num;

    return 0;
    }

    ----------------------------------------------

    everything works fine unless i input a character, which prints the cout message over and over again to the screen.

    can someone help me with this please

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You can't read a character into an integer variable.
    The most common solution to this is to read theinput as a string, check whether it's numerical digits only, then convert it to an integer using atoi() or something.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    mrukok
    Guest
    i wouldnt have thought it a problem, because when i do this:

    int num;
    cin >> num;
    cout << num;

    and enter a character it just returns 0

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The problem lies in the loop. cin << int tries to read an integer value from the instream. It encounters a character instead, thus leaving it in the keyboard buffer and moves on. In the next iteration it finds that character again and will go on doing this again and again and again and again... forever.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    mrukok
    Guest
    so the solution is...?

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    What I mentioned in my first reply.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    A possible solution to your problem is to check what type of input the user gave before it is processed. A check can determine wether or not input is valid (ie is it an integer or invalid type, such as a character?).

    Take this code for example:

    Code:
    while (cout << "Enter age: " && !(cin >> age))
    	{
    		cout << "\nInvalid input\n";
    		cin.clear();
    		cin.ignore();
    	}
    This line prompts the user for their age. Should you enter an integer (which is the declaration of age), it will run correctly and store the number in age. If you enter something else, like a character, it will indicate invalid input, and prompt you again for an integer value.

    I hope this helps,

    -RoD

  8. #8
    mrukok
    Guest
    yeah i think that does what i want.

    could you just run past me what exactly is happening though

  9. #9
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    My pleasure!

    Code:
    (cout << "Enter age: " && !(cin >> age))
    The user is prompted to enter their age. After they do so and strike enter, its read into age just like any other time with the exception that its in a while statement. I'm not sure exactly how this works, but it checks to see if it is a integer. if not (!), it will enter the body of the while loop(while not an integer). At this time the user is told its invalid and prompted again. If the statement while not an integer returns false (if an int is enterd it will be false) it breaks from the loop and continues on through the program.

    Hope that helps!

    -RoD

  10. #10
    mrukok
    Guest

    Thumbs up

    yeah thanks.

  11. #11
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Anytime, here is an example program from VC++ 6.0:

    Output:

    Code:
    Enter age: h
    
    Invalid input
    Enter age: 6
    An integer was recieved!
    6
    Press any key to continue
    Code:

    Code:
    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    
    int main(int argc, char* argv[])
    {
    	int age;
    
    	while (cout << "Enter age: " && !(cin >> age))
    	{
    		cout << "\nInvalid input\n";
    		cin.clear();
    		cin.ignore();
    	}
    
    	cout << "An integer was recieved!" << endl;
    
    	cout << age << endl;
    
    	return 0;
    }

  12. #12
    mrukok
    Guest
    is there a way to incorporate a range verification into that aswell?
    so that it has to not only be an integer, but also, for example, between 1 and 100.

  13. #13
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Of course! Just add an if statement:

    Code:
    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    
    int main(int argc, char* argv[])
    {
    	int age;
    
    	while (cout << "Enter age: " && !(cin >> age))
    	{
    		cout << "\nInvalid input\n";
    		cin.clear();
    		cin.ignore();
    	}
    
    	if (age >= 1 && age <= 100)
    	{
    		cout << "An integer was recieved!" << endl;
    
    		cout << age << endl;
    	}
    	else 
    	{
    		cout << "Number not between 1-100" << endl;
    	}
    
    	return 0;
    }
    -RoD

  14. #14
    mrukok
    Guest
    but then the program will just end. is there no way to keep looping the "Enter age: " output while the input is either not an int or not between 1 and 100?

  15. #15
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Yea we can do it that way:

    Code:
    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    
    int main(int argc, char* argv[])
    {
    	int age;
    
    	while (cout << "Enter age: " && !(cin >> age) || (age < 1 || age > 100))
    	{
    		cout << "\nInvalid input or out of range\n";
    		cin.clear();
    		cin.ignore();
    	}
    		cout << "An integer was recieved!" << endl;
    
    		cout << age << endl;
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM