Console Input

I want to make this work:

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
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()

{
	cout << "THE FAHRENHEIT TO CELSIUS CONVERSION PROGRAM\n"
		<< "Which temperature do you want to convert to which?\n"
		<< "(C for Celsius, F for Fahrenheit)\n";

	char C;
	char F;
	char t;
	cin >> t;
		if (t == C)
		{
			cout << "Enter Celsius temperature...\n";
			int Ctemp;
			cin >> Ctemp;
			cout << "The equivalent in Fahrenheit is:" << Ctemp * 1.8 + 32;
		}
		else if (t == F)
		{
			cout << "Enter Fahrenheit temperature...\n";
			int Ftemp;
			cin >> Ftemp;
			cout << "The equivalent in Celsius in:" << (Ftemp - 32) / 1.8;
		}
	
	cout << "Type anything to continue...";
	
	cin.get();
	return 0;
}


What I don't know how get the program to do is recognize the console input. Every time I run the program it gives me an error after I type something. Everything else should work fine, but I'd prefer to let the user type letters rather than numbers, which most F to C converters in C++ let you do.
Last edited on
Try this:

if (t = C)

I don't have a compiler and I'm really tired, so it's only a thought.
Last edited on
No, don't do that.

Your problem is you are mixing up the concept of literals and variables.

1
2
3
4
5
6
	char C; // this creates a variable named C, and doesn't assign anything to it
	char t;  // this creates a variable named t
	cin >> t;  // this takes input from the user and assign it to t
 // at this point, C is still unassigned
		if (t == C) // this compares two variables, t and C.
// Since you never said what C contains, this line basically is meaningless. 


This is similar to saying something like this:

1
2
3
4
5
6
7
8
9
10
// I want to add 5 + 3 and put it in a variable named 'foo'
// the proper way:

int foo = 5 + 3;

// what you're doing:
int five;
int three;

int foo = five + three;  // ??? wtf ??? 


This of course doesn't work because five doesn't necessarily contain the number 5. Likewise three likely doesn't container the number 3.

5 and 3 are literals. They have an actual value of 5 and 3. five and three are variables. They can have any value that you assign them. The fact that they are named five and three means nothing.

1
2
3
4
5
6
int five = 10;  // the variable 'five' contains the number 10

if(five == 5)
{
  // this block will not execute because five == 10, not 5
}



Long story short, C and F should not be variables, but they should be literals:

1
2
3
4
5
//	char C;  // get rid of these
//	char F;
	char t;
	cin >> t;
		if (t == 'C') // use the literal 'C' 
Topic archived. No new replies allowed.