Warning Message Print out Several Times from Wrong Input

Hello All,

I am new to C++ and here is my code

https://onlinegdb.com/SyYxPykTM

I want to set a checker using "While Loop" on line 34 to filter out non-integer input. The problem i am having on the checker is that it repeatedly printing itself bases on how many non-integer letter or symbol I had entered before returned to line 33. I kind of get a sense that C++ reads my input number-by-number and the message bar comes out whenever it reads something non-integer rather than integer number as a whole. For instance, "1" "a" "b" rather than "1ab". In this case, the first number passed the checker and moved on but the rest got capture with two warning messages follow suit. How can I ask C++ to read the number as a whole? Many Thanks for this.
Last edited on
This
 
while(!(array[y])){
is the same as
 
while(array[y] == 0){

What you probably want is
 
while(cin){

Or if you want you can put the >> operator into the loop condition. That way you don't need to repeat the same code on line 33 and 39.
1
2
3
4
5
cin>>array[y];
while (cin>>array[y]) {
	...
	cin>>array[y];
}



Other problems that you need to fix:
• You need to add return statements to printheader and CheckInputValu, or change the return type to void.
• You need to initialize a before passing it as argument to CheckInputValu.
• In CheckInputValu you have two variables named y, which is confusing. The loop always runs three times no matter how big the array is so you might not use all elements, ore you might run out of bounds.
Thz for your promptly reply Peter.

I think i kind of get a sense of where the problem is originated. Its all about "!int" in which C++ doesn't read the number as a string. I am using stringstream to convert Int to string and sort of scan the string input if it contains letter or symobl or whilespace. It works quite the way i want it to do.
Your best bet is to read a whole line with std::getline and parse it with istringstream.
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
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

void CheckInputValue(int *a) {
    const char *NameOfVariable = "ABC";
    for (int i = 0; i < 3; i++) { 
        while (true) {
            cout << "Please Enter An Integer for Variable "
                 << NameOfVariable[i] << ": ";
            string line;
            getline(cin, line);
            istringstream sin(line);
            if (sin >> a[i]) {
                char c;
                if (sin >> c) { // if successful reading non-space char
                    cout << "Error: extra characters in input\n";
                    continue;
                }
                break;
            }
            cout << "Error: not an integer\n";
        }
    }
}

int main() {
    cout << "Please Enter A, B and C for the parabolic function:\n";
    cout << "  y = Ax^2 + Bx + C\n";
    int abc[3] = {0};
    CheckInputValue(abc);
    printf("A: %d,  B: %d,  C: %d\n", abc[0], abc[1], abc[2]);
    return 0;
}

Last edited on
I further tweaked my code like this after including a string function. But one thing kept bugging me on line 27 and line 29. I knew npos means found or not found from a string input. But why include two seems so contradictory statements? What does it do to the one within the if-statement?

line 27: theInput.find_first_not_of("0123456789")!=std::string::npos
line 29: theInput.find_first_not_of("0123456789")==std::string::npos

Here is my code:
https://onlinegdb.com/SJgG6wbaG
Topic archived. No new replies allowed.