My first useful program

So, I've just started programming in C++ (as probably evidenced below), this is my first useful program. It converts a user-inputted Celsius measurement into a Fahrenheit measurement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main(){
	int celsius, fahrenheit;
	cout << " This program converts celsius measurements to fahrenheit measurements." << endl;

	while(true){
	cout << "Please input the celsius measurement to convert: ";
	cin >> celsius;

	fahrenheit = celsius * 9/5 + 32;
	cout << endl <<"Your measurement in fahrenheit is: " << fahrenheit << endl;
	}

}
Last edited on
Awesome Job!!!
Nice
Nice job. Does it keep looping properly? I ask this because you are using cin and usually cin does not clear input buffer and the program should

Make a way for the user to terminate the program without having to send an eof signal.
Nice.
Thanks for all the replies. I don't know what you mean about having it loop properly, I've tested it a few times, but never done more then 4-5 conversions at a time. When I was writing this, I couldn't really see people using this for more then 1-2 conversions at a time, so I don't know if clearing the buffer would matter.
Not a major issue, but I'd suggest the use of type double rather than int here. Not only will that allow the input and output of decimal values, it also reduces rounding errors.

For example 17C = 62.6F but using all integer values, the output is just 62.
Ok, having taken into consideration what has been said before, I've changed a few things in the program, such as instead of using int I now use double, along with writing the code so that if the user inputs 0 for the degrees in Celsius, the program will quit, and I believe making the buffer clear in every iteration of the loop. Updated code is below.

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

int main(){
	double celsius, fahrenheit;
	cout << " This program converts celsius measurements to fahrenheit measurements." << endl;

	while(true){
	cout<< "To quit, input 0." << endl;
	cout << "Please input the celsius measurement to convert: ";
	cin >> celsius;

	fahrenheit = celsius * 9/5 + 32;
	cout << endl <<"Your measurement in fahrenheit is: " << fahrenheit << endl;
	cin.clear();
		
		if (celsius == 0){
			system("exit");
			return 0;
		}
	
	}

}
This is way better than my first "Hello World!" =)

FYI: the system call is not required, a simple return 0; is all you need to exit the program.

The "system" call will actually execute a shell command, not what you want to do. For more info type, "man 3 system" at the shell.

Try the following to see what happen, I assume you're using a unix system.
system("ls");

-- -
Rajinder Yadav <labs@devmentor.org>

DevMentor Labs
http://labs.devmentor.org
Creating Amazing Possibilities
Trust me, my first "Hello World" was bad, I forgot to add the #include <iostream> , and then had to spend 20 minutes trying to figure out why cout and cin didn't work. This is merely the first program i've made that did something remotely useful. Also, I don't have a Unix system, just Windows 7. Don't know how much that actually changes anything in relation to programming.
Last edited on
Also, i was just now dicking around in the program, basically trying to break it to find bugs, and I realized that if the user inputs a string when asked what measurement they want to convert, the program gets stuck in an infinite loop and continually outputs some random number that I can't read. My question is, is there anyway I can make it so that if the user inputs a string instead of an int, that the program clears the input, says that what was inputted was not valid, and asks them to re-enter the number?
Yea that is what I meant by it looping properly. I guess I should have mentioned improper input as well. Yes there is a way to fix this and it is done using stringstream. You need to include sstream library in your code to use it.

http://www.cplusplus.com/forum/beginner/92578/#msg497505
Last edited on
How to handle non-numeric keyboard input:
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
#include <iostream>
using namespace std;

int main()
{
    double celsius, fahrenheit;
    cout << " This program converts celsius measurements to fahrenheit measurements." << endl;

    while (true)
    {
        cout<< "To quit, input 0." << endl;
        cout << "Please input the celsius measurement to convert: ";

        while (!(cin >> celsius))   // check the input was valid
        {
           cin.clear();             // Reset error state flags
           cin.ignore(1000,'\n');   // Remove unwanted characters from input buffer
           cout << "Input not valid - please try again ";
        }

        fahrenheit = celsius * 9.0/5.0 + 32;
        cout << endl <<"Your measurement in fahrenheit is: " << fahrenheit << endl;

        if (celsius == 0){
            break;                  // Exit from the while loop.
        }

    }
    return 0;
}
You could instead of using zero to terminate, use any non-numeric input:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
    double celsius, fahrenheit;
    const char prompt[] = "\n\nPlease input the Celsius value: ";
    cout << " This program converts Celsius to Fahrenheit." << endl;
    cout<< " To quit, input a non-numeric value.\n" << prompt;

    while (cin >> celsius)
    {
        fahrenheit = celsius * 9.0/5.0 + 32;
        cout << "The value in Fahrenheit is:     " << fahrenheit << prompt;
    }

    return 0;
}
Last edited on
I tried the first option, and it worked. I didn't use the second option because I didn't want to close the program when someone enters non-numeric input, I just wanted to tell them that the input was not valid.
The choice is yours, that's fine. My only concern is that 0 is a valid temperature, so I'd prefer to use something like -999 which is not a valid temperature in either C or F.
That would be an easy fix though. I'm not trying to claim that the way I did it is the only way, or even the best way. That's the beauty with C++, multiple people can have multiple solutions to the same issue. If I (or someone who had the source code) decided to make -999 the exit input, they would only have to change one line of the code. The way I have it set up now is just the way that I want the program to operate.
Topic archived. No new replies allowed.