As a preface, I want to say that this is a question asked by a student, but unlike most of their types, this is a quality question that deserves a qualitative answer, so I will try to do it;). I will not try to just answer your specific question, but to show you other small problems in your code.
First of all, analyze your code step by step. More or less like what the debugger does. Take the time to read this carefully;) ...
#include <iostream> #include <CTYPE.h>
Includes the headers <iostream> and <ctype.h> (uppercase works due to some NTFS Windows flaws / solutions). I recommend that you replace the second line with #include <cctype> .
using namespace std;
This is normal for any beginner / student, but he has no habit! For the purpose of "cleanliness," I would explicitly use std:: along this answer, as if this line did not exist.
int ReadInt (unsigned short int &UserIn);
Declares a ReadInt function that takes a UserIn link to enter unsigned short int and returns an object of type int .
int main() {
Special function main ; no parameters, returns int . Start function.
int Error; unsigned short int UserInput; char RepeatProgram;
Declares the variables Error , UserInput and RepeatProgram with the corresponding types int , unsigned short int and char .
do {
Do-while block. Begin.
Error=ReadInt(UserInput);
Assign an int ReadInt returned with the UserInput argument of int& type int& Error variable of unsigned short int .
if (Error==0) std::cout << "Number is " << UserInput << endl;
If Error is zero, print UserInput to standard output.
else if (Error==1) std::cout << "Illegal Data Entry\n"; else if (Error==2) std::cout << "Numerical overflow, number too big\n";
Otherwise, if an error occurs, report it to the user using std::cout .
std::cout << "Continue? n/N to quit: "; std::cin >> RepeatProgram;
Ask the user if he wants to continue or exit. Save the input character in a RepeatProgram type char .
std::cout << std::endl;
It is redundant if you do not want to add indentation, which is probably your goal. Actually, you'd better do std::cout << '\n' , but that doesn't really matter.
} while (RepeatProgram!='N' && RepeatProgram!='n');
The expression match for the do-while block above. Repeat this block if RepeatProgram is neither the lowercase nor the capital letter N
}
The final function is main . The implicit return value is zero.
int ReadInt (unsigned short int &UserIn) {
The ReadInt function accepts the ReadInt link in an unsigned short int and returns an object of type int . Start function.
int Err=0; char TemporaryStorage; long int FinalNumber=0;
Declares Err , TemporaryStorage and FinalNumber corresponding int , char and long int types. Err and FinalNumber initialized to 0 and 0 , respectively. But, only one thing. Didn't the job indicate that the exit number is stored in unsigned short int ? So, better ...
unsigned short int FinalNumber = 0;
Now...
std::cout << "Enter a number: ";
A? What was that supposed to be? ( Error : canceling the debugger because it does not make logic! **). I expect you just forgot // before the comment, right? Now what do you expect from !' ' !' ' for a rating other than '\0' ? istream::ignore(n, ch) will discard characters from the input stream until N characters are discarded, ch found, or the final file is reached.
A better approach would be ...
do std::cin.get(TemporaryStorage); while(std::isspace(TemporyStorage));
Now...
std::cin.get(TemporaryStorage);
This line can be discarded using the above approach;).
Right Now that you are in the part where you obviously hit your head on all the solid objects known to mankind. Let me help you a little. We have such a situation. With the above code, TemporaryStorage will contain the first character, which is not a space after the while-while loop. So, we have three things left. First of all, make sure that the input has at least one digit, otherwise return an error. Now that the input is made up of numbers, convert the characters to integers and then multiply them to get the actual integer. Finally, and this is the most ... ah ... the weird part, we need to avoid overflow.
if (!std::isdigit(TemporaryStorage)) { Err = 1; return Err; } while (std::isdigit(TemporaryStorage)) { unsigned short int OverflowChecker = FinalNumber; FinalNumber *= 10;
The code is self-explanatory. Please comment if you have any doubts.
std::cout << TemporaryStorage;
Should I say something here? Anyway, I already did it. Just remember to take this std::cout before showing your work;).
}
The ultimate ReadInt function.