Temperature 3q accepted as valid

Write your question here.
The problem with the following code is that when the user enters a temperature such as 8q instead of 86 or 3q instead of 30, the program interprets it as 8 or 3 and accepts it instead of displaying the error message. If the temperature is something like qq the program seems to work fine. Any ideas???

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


double ctof(double c)
{

    double f = (9.0/5*c)+32;
    return f;
}

double ftoc(double f)
{

    double c = (f  -  32)  *  5/9 ;
    return c;
}


int main()
{
    char scale = ' ';
    cout << "Enter c or f for Celcius or Farenheit: " << endl;
    cout << (char)26 << " ";
    cin >> scale;

    if (scale == 'c')
    {
    double c = 0;
 //   cin >> c;


    while(!(cin >> c)) {
    string garbage;
    cin.clear();
    getline(cin,garbage);
    cout << "Invalid temperature. "
            << "Enter Numeric value for temperature:" << endl;
    }

    double f = ctof(c);
    cout << f << endl;
    }
    else
        if (scale == 'f')
    {
        double f = 0;
        //cin >> f;

        while(!(cin >> f)) {
            string garbage;
            cin.clear();
            getline(cin,garbage);
            cout << "Invalid temperature. "
                 << "Enter Numeric value for temperature:" << endl;
        }

        double c = ftoc(f);
        cout << c << endl;
    }
}
Basically there are two types of inputs in C++: formatted and unformatted.
http://cplus.about.com/od/learning1/ss/clessontwo_7.htm

Since 86 and 30 are numbers they behave as you would expect. What would you expect the program to do if it is doing a formatted integer input operation and the input was '8q' or '3q'? 'q's are not numbers, and thus are discarded by the formatted input operation.
Why would the user of a temperature conversion program be entering 'q's?
Thank you.
Topic archived. No new replies allowed.