i have one little problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
  int AskRange;
   
   while ( true )
     {
       cout << " Please enter a value that is greater than zero \n";
       cin >> AskRange;
       cin.ignore();
        
       if ( AskRange > 0 )
        {
           break;
        }
          else
           {
           }
     }
return(0);
}


how come when i enter a letter is spams me?
what am i doing wrong ?
Last edited on
I suggest using the sstream and the string library. It handles the transition in case they don't input the value you want them to.

1
2
#include <sstream>
#include <string> 


And here is how you would use it:

1
2
3
4
5
int i;
	std::string test;
	std::cin >> test;
	std::stringstream(test) >> i;
	std::cout << i << std::endl;
is there another way to do it besides that?

Cause i haven't been taught that and i don't think i should use something i haven't learned yet or else my prof. will think i didn't do this.

Could i adjust my conditions or add some ?
Last edited on
I mean. It spams if you type in a letter, but you're not supposed to type in a letter, it clearly says input a value greatar than 0, meaning a number.
Last edited on
yeah i agree, but what if they do, is there a way to just loop back?
closed account (2LzbRXSz)
It spams you because you haven't programmed it to handle any sort of input that isn't and integer.

However, if you edit your if statement as so,
1
2
3
4
5
6
7
8
9
10
11
if ( AskRange > 0 )
        {
            break;
        }
        else if (isdigit(AskRange) == false)
        {
            break; // Stopping the program
        }
        else
        {
        }

(You can read more about isdigit here http://www.cplusplus.com/reference/cctype/isdigit/ )

Though, I have a feeling you don't want it to stop just because it's not an int.
You can just add a parameter to your cin.ignore().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int AskRange;

	while (true)
	{
		cout << " Please enter a value that is greater than zero \n";
		cin >> AskRange;
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');

		if (AskRange > 0)
		{
			break;
		}
		else
		{
		}
	}
My prof. hasn't taught any of this lol and by that i mean the "is digit" thing and the cin.ignore() parameters, so that makes me believe there must be a different way of doing it. Thanks for helping me by the way.
Last edited on
If your professor hasn't taught you the necessary tools to be able to handle this problem, then don't worry about it. He will (hopefully) teach you about these things in your future.
Yeah you're right haha, i read the sheet again and it just says " check that the user indeed enters a positive number " so, he shouldn't be entering a letter when compiling it.

but thanks guys for the help :D
closed account (2LzbRXSz)
By the way, just a thought. Possibly it's set up so that you realize it spams you when a letter is inputted, making for a great transition to the next lesson (which would be stringstreams)?
Topic archived. No new replies allowed.