String Comparing problem... I don't know why it's not working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "stdafx.h"
#include <iostream>

using std::cin;
using std::cout;
using std::endl;


int main ()
{
	char key[] = "apple";
	char input[99];

	cout << "Guess my favourite fruit? ";
	cin >> input;

	if(key == input)
	{
		cout << "correct";
	}
	else 
		cout << "wrong" <<endl;
return 0;
}


I don't know why it's not saying correct when I enter "apple"

I used this post to help me write this:
http://www.cplusplus.com/forum/beginner/3238/

Mitsakos (320) Jul 28, 2008 at 5:35pm
try:
if(pick == "withdrawl"){
you can aply directly the operators +, >=, ==, <= etc to strings...
If you want to use strcmp() use pick.c_str()
strcmp() works only with c type strings.
You can use == directly on strings, but in your example, you are not using strings; just C-style char arrays.

For char arrays (C-style strings):
strcmp(array1, array2)

For std::strings (C++ strings):
string1 == string2

If you want to use ==, make key and input of type std::string, and change the line 15 to getline(cin, input);. (cin does not work well will strings.)
Last edited on
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Lab1 Exercise 3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>

using std::cin;
using std::cout;
using std::endl;

int _tmain(int argc, _TCHAR* argv[])
{
	double celcius, fahr, counter, stemp, etemp, inc;
	char celkey[] = "celcius";
	char fahrkey[] = "fahr";
	char CoFinput[255];
	//declare doubles and chars

	cout << "Temperature Conversion Table Software:" << endl << endl << "Do you want to convert Celcius Temperatures or a Fahrenheit Temperatures?";
	cout << endl << "Enter \"celcius\" or \"fahr\": ";
	cin >> CoFinput;
	//ask users for conversion style

	for (counter = 1; counter != 0;) //for loop used to test whether if it's Celcius Fahrenhiet or an invalid input
	{

		if(strcmp(CoFinput, celkey) == 0 || strcmp(CoFinput, fahrkey) == 0) //if user input for CoFinput is celsius or fahr, it runs the if else statement below
		{
			if(strcmp(CoFinput, celkey) == 0) //if it's celcius, it runs the code below
			{	//the code below ask for the starting and ending temperature
				cout << "Enter the starting temperature: ";
				cin >> stemp;
				cout << "Enter the ending temperature: ";
				cin >> etemp;
				while (stemp > etemp) //this while loop checks starting and ending temperature mistakes
				{
					cout << "Enter a starting temperature that is lower than the ending temperature." << endl;
					cout << "Enter the starting temperature: ";
					cin >> stemp;
					cout << "Enter the ending temperature: ";
					cin >> etemp;
				}
				//the code below ask for hte increment to be added
				cout << "Enter the incremental factor (do not use a value <= 0): ";	
				cin >> inc;
				while (inc <= 0) //this while loop checks to be sure the increment isn't a negative or zero
				{
					cout << "Enter a valid value for the incremental factor: ";
					cin >> inc;
				}

				celcius = stemp; // the computing will use the starting temp

				cout << "\nCelcius Temperature \t \t Fahrenheit Temperature \n";
				cout << "_______________________________________________________\n";
				
				while (celcius <= etemp) //the while loop will continue to do the converting until the temperature goes pass the ending temperature
				{
					fahr = celcius * (9.0/5.0) + 32;
					
					cout << celcius << "\t \t \t \t " << fahr << endl;

					celcius = celcius + inc;
				}
	
				counter = 0;
			}
			if(strcmp(CoFinput, fahrkey) == 0) //read the comments above to understand the following code, it's the same thing
			{
				cout << "Enter the starting temperature: ";
				cin >> stemp;
				cout << "Enter the ending temperature: ";
				cin >> etemp;
				while (stemp > etemp)
				{
					cout << "Enter a starting temperature that is lower than the ending temperature." << endl;
					cout << "Enter the starting temperature: ";
					cin >> stemp;
					cout << "Enter the ending temperature: ";
					cin >> etemp;
				}
				
				cout << "Enter the incremental factor (do not use a value <= 0): ";
				cin >> inc;
				while (inc <= 0)
				{
					cout << "Enter a different value for the incremental factor: ";
					cin >> inc;
				}

				fahr = stemp;

				cout << "\nFahrenheit Temperature \t \t Celcius Temperature \n";
				cout << "_______________________________________________________\n";
				
				while (fahr <= etemp)
				{
					celcius = (5.0/9.0) * (fahr-32);
					
					cout << fahr << "\t \t \t \t " << celcius << endl;

					fahr = fahr + inc;
				}
	
				counter = 0;
			}
		}

		else //this ask the user to input the correct string, "celcius" or "fahr"
		{
			cout << endl << "Invalid Input! Please enter a valid string." <<endl;
			cout << "Enter \"celcius\" or \"fahr\": ";
			cin >> CoFinput;
		}

	}

	return 0;
}


thx, zhuge helped me fix the code and I finished my HW. I only have one problem that I can't seem to understand. If I ask it to start at 0 to 1 in increments of .1, it goes from 0 to 1, but if I ask it to do increments of .01, it goes from 0 to .99, not 0 to 1

why?
Last edited on
Your problem is floating point roundoff error because .01 is not exactly representable in IEEE floating point format with a finite number of bits.

Read http://docs.sun.com/source/806-3568/ncg_goldberg.html

and if you don't understand, then I suggest converting your for() loop so that it
uses an integer index instead of a double.
Topic archived. No new replies allowed.