NEED HELP PLEASE!!!

Write a program to assign to, and later output to the screen, a string representing a value. The value, in the range of [0, 9], is read, from a file. For example, a value of 4 causes the string to be assigned four.
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

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int value;

int main() {

	ifstream inData;
	
	inData.open("Value.txt");

	inData >> value;

	if (value = 1) {
		cout << "one" << endl; }
	
	else if (value = 2) {
		cout << "two" << endl; }
	
	else if (value = 3) {
		cout << "three" << endl; }
	
	else if (value = 4) {
		cout << "four" << endl; }
	
	else if (value = 5) {
		cout << "five" << endl; }
	
	else if (value = 6) {
		cout << "six" << endl; }
	
	else if (value = 7) {
		cout << "seven" << endl; }
	
	else if (value = 8) {
		cout << "eight" << endl; }
	
	else (value = 9); {
		cout << "nine" << endl; }

	cout << endl;

	system("PAUSE");
	return 0;
}
  


As it is, When I've written a "4" to the file Value.txt located in the project directory, I get "fournine" printed to the screen. Why?

Also, I realize I have thus far ignored the string part of this question. I though setting it up this way first might help me better understand how to go about building the program. I didn't, hence me posting here. PLEASE, I NEED HELP.
Last edited on
closed account (2UD8vCM9)
One issue is you are not checking by using =, you need to use ==

ex.

 
if (value == 1)


Also
1
2
else (value = '9'); {
cout << "nine" << endl; }


what is this?

Why not just do

1
2
else if (value == 9) {
cout << "nine" << endl; }
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

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int value;

int main() {

	ifstream inData;
	
	inData.open("Value.txt");

	inData >> value;

	if (value == 1) {
		cout << "one" << endl; }
	
	else if (value == 2) {
		cout << "two" << endl; }
	
	else if (value == 3) {
		cout << "three" << endl; }
	
	else if (value == 4) {
		cout << "four" << endl; }
	
	else if (value == 5) {
		cout << "five" << endl; }
	
	else if (value == 6) {
		cout << "six" << endl; }
	
	else if (value == 7) {
		cout << "seven" << endl; }
	
	else if (value == 8) {
		cout << "eight" << endl; }
	
	else (value == 9); {
		cout << "nine" << endl; }

	cout << endl;

	system("PAUSE");
	return 0;
}



Still gives me the same problem.

As for the '9', good eye, but that was more or less a mere type-o.

Thanks for the quick reply, btw
Last edited on
closed account (2UD8vCM9)
Re-read my post. You skipped the second half of it.

Your use of else is wrong.
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

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int value;

int main() {

	ifstream inData;
	
	inData.open("Value.txt");

	inData >> value;

	if (value == 1) {
		cout << "one" << endl; }
	
	else if (value == 2) {
		cout << "two" << endl; }
	
	else if (value == 3) {
		cout << "three" << endl; }
	
	else if (value == 4) {
		cout << "four" << endl; }
	
	else if (value == 5) {
		cout << "five" << endl; }
	
	else if (value == 6) {
		cout << "six" << endl; }
	
	else if (value == 7) {
		cout << "seven" << endl; }
	
	else if (value == 8) {
		cout << "eight" << endl; }
	
	else if (value == 9); {
		cout << "nine" << endl; }

	cout << endl;

	system("PAUSE");
	return 0;


Same Difference

Also, if my use of "else" is wrong, why would this code work perfectly?:

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

// This program is inteded to assign an alphabetic value to its corresponding numeric score and write the result to a file

#include <iostream>
#include <fstream>
using namespace std;

int score; // Numeric value
char grade; // Alphabetic value assigned to a given numeric value

int main() {

	cout << "Please enter a score: "; // Prompts user to enter a score
	cin >> score;

	cout << endl;

	// If-else statement for calculating a score's corresponding grade
	if (score >=80) {
		grade = 'A'; }
	else if (score >=70) {
		grade = 'B'; }
	else if (score >=60) {
		grade = 'C'; }
	else if (score >=50) {
		grade = 'D'; }
	else  {
		grade = 'F'; }

	ofstream outData;

	outData.open ("Grade.txt"); // Creates a file named "Grade.txt"
	
	outData << "The grade given for a score of " << score << " is " << grade << endl; // Writes the users grade to the file "Grade.txt"

	outData.close();

	cout << "Your corresponding grade was written to the file Grade.txt" << endl; // Informs the user where they can find their grade

	cout << endl;

	system ("PAUSE");
	return 0;

closed account (2UD8vCM9)
YOU ARE NOT COPYING THE CODE I PASTED. IT'S NOT THE SAME DIFFERENCE. You're putting a semicolon after your condition. Your else if isn't checking anything, the code is just being executed regardless. Take out the semicolon in

 
else if (value == 9);


It should be

 
else if (value == 9)
Last edited on
Ok, my bad, you're right. That worked. Now, can you tell me why when I change it from int to string, it no longer works?

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

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

string value;

int main() {

	ifstream inData;
	
	inData.open("Value.txt");

	inData >> value;

	if (value == 1) {
		cout << "one" << endl; }
	
	else if (value == 2) {
		cout << "two" << endl; }
	
	else if (value == 3) {
		cout << "three" << endl; }
	
	else if (value == 4) {
		cout << "four" << endl; }
	
	else if (value == 5) {
		cout << "five" << endl; }
	
	else if (value == 6) {
		cout << "six" << endl; }
	
	else if (value == 7) {
		cout << "seven" << endl; }
	
	else if (value == 8) {
		cout << "eight" << endl; }
	
	else if (value == 9) {
		cout << "nine" << endl; }

	cout << endl;

	system("PAUSE");
	return 0;
}
because 1 != "1"

In fact, I'm not even sure if this compiles. Even if it does it won't execute as you expected because of the above.

If you changed it to
1
2
3
4
5
6
7
if (value == "1") {
  cout << "one" << endl;
} else if (value == "2") {
....
} else if (value == "9") {
  cout << "nine"<< endl;
}


Then it should work.
Wooooow... ok, I was at one point on the right track then, except I was using single quotes, as in:

1
2
3
4
5
6
7
8
9

if (value == '1') {
  cout << "one" << endl;
} else if (value == '2') {
....
} else if (value == '9') {
  cout << "nine"<< endl;
}


I remembering learning in a lecture something about strings with only one character needing 'single quotes' as opposed to a string with multiple characters using "double quotes". Clearly whatever I thought I was thinking of was wrong. Do you have any idea what my proff might actually have been referring to?

Btw, it did work. Thank you!
Last edited on
closed account (2UD8vCM9)
He was referring to characters.
ex.

char a = 'a';

1
2
string name="John"; //name = John
name[0] = 'Z'; //name = Zohn 
1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
int main(){	
	string s[4] = {"one","two","three","four"};
	int num = 4; 
	//cin >> num;		
	cout << "the number spells: " << s[num-1] << endl;	
return 0;
}
Riiiight. Thanks, Pindrought.

Not sure what you've got going on there, anup30. But thanks to you, too.
You know, I have to admit, learning to code C++ is difficult. It's literally like learning a second language. However, learning, understanding, and executing its concepts can be quite rewarding. Thanks again, gents.
Not sure what you've got going on there,


This thread is to see examples of other ways to do it.

http://www.cplusplus.com/forum/beginner/156414/

so, that's a different way to solve. typing if else for every number maybe tiresome. you can make it much shorter by string s[4] = {"one","two","three","four"}; way.
Topic archived. No new replies allowed.