0

I am having an issue. I need to check whether the user enters a digit greater than 0. If so the user needs to be reprompted. If 23409932 is entered then the 0 entered should be caught and the proper messaage displayed. Here is my code and please somebody help.

int userInput (int numberInput)

{   
    int index;
    int remains;

    cout << endl;
    cout << " Enter a positive number count: ";
    cin  >> numberInput;

    while (numberInput > 0) {
       index = numberInput % 10;

       if (index == 0) {
          cout << " Error! Enter a positive number count: ";
          cin  >> numberInput;
          numberInput = numberInput / 10;
       }
       else {
          return numberInput;
       }
    }

Edited by Reverend Jim: Fixed formatting

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by rahul8590
0
cout << " Enter a positive number count: ";
cin >> numberInput;

    if(numberInput > 0) 
    {
    index = numberInput % 10;
    cout<<"index is :"<<index;
    }
    else if
     {
     cout << " Error! Enter a positive number count: ";
    cin >> numberInput
    }
0

The OP wants to check the existence of 0 in the number only if loop wont suffice , u will have to iterate within a while loop and keep finding the last significant number one after the another and check whether that number is equal to 0 or not.

int number, in , index , flag =0 ;

cout << " Enter a positive number count: ";
cin >> number;

index = number;

while(index > 0)
{

in = index % 10 ;
index = index / 10;

if (in == 0)
{cout << " Error! The number contains 0 in it ";
flag = 1;
break;
}
}

if(flag == 0 )
cout<<" the number does not 0 in it ";

above i have written simple code .
Since initially i focused only on getting the desired output i didn't tweak it much
u can still tweak it further and shorten the steps in it .

Edited by rahul8590: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.