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;
       }
    }

Dani AI

Generated

A robust, simple approach is to read the user input as a string and validate every character instead of trying to peel digits off an integer. That avoids integer overflow, handles non-digit input cleanly, and makes it trivial to reprompt until every digit is 1-9 (no zero allowed).

Several issues in the thread:

  • : returning from inside the digit loop and dividing only in the zero branch will stop the scan early and drop digits incorrectly. Also there is no clear handling for negative or non-numeric input.
  • : the sample has syntax problems (missing semicolons, malformed else/if).
  • : the modulo loop correctly finds a zero digit, but it stops at the first scan and does not reprompt the user or validate non-digit input. Any reprompt must restart the full validation on the new input.

Example (string-based) — reads a line, rejects any non-digit or any '0', and reprompts until valid:

#include <iostream>
#include <string>
#include <cctype>

int main() {
    std::string s;
    while (true) {
        std::cout << "Enter a positive number count: ";
        if (!std::getline(std::cin, s)) return 0; // EOF
        if (s.empty()) continue;
        bool bad = false;
        for (char c : s) {
            if (!std::isdigit(static_cast<unsigned char>(c))) { bad = true; break; }
            if (c == '0') { std::cout << "Error: digits must be 1-9 (no 0)\n"; bad = true; break; }
        }
        if (bad) continue;
        std::cout << "Accepted: " << s << '\n';
        break;
    }
    return 0;
}

Notes: always cast to unsigned char before calling std::isdigit. If you later need a numeric type, convert with std::stoll inside try/catch to handle out-of-range or invalid input. If you prefer the modulo approach shown earlier, wrap the entire input-and-scan in a loop, never return on the first non-zero digit, and always restart scanning after reprompting.

Recommended Answers

All 2 Replies

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
    }

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 .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.