What am I doing wrong? Infinite loop etc.

I am taking a class in C++ and my task is to create a program like 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int km;
    char y;

    while(km)
        {
            cout << "How long is your trip?" << endl;
            cin >> km;
            cin.ignore();

            cout << endl << "Best way to get to destination is ";

            if(km <= 2)
                {
                    cout << "by walking." <<endl;
                }
            else if(km >= 8)
                {
                    cout << "with a vehicle." << endl;
                }
            else
                {
                    cout << "with a bike." << endl;
                }

            do
                {
                    cout << endl << "Continue? Y/N: ";
                    cin >> y;
                    cin.ignore();

                }
            while(((y !='Y')&&(y !='N')&&(y !='y')&&(y !='n')));

            if((y =='n')||(y =='N'))
                {
                    break;
                }
        }
return 0;
}


First of all, it must be an easier way? This is so much.... code..?

Second:
When I type in a letter instead of a number at
1
2
3
            cout << "How long is your trip?" << endl;
            cin >> km;
            cin.ignore();

it just keeps on looping. Infinite loop. No way to stop it. How do I prevent this in an easy way?

Third:

When I answer the Continue with multiple char (like Continue Y/N: BLAHA) it asks the same questions five times. ???

Sorry, I know this is a very beginner question. But I'm really stuck and I need to get this right.

Thanx
When I type in a letter instead of a number it just keeps on looping. Infinite loop.

That's because this causes the failbit to be set (you can check it with cin.fail()), which will cause all subsequent operations to fail as well. Call cin.clear() in order to reset the fail bit.

How do I prevent this in an easy way?

The standard C++ streams and "easy" are mutually exclusive.

When I answer the Continue with multiple char (like Continue Y/N: BLAHA) it asks the same questions five times. ???

This is because you're reading only a single character. Use ignore or getline to extract any characters that might follow.

Also, this is incorrect:
while(km)
km is not initialized. Use for(;;)

Your existing code can be shortened slightly:

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
int main()
{
    for(;;)
    {
        int km;
        cout << "How long is your trip?" << endl;
        cin >> km;
        cin.ignore();

        cout << endl << "Best way to get to destination is ";

        if (km <= 2)cout << "by walking." << endl;
        else if (km >= 8)cout << "with a vehicle." << endl;
        else cout << "with a bike." << endl;

        for(;;)
        {
            char y;
            cout << endl << "Continue? Y/N: ";
            cin >> y;
            cin.ignore();
            if (tolower(y)=='y')break;
            else if (tolower(y)=='n')return 0;
        }
    }
}
Last edited on
That's because this causes the failbit to be set (you can check it with cin.fail())

Thank you! Let's see if I got this correct. Something like:
1
2
3
4
            cout << "How long is your trip?" << endl;
            cin >> km;
            cin.fail();
            cin.ignore();

Do I need to put something in between the ()?

Where can I find more information about how to use the
ignore
or
getline
and how to extract chars?

I do know there is a problem with the while(km), but I did not know any other way to make it work. How do I set up the for loop in a correct way? Any example?

Again: Thanks! =)
Do I need to put something in between the ()?

No, the function returns whether the fail bit is set.
See: http://www.cplusplus.com/reference/iostream/ios/fail/

Where can I find more information about how to use the ignore or getline and how to extract chars?

Also on the reference on this site:
http://www.cplusplus.com/reference/iostream/istream/ignore/
http://www.cplusplus.com/reference/string/getline/

I do know there is a problem with the while(km), but I did not know any other way to make it work. How do I set up the for loop in a correct way? Any example?

As shown in the modified code I posted. You want a pseudo-infinite loop, so you don't need a condition - while doesn't allow omitting the condition, but for does. Alternatively, you can use while(true).
Topic archived. No new replies allowed.