First output is always wrong in do-while loop?

Hello. I'm new to C++. I was just practicing this code and I noticed that even if I input 5 or 1 or 2, the first output result is always wrong in the loop. If I press 5, the output is,

"You've inserted USD 5 note which is not supported."
Why is it doing that?

But when the loop wants user prompts, it works fine. 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
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string a = "Coca Cola";
    string b = "Pepsi";
    string c = "Sprite";
    string d = "Nescafe Ice Mocha";
    string f;
    double e; //The money inserted by the user
    cout << "Welcome to the Vending Machine!\n";
    cout << "Please Enter only USD 1, 2 & 5 notes: ";
    cin >> e;
    cout << endl;
    do
    {
        cout << "You have inserted " << e << " USD note which is not supported.\n";
        cout << "Please Try Again!: \n";
        cin >> e;
        cout << endl;
    }
    while ( e != 1 && e!= 2 && e!= 5);
    cout << "You've entered " << e << " !";
    cout << endl;
}


Thank you!
With a do loop:
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
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string a = "Coca Cola";
    string b = "Pepsi";
    string c = "Sprite";
    string d = "Nescafe Ice Mocha";
    string f;
    double e; //The money inserted by the user
    cout << "Welcome to the Vending Machine!\n";
    cout << "Please Enter only USD 1, 2 & 5 notes: ";
    do
    {
        cin >> e;
        cout << endl;
        if (e != 1 && e != 2 && e != 5 )
        {
           cout << "You have inserted " << e << " USD note which is not supported.\n";
           cout << "Please Try Again!: \n";
        }
    }
    while (e != 1 && e != 2 && e != 5 );

    cout << "You've entered " << e << " !";
    cout << endl;
    return 0;

}


With a while loop:

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

int main()
{
    string a = "Coca Cola";
    string b = "Pepsi";
    string c = "Sprite";
    string d = "Nescafe Ice Mocha";
    string f;
    double e; //The money inserted by the user

    cout << "Welcome to the Vending Machine!\n";
    cout << "Please Enter only USD 1, 2 & 5 notes: ";
    cin >> e;
    cout << endl;
    while (e != 1 && e != 2 && e != 5 )
    {
        cout << "You have inserted " << e << " USD note which is not supported.\n";
        cout << "Please Try Again!: \n";
        cin >> e;
        cout << endl;
    }


    cout << "You've entered " << e << " !";
    cout << endl;
    return 0;

}


Thank you, MiiNiPaa. Actually I initially wrote the code with While loop and it worked liked the one CodeWriter wrote. I just wanted to explore a bit since I hardly saw do-while loop in action and that' why I thought I'd give it a go. Thank you for the links :)

And thank you CodeWriter for taking your time to solve the problem. Appreciate it! :)
Topic archived. No new replies allowed.