C++98 user entry question

Hi guys, I'm trying to put together a piece of code that will take a user entry (a number from 1-65535). I want to have it in both string and int formats and be able to pick out all the invalid entries.
What I wrote below pretty much does what I need it to do, with one exception: the output is correct only when I enter a correct number at the first attempt. If, for instance I make 3 consecutive entries: 1. dskj8e8, 2. 8a8, 3. 12345, the output product will not be "12345" but "123451234512345". As you can see, I'm trying to reset the variable each time the entry is incorrect, but not doing it right, I guess. What is the best way to go about this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

string inp;
int inpSize = 0;
int number = 0;

int main()
{
    cout << "Enter a number from 1 to 65535 ";
    getline(cin, inp);
    inpSize = inp.size();
    if (inpSize > 5 || inpSize < 1)
    {
        cout << "Invalid Entry" << endl;
        inp = "";
        main();
    }
    for (int i = 0; i < inpSize; i++)
    {
        if (!isdigit(inp[i]))
        {
            cout << "Invalid Entry" << endl;
            inp = "";
            main();
        }
    }
    istringstream ( inp ) >> number;
    cout << number;
}
main is a special function as the entry point of the program - it shouldn't be called again within itself.

You could do something like a while loop that continues to prompt for input until the input is valid.
Thank you! I was certain it was concatenating the string, until I ran a few more tests. I am such a noob :)
Topic archived. No new replies allowed.