Specific output know, how to do it unkown

Ok, so I am trying to get my program to do something simple. It has to display either a "1", for any number entered that is illegal for an unassigned short int , a "2" if it overflows, or the number entered if it works fine.

I was trying to do the input as such:
-skip leading spaces
-first character is numeric or an error occurs
-each numeric character is processed individually and then combined
-process stops when a non-numeric character is found.

Problem is, I don't know how to do that so I used this sstream instead...
To understand what I'm saying, here's the code.
Any help at all would be appreciated.

Also note that "cin >> variable" is comepltely unwanted for this.
-----------

#include <iostream>
#include <sstream>
using namespace std;

int ReadInt(unsigned short int &Shortint);

void main()
{
unsigned short int Shortint;
char Yesno, R;

do
{
R = ReadInt(Shortint);

if (R == 0)
cout << Shortint << endl;
else if (R == 1)
cout << "1" << endl;
else
cout << "2" << endl;

cout << "Do you wish to continue? : ";
cin >> Yesno;
}
while (Yesno != 'N' && Yesno != 'n');
}

int ReadInt(unsigned short int &Shortint)
{ int Errornumber;
string Line;
stringstream stream;

cout << "Enter a number : ";
getline(cin, Line); //this is the area it is supposed to do the process mentioned earlier

stream << Line;
stream >> Shortint;

if (Shortint > 65535)
Errornumber = 2;
else if (Shortint < 0)
Errornumber = 1;
else if (Shortint <= 65535 && Shortint >= 0)
Errornumber = 0;

return Errornumber;
}
Last edited on
Topic archived. No new replies allowed.