Program wants input twice?

I wrote this program, and it has no errors, however you must enter a value and press enter twice each time it prompts for user input for the program to continue running. for example,
"enter value"
5 [enter]
5 [enter]
"you entered 5"
of course thats not my program, just what my problem is. Here's the Code:

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
#include <iostream>
#include <cmath>
using namespace std;

class verify{
    public:
        int inp;
        void verifyin(int x){
                inp = x;
                while ( ! (cin >> inp)){
                    cout << "Error. Try again: ";
                    cin.clear ();
                    cin.ignore (1000, '\n');
            }
        }

};

int main(){
    int input;
    double due, pay, stilldue, absvalue, pay2, stilldue2;
    verify obj;
    cout << "Each Movie is $10." << endl;
    cout << "Please enter in how many you want:";
    cin >> input;
    obj.verifyin(input);
    due = input * 10;
    cout << "$" << due << " is due." << endl;
    cout << "Please insert money:";
    cin >> pay;
    obj.verifyin(pay);
    stilldue = due - pay;


   while(stilldue > 0){
        cout << "Please insert $" << stilldue << endl;
        cin >> pay2;
        obj.verifyin(pay2);
        stilldue =  stilldue - pay2;
    }
    if(stilldue < 0){
        absvalue = abs (stilldue);
        cout << "Here is $" << absvalue << " back" << endl;
        cout << "thank you";
    }
    else if (stilldue == 0){
        cout << "thank you";
    }
    return 0;
}


any help would be greatly appreciated! thanks!
at which line is this happening ?
I wrote this program, and it has no errors, however you must enter a value and press enter twice each time it prompts for user input for the program to continue running.


I'd say that was an error!

Before each obj.verifyin() you get input. In each obj.verifyin() call you get input.

This may have something to do with why you have to enter a value twice. Coincidentally, if the initial input fails, subsequent input in the verifyin() method is discarded.
Last edited on
well i took out the input before each obj.verifyin() and i only have to enter the number in once, but now when i try to do obj.verifyin(input) without prompting any input for it before hand, i get really messed up numbers. i enter in that i want 5 movies and it returns -1.38195e+009. i want to run the function but store the input as the variable "input."
Last edited on
remember that cin >> var returns a bool when used in a while loop. In your class you ask whether the cin returns true of false. In order to get true or false the computer must ask for input(again)
i got it now. Simple program is finished and works beautifully now.

Finished Code:
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
#include <iostream>
#include <cmath>
using namespace std;

class verify{
    public:
        int x;
        void verifyin(){
            while ( ! (cin >> x)){
                    cout << "Error. Try again: ";
                    cin.clear ();
                    cin.ignore (1000, '\n');
            }

        }
        int returnn(){
            return x;
        }

};

int main(){
    int input;
    double due, pay, stilldue, absvalue, pay2, stilldue2;
    verify obj;
    cout << "Each Movie is $10." << endl;
    cout << "Please enter in how many you want:";
    obj.verifyin();
    input = obj.returnn();
    cout << "input is " << input << endl;
    due = input * 10;
    cout << "$" << due << " is due." << endl;
    cout << "Please insert money:";
    obj.verifyin();
    pay = obj.returnn();
    stilldue = due - pay;


   while(stilldue > 0){
        cout << "Please insert $" << stilldue << endl;
        obj.verifyin();
        pay2 = obj.returnn();
        stilldue =  stilldue - pay2;
    }
    if(stilldue < 0){
        absvalue = abs (stilldue);
        cout << "Here is $" << absvalue << " back" << endl;
        cout << "thank you";
    }
    else if (stilldue == 0){
        cout << "thank you";
    }
    return 0;
}
oh god, nevermind I forgot about entering in floating point numbers. Lol oh well, at least my initial problems are solved. Thanks guys! One more thing, would anyone be willing to critique my code? Is there more efficient ways of doing what I'm doing?
This is the correct version if your program....


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
#include <iostream>
#include <cmath>
using namespace std;

class verify{
    double inp;
    public:
        double verifyin(){
                while ( ! (cin >> inp)){
                    cout << "Error. Try again: ";
                    cin.clear ();
                    cin.ignore (1000, '\n');
            }
            return inp;
        }
};

int main(){
    int input;
    double due, pay, stilldue, absvalue, pay2, stilldue2;
    verify obj;
    cout << "Each Movie is $10." << endl;
    cout << "Please enter in how many you want:";
    //cin >> input;
    input=(int)obj.verifyin();
    due = input * 10;
    cout << "$" << due << " is due." << endl;
    cout << "Please insert money:";
    //cin >> pay;

    pay=obj.verifyin();
    stilldue = due - pay;


   while(stilldue > 0){
        cout << "Please insert $" << stilldue << endl;
        //cin >> pay2;
        pay2=obj.verifyin();
        stilldue =  stilldue - pay2;
    }
    if(stilldue < 0){
        absvalue = abs (stilldue);
        cout << "Here is $" << absvalue << " back" << endl;
        cout << "thank you";
    }
    else if (stilldue == 0){
        cout << "thank you";
    }
    return 0;
}
Topic archived. No new replies allowed.