using static_cast

Hi all,
This is supposed to be a problem to convert temps, and I am supposed to use static_cast, but I'm missing something here. The math is not coming out correct.
Any and all help is greatly appreciated!!

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
/*************
Program:	Asg_11.cpp
Author: 	Alan P. Matie                     
Date:       20 Apr 2009
Description: 
	A program to convert Fahrenheit to Celsius, or Celsius to Fahrenheit.
New Concepts:
Challenges:
Last Modified:27 Apr 2009
**************/

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

// Declare constants

// Function Prototypes


int main()
{
	// Declare variables below here
	double temp, result;
	char Utemp;
	
	// Initialization Section for real number output. DO NOT MOVE!
	cout <<setiosflags(ios::fixed | ios::showpoint);
	cout <<setprecision(4);
	// Begin your "main processing" below here
	cout << "Welcome to your temperature conversion program!";
	do
	{
		cout << "Please choose from the following prompts"<<endl;
		cout << "Enter F to convert Fahrenheit to Celsius"<<endl;
		cout << "Enter C to convert Celsius to Fahrenheit"<<endl;
		cout << "Enter Q to quit"<<endl;
		cin >> Utemp;
		cout << "Please enter the temperature to be converted ==>";
		cin >> temp;
		switch (Utemp)
		{
		case 'Q':
			break;
		case 'q':
			break;
		case 'F':
			result = (temp - 32) * static_cast<double>(5/9);
			cout <<"the answer is: " <<result <<endl;
			break;
		case 'C':
			result = temp * static_cast<double>(9%5) + 32;
			cout <<"the answer is: " <<result <<endl;
			break;
		default:
			cout <<"Program will terminate!"<<endl;
			break;
		}
	}while (Utemp != 'Q');
		cout << "End of Program!";
		

	

	
	return 0;
}

// function definitions below here 


I think that 5/9 on line 50 will evaluate to an integer. Try typecasting or changing it to 5.0/9.

Also, notice the (accidental?) use of the modulo operator on line 54.
Right, the following code will cast the result of the division 5/9 to 0.0.
static_cast<double>(5/9)
Thank You,
The math works now but my loop isn't ending like I thought it should. Any suggestions? It asks for the number before it ends, shouldn't it end as soon as I enter Q?
Go through your do... while loop one step at a time. You'll see that temp is inputted immediately after Utemp,
but the decision to exit the program isn't made until the very end of the loop. If you want it to end as soon as you enter 'Q' then you need to change the ordering within the do... while loop.

Edit:

Just formatting.... Does anyone know why word wrapping sometimes does not work?
Last edited on
Go through your do... while loop one step at a time. You'll see that temp is inputted immediately after Utemp,
but the decision to exit the program isn't made until the very end of the loop. If you want it to end as soon as you enter 'Q' then you need to change the ordering within the do... while loop.


Ok, I see what you're getting at, but honestly don't see how to change it to make it work. Could you point me in the correct direction?
Ok, I see what you're getting at, but honestly don't see how to change it to make it work. Could you point me in the correct direction?


You need to rewrite the loop. First you ask the user to enter a letter. Then you ask the user to enter the temperature and you are wondering why the user is being asked for the temperature prior to the loop ending. You obviously need to use if statements to test the character prior to asking the user the next question. The empty case statements for 'Q' and 'q' are pointless. There are many ways you could fix the problem.
Topic archived. No new replies allowed.