Input test

Hello everyone,
I'm very new to programming and taking an intro course for fun. I have a project due tonight that I wanted to spice up a bit if I can. Here is my code:

//This program is designed to output which substances will either freeze or boil
//given a user generated temperature.

#include <iostream>

using namespace std;

int main()
{
//Temperature variable input from user.
int temp;

//Display of programs purpose.
cout << "Welcome to the Freeze & Boil Program!\n";
cout << "This program will take any temperature you type in\n";
cout << "and display what substances on record will freeze or boil\n";
cout << "at that point.\n" << endl;

//Program prompts user to input a temperature to be tested.
cout << "Please enter a temperature in degrees Farenheit:\n";
cin >> temp;

//Program processes given temperature so it may output each corresponding substance.
if
cout << "That is not a valid temperature. Please try again.\n";

cout << endl << "Subsances that will freeze:\n";
if (temp <= -173)
cout << "Ethyl Alcohol\n";

if (temp <= -38)
cout << "Mercury\n";

if (temp <= -362)
cout << "Oxygen\n";

if (temp <= 32)
cout << "Water\n";

cout << endl << "Substances that will boil:\n";
if (temp >= 172)
cout << "Ethyl Alcohol\n";

if (temp >= 676)
cout << "Mercury\n";

if (temp >= -306)\
cout << "Oxygen\n";

if (temp >= 212)
cout << "Water\n";

cout << endl << "Thank you for using the Freeze & Boil Program!" << endl;

cin.get();

return 0;
}

I know it's a very simple and common program. So far The program works correctly (at least with the tests I've attempted) but I wanted to add to it the ability to catch any input that isn't a number. Such as a-z or A-Z. You can see I've started the If function with a complementing cout statement.

Keeping in mind I'm still a novice is there an easy way for me to code in what I need. I know it's possible but I'm trying to stay close to the beginner level.
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
//This program is designed to output which substances will either freeze or boil
//given a user generated temperature.

#include <iostream>

// using namespace std;

int main()
{
    //Temperature variable input from user.
    int temp;

    //Display of programs purpose.
    std::cout << "Welcome to the Freeze & Boil Program!\n"
                 "This program will take any temperature you type in\n"
                 "and display what substances on record will freeze or boil\n"
                 "at that point.\n\n" ;
                 // note: these four line (note: there is no semicolon at the end of the first three)
                 // are concatenated into a single literal c-string by the preprocessor

    //Program prompts user to input a temperature to be tested.
    std::cout << "Please enter a temperature in degrees Farenheit: ";

    if( std::cin >> temp ) // if input was successful (user entered a number)
    {
        //Program processes given temperature

        std::cout << "\nSubsances that will freeze:\n";
        if ( temp <= -173 ) std::cout << "Ethyl Alcohol\n";
        if ( temp <= -38 ) std::cout << "Mercury\n";
        if ( temp <= -362 ) std::cout << "Oxygen\n";
        if ( temp <= 32 ) std::cout << "Water\n";

        std::cout << "\nSubsances that will boil:\n";
        if ( temp >= 172 ) std::cout << "Ethyl Alcohol\n";
        if ( temp >= 676 ) std::cout << "Mercury\n";
        if ( temp >= -306 ) std::cout << "Oxygen\n";
        if ( temp >= 212 ) std::cout << "Water\n";
    }

    else // user did not enter a number; input failed
        std::cout << "That is not a valid temperature.\n";
}
Note that JLBorges' suggestion is a simplified, partial solution. It will ensure than you can extract an int value from the input string but not that the string is just an integer value. For example:

174 will be extracted from the string 174cm
3 will be extracted from the string 3.1415

Maybe this is sufficient for now. But if you're interested, cire gave a more complete solution to this problem here:

Trying to limit input to int type
http://www.cplusplus.com/forum/beginner/108849/#msg592118

which rejects any string which is not just a number.

A simplified form of the code is (cire's version is a template function and uses a validation function) :

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

int getInt(const string& prompt) {
    int result = 0;

    bool done = false;
    while (!done) {
        cout << prompt;

        // get a a whole line from the user
        string line;
        getline(cin, line);

        // use it to init a stringstream
        istringstream is(line);

        // try to read a number from the stringstream...
        // if a number can't be read into result
        // or there is more than white space in the string after the number
        // then fail the read and get another string from the user
        // (so "abc", "42nd St", "1 2 3", etc are all invalid.)
        char dummy = '\0';
        if (!(is >> result) || (is >> ws && is.get(dummy)))
            cout << "Invalid input. Try again!\n";
        else
            done = true ;
    }

    return result;
}

int main() {
    int result = 0;

    do {
        result = getInt( "Enter a number (0 to exit): ");
        cout << "Your number is : " << result << '\n';
    } while(0 != result);

    return 0;
}


Enter a number (0 to exit): 12
Your number is : 12
Enter a number (0 to exit): 99 Red Balloons
Invalid input. Try again!
Enter a number (0 to exit): 174cm
Invalid input. Try again!
Enter a number (0 to exit): 3.1415
Invalid input. Try again!
Enter a number (0 to exit): 42
Your number is : 42
Enter a number (0 to exit): 0
Your number is : 0


Andy
Last edited on
Dupe post!!!

Here...

And there...

Input test
http://www.cplusplus.com/forum/general/166564/

Andy
Topic archived. No new replies allowed.