Error Checking

Everytime I run my program and enter in a letter for my input values I go into an infinite loop, I have no clue how to error check it either, I've been working on it for days...

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

//Prefix functions

char displayMenu();
double getFahrenheit();
double getCelsius();
void displayFahrenheit();
void displayCelsius();

int main()
{
// Setting my Variables
    char choice;
    double C,F;

// Menu options, Start loop
    do
    {
        choice = displayMenu();
        switch(choice)
        {
        // Everything for Choice A,a
                case 'a':
                case 'A':
                {
                getFahrenheit();
                displayFahrenheit();
                        break;
                }
        //Everything for Choice B,b
                    case 'b':
                    case 'B':
                {
                getCelsius();
                displayCelsius();
                        break;
                }
        // End Program
                    case 'C':
                    case 'c':
                    {
                        return 0;
                        break;
                    }

                    default:
                    {
                        // Option for if they key'd in something other than what was given
                        cout << "You did not key in either A B or C\n " << endl;
                    }
        }
    }
        while(choice != 'C' && choice != 'c');

return 0;
}


char displayMenu()
{
        char choice;
        cout << "\t Welcome to the Temperature Converter!!\n\n";
        cout << "A) Celsius - Fahrenheit " << endl;
        cout << "B) Fahrenheit - Celsius " << endl;
        cout << "C) Close Program " << endl;
        cout << "Choose one of the following letters and press enter." << endl;
        cin >> choice;
        return choice;
}


double getFahrenheit()
{
        double F,C;
        cout << setprecision(1) << fixed << showpoint;
        cout << "\tCelsius - Fahrenheit\n " << endl;
        cout << "Enter a degree in Celsius, Then push enter\n";
        cin >> C;
        F = (9.0/5)*C+32;
        return F;
}

void displayFahrenheit()
{
       double F;
       cout <<"\n\tYour degree's in Fahrenheit is " << endl;
       cout <<"\n" << setw(25)<< F << "\n" << endl;
       return;
}

double getCelsius()
{
        double F,C;
        cout << setprecision(1) << fixed << showpoint;
        cout << "\tFahrenheit - Celsius\n " << endl;
        cout <<"Enter a degree in Fahrenheit, Then push enter\n";
        cin >> F;
        C = (F-32)/(9.0/5);
        return C;
}

void displayCelsius()
{
        double C;
        cout <<"\n\t Your Degree's in Celsius is " << endl;
        cout <<"\n" << setw(25)<< C << endl << "\n";
        return;
}
Just looking at line 56 I see a problem. You use && which means 'and'. There is no way choice can be 'C' and 'c'. Try changing && to ||, 'or'.
No change, thats just my false statement for my Switch,

The loop happens when it ask me for a input, Line 81 or 100

typically degrees is a number however, if I key in a letter I go for a infinite loop.
Hi,

Can we ask you not to start a new topic, when the question is related directly to the code you had in a previous topic, just keep the same topic going.

I have a personal hatred for constructs like this, the are error prone, UGLY, unnecessary, and non scalable:

while(choice != 'C' && choice != 'c');

Instead, provide a Quit option in your menu, and in the switch. And a while loop can run on a bool Quit value.

1
2
3
4
5
6
7
bool Quit = false;

while (!Quit) {
   // menu
   //switch

}


The default: case catches bad input.

There is no need for a return statement in a void function, unless you wish to return early.

Otherwise if you ever use a do loop, put the while condition on the same line as the closing brace. This is so it doesn't look like a while loop with a null statement.

I am not a fan of do loops either: I find the need for them is fairly rare and they can be error prone. Just because they always execute once should not be the sole reason to use them. All 3 types of loop can be converted from one to another, maybe at the price of an extra variable.

@mgoetschius

That's wrong, and shows how confusing these things are. I won't describe how to do that type of statement, because that might encourage someone else to use it. Personally, I think that those who teach this non-sense need their collective asses kicked: preferably with steel capped boots; or until the kicker has webbed feet !! I mean that in the nicest possible and comedic way :+)
Thanks for the constructive criticism, Ill keep that in mind next time I make another program.

However, I tried what you said and I still am coming up with an infinite loop when keying in a letter when its asking for a number.

shouldn't line 29 be
F = getfarenheit(); instead of just
getfarenheit();?

Same for the celsius function.
Sorry, I skipped over what you mentioned about lines 81 and 100.

If one enters a char when a double is expected, then the cin will be in a failed state, and problems continue from there. Have a look at this: http://stackoverflow.com/questions/22573235/how-can-i-avoid-bad-input-from-a-user

Also there are problems with all your functions. With the display functions, they need to be sent an argument that can be printed. At the moment they print an un-initialised local value. With the get functions, they don't do anything with the value returned - you need to assign the value form the function to a variable.

Oh my god ! That article help me so much !!!

this is what I ended up using,

while(cin.fail())
{
double C;
cin.clear();
cin.ignore();
cout << "Enter in a number you fool!\n";
cin >> C;
}

and tell me if I am wrong but it goes like this,
while (cin.fail) // lets the computer know something wasn't right,
cin.clear(); // clears the input for C
cin.ignore(); // ignores the error that occured ?
cout <<""<<; //error message
cin >> C; // new input
fail refers to the stream being in a failed state.

clear clears the failed state

ignore ignores any characters still in the stream.

All these are detailed in the documentation. Have a look in the reference section top left of this page.
@Rawrr

Sorry if I unintentionally misled you.

@ TheIdeasMan

I did some research and I stand humbly corrected and just a bit smarter: I was wrong. Thanks for the correction. Although, I still find this confusing and agree with your proposed ass-kickings.
@mgoetschius

It's alright, you weren't the one teaching it :+D

I used to have a C programming teacher at Tech who actually had a steel capped work boot on his desk in the class. He mentioned that it was a "teaching aid" for when anyone had neglected to initialise a variable or divide by zero - that sort of thing. He also had a rubber stamp which had the image of a boot :+)

When we came into class after our first assignment had been marked, he was looking really serious with most of the assignments covered in red stamps. He asked in a menacing tone for some one to bring his boot over, ostensibly so he could put it on and start using it. The 18 year olds in the class were all packing themselves, I was older at the age of 25, so I jumped up and brought the boot over to him. Then he asked me what I thought about the whole situation? So I very lightly punched him on the shoulder and told I him I thought it was hilarious. We both LOL and it eased the tension quite a bit. But it was a good lesson for all, we knew not to do any bad practise.

I should probably have a number of "bruises" from some of the things I have said on this forum - lol
Topic archived. No new replies allowed.