-1
//review3
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
using namespace std;

int number;
int main()
{
        cout << "Enter a positive number" << endl;
        cin >> number;
        while (number < 0)
        {
            cout << "Enter a positive number" << endl;
        }
        if (number > 0)
        {
            cout << "Awesome job!" << endl;
        }
        return 0;
}

This is my code so far. I started with an else if but if the user entered a negative number the program would simply close. I changed this to a while loop and got stuck in an infinite loop. Before I had an if and else if statement. I need to continue to prompt the user until they enter a positive number in c++.

5 Answers5

3

Your while() loop doesn't continue to prompt for input, that's why you're getting an infinite loop - because number never changes!

You can put the input operation into the while() loop like this:

while (cin >> number && number < 0)
{
    cout << "Enter a positive number: " << endl;
}

if (cin)
{
    cout << "Awesome job" << endl;
}

Thus, during each iteration of the loop the user will be prompted for input.

We check the state of cin afterwards to make sure that the above loop didn't stop because of invalid input (or no input at all).

0x499602D2
  • 87,005
  • 36
  • 149
  • 233
  • 1
    And what happens if the user enters `"abc"`. I'd make the loop condition `while ( !(std::cin >> number) || number < 0 )`, and then, in the loop, if there was an error on cin, clear it and ignore the erroneous input. (Of course, if the error was due to end of file, because the input was redirected from a file, this will lead to an endless loop as well. So perhaps it should be `while ( (!(std::cin >> number) && !std::cin.eof()) || number < 0 )`. With an additional check after the loop, since you don't want to output success if the input was redirected from an empty file.) – James Kanze Nov 09 '14 at 00:46
  • 1
    And of course, if he's using this solution, he should initialize `number` to something negative before entering the loop. – James Kanze Nov 09 '14 at 00:48
  • @JamesKanze Good idea. But when you say "additional check after the loop", do you mean like my `if (cin)`? Or should it be `if (!cin.eof())`? – 0x499602D2 Nov 09 '14 at 01:07
  • `cin`. If we break out of the loop, we've either encountered end of file (which means that both `!cin` and `cin.eof()` are true), or we've successfully read a non-negative number, in which case, `cin` is true, but `cin.eof()` may be either true or false. – James Kanze Nov 10 '14 at 09:19
0
#include <iostream>
#include <string>
#include <cstring>
int main()
{
    while(true)
    {
        std::cout << "Pleaase enter a positive number." << std::endl;
        std::string buf;
        int x = -255;
        std::cin >> buf;
        x = std::atoi(buf.c_str());
        if(x > 0)
        {
            break;
        }
    }
    std::cout << "Awesome Job!" << std::endl;
}
yash101
  • 633
  • 1
  • 8
  • 20
  • 1
    Undefined behavior if the user enters `"abc"`. – James Kanze Nov 09 '14 at 00:49
  • I am sure that can be fixed by properly initializing x. Lemme fix that real fast! – yash101 Nov 09 '14 at 04:18
  • So instead of undefined behavior, you have an endless loop. – James Kanze Nov 10 '14 at 09:21
  • How is that going to create an endless loop? It will loop until the correct value is given. Then, it will break out of the loop using that break keyword. It will then display, "Awesome Job!" – yash101 Nov 10 '14 at 16:06
  • 1
    As soon as the user inputs something which isn't a number, `std::cin` will set `failbit`, and all further operations will be no-ops. – James Kanze Nov 10 '14 at 16:35
  • Now I understand. I am a bit confused though, because that code always works for me! Otherwise, one could also do std::string x and then atoi/stoi – yash101 Nov 11 '14 at 00:44
0
#include <iostream>
using namespace std;

int number;
int main()
{
    cout << "Enter a positive number" << endl;
    cin >> number;
    if (number < 0) {
    cout << "Enter a positive number" << endl;
    }
    else {
    cout << "Awesome job!" << endl;
    }
    return 0;
}
lappet
  • 106
  • 1
  • 7
0

You can check if you get number or string here

sure in this case you should get input to string variable. If you want to convert it to integer you can use std::stoi

Community
  • 1
  • 1
santipianis
  • 41
  • 1
  • 7
-1

You can also do this snippet . Use #include <ctype.h> or <cctype>

while(1){
  cin>>number;
  if(number<0 || isalpha(number)) return 0;
  cout<<"Awesome Job";
}
saruftw
  • 856
  • 12
  • 30