Verifying Integer Input with a function call

I'm trying to verify that the user has input an integer. I found a great post on this on this site (http://cplusplus.com/forum/articles/6046) - Thanks Zaita!

I'm now trying to modify this so that I can use it as a function call (just the integer part from Zaita;s post). The context is that the user's input would have been captured in a string variable via getline(cin, response); which I then pass to the following function as a string.

Here is what I have done:

bool confirmInteger(string response) {
int myNum = 0;
while (true) {
// Convert from string to integer
stringstream myStream(response);
if (myStream >> myNum) {
break;
barkAtUser(); //invalid entry detected. Send back for re-entry.
}
}
return true;
}

This just goes into an infinite loop. Despite reading about this, I don't understand the test (myStream >> myNum). As I step through this code, this test always evaluates to false, so the loop never terminates. The idea of this is that this test would evaluate to true if the input was not an integer, thus terminating the loop. It works in Zaita's post, but not in mine.

Can anybody see what I have screwed up?

Thanks!
Last edited on
Here's a little program i just wrote as a excercise. Thanks for the idea i've been looking for something like this too.

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <sstream>
#include <iostream>
#include <string>
#include <ctype.h>

using namespace std;

template <class type>
bool good_input(type & item, const string & thestring);
bool good_input(string & item, const string & thestring);

int anumber;
char acharacter;
string astring;
class word
{
    private:
    string data;

    public:
    word()
    {
        data = "";
    }

    word(const word & w)
    {
        data = w.data;
    }

    ~word()
    {}

    const char * c_str()
    {
        return data.c_str();
    }

    friend istream &operator>>(istream &in, word & w)
    {
        string temp = w.data;
        in >> w.data;
        int size = w.data.size();
        for (int i = 0; i < size; ++i)
            if (!isalpha(w.data[i]))
                {
                    w.data = temp;
                }
    }

    friend bool & operator >>(stringstream &in, word &w)
    {
        word temp = w;
        in >> w.data;
        int size = w.data.size(), i = 0;
        for (i = 0; i < size; ++i)
            if (!isalpha(w.data[i]))
                {
                    w = temp;
                    //return 0;
                }
        //return i;
    }

    friend ostream & operator<<(ostream & out, const word & w)
    {
        out << w.data;
    }

} aword;

int main()
{
    string holder = "";
    short start = 0;
    while(1)
        {
            switch(start)
                {
                    case 0:
                        printf("Enter a number.\n>");
                        getline(cin, holder);
                        if (!good_input(anumber, holder))
                            {
                                --start;
                                printf("\n");
                            }
                        else printf("\nYou typed: %d\n", anumber);
                        break;

                    case 1:
                        printf("\nEnter a character.\n>");
                        getline(cin, holder);
                        if (!good_input(acharacter, holder))
                            --start;
                        else printf("\nYou typed: %c\n", acharacter);
                        break;

                    case 2:
                        printf("\nEnter a word.\n>");
                        getline(cin, holder);
                        if (!good_input(aword, holder))
                            --start;
                        else printf("\nYou typed: %s\n", aword.c_str());
                        break;

                    case 3:
                        printf("\nEnter a string.\n>");
                        getline(cin, holder);
                        if (!good_input(astring, holder))
                            --start;
                        else { start = -1; /*printf("\nYou typed: %s\n\n", astring);*/  cout << "You typed: " << astring << "\n\n"; }
                //}
                        break;

                    default: break;
                }
            ++start;
        }
    return 0;
}

template <class type>
bool good_input(type & item, const string & thestring)
{
    type tempitem = item;
    stringstream tempstream(thestring);
    string junk = "", garbage = "";
    if (tempstream >> item)
        {
            if ( (tempstream >> junk) )
                {
                    while( tempstream >> garbage)
                        {
                            junk += " ";
                             junk += garbage;
                             //cout << "garbage = " << garbage << endl;
                             //cout << "junk = " << junk << endl;
                        }
                    item = tempitem;
                    return false;
                }
            else return true;
        }
    else
        {
            item = tempitem;
            return false;
        }
}

bool good_input(string & item, const string & thestring)
{
    string tempitem = item;
    stringstream tempstream(thestring);
    string junk = "", garbage = "";
    if (tempstream >> item)
        {
            if ( (tempstream >> junk) )
                {
                    while( tempstream >> garbage)
                        {
                            junk += " ";
                             junk += garbage;
                        }
                    item += ' ';
                    item += junk;
                    return true;
                }
            else return true;
        }
    else
        {
            item = tempitem;
            return false;
        }
}


I was still guessing at a lot of it, especially with overriding the >> operators in the word class, and having to declare the word c_str() function as a const char *. beats me. but ive tested it and it appears to work. anyone else wanna look at my code and tell me if there is anything wrong with it?
in particular this was the only version that actually specifically returns a value for the friend bool & operator >>(stringstream &in, word &w) that I could get to compile:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    friend bool & operator >>(stringstream &in, word &w)
    {
        word temp = w;
        in >> w.data;
        int size = w.data.size(), i = 0;
        for (i = 0; i < size; ++i)
            if (!isalpha(w.data[i]))
                {
                    w = temp;
                    bool * t; *t = false;
                    return *t;
                    //return 0;
                }
        //return i;
        bool * t; *t = i;
        return *t;
    }


Sadly, this also crashes.
Your program is awesome, but is a bit advanced for me. I know that the program written by Zaita works perfectly, so I don't want to stray very far from that unless I know that it cannot be modified to work with a function call. Can anyone see how to modify Zaita's code for this purpose?

Thanks.
I did modify Zaita's code. Just skip over the advanced bits and look at the bool good_input(string & item, const string & thestring); and the templated function. The template function just means that it will be called for whatever parameter is passed to it, int, char, bool, double etc. The only exception is string, which will execute its own function instead.


Topic archived. No new replies allowed.