Error in my program

I keep getting an error for undeclared choice and cin, I don't understand what to do?

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
#include <iostream>
using std::cout;
using std::endl;

int main() {
	char choie;  // for the user's input

	cout << "Do you like"
		" green eggs and ham?" ""
		<< "Enter 'y' or 'Y' for"
		" yes, 'n' or 'N' for no: ";
	cin >> choice;

	cout << endl;


	// respond to the user's choice!

	if (choice == 'y' || 'Y')
		cout << "Me too!\n";
	else if (choice == 'n' || 'N')
	cout << "Me neither?\n";
	else {
		cout << "I don't understand :(\n";

		cout << endl;


		// analyze the user's choice

		switch (choice) {
		case 'y':
		case 'n': cout << "You entered a lower case letter.\n"; break;
		case 'Y':
		case 'N': cout << "You entered an upper case letter.\n"; break;
		default: cout << "You entered an invalid letter!\n"; break;
		}

		cout << endl;


		// print the letters from 'A' or 'a' to the user's choice, or exit if the
		// user's choice was invalid

		char start;  // the character to begin printing from

		if (choice == "Y" || "N") {
			start = 'A';
		}
		else if (choice == "y" || "n") {
			start = 'a';
		}
		else {
			cout << "I am unhappy. Terminating.\n";
			return 1;  // error
		}

		cout << "The letters from '" << start << "' to '" << choice << "' are:\n";

		for (char c = start; c <= choice; c++)
			cout << c << " ";

		cout << endl;


		return 0;  // success
	}
Last edited on
1
2
3
4
5
6
7
8
9
// Notice the spelling here
char choie;  

	cout << "Do you like"
		" green eggs and ham?" ""
		<< "Enter 'y' or 'Y' for"
		" yes, 'n' or 'N' for no: ";
        // And the spelling here.
	cin >> choice;


As for std::cin not defined it is because you don't have a using statement for it. You have the using declarations for cout and endl but not cin. So you will either need to use std::cin or add using std::cin;
Last edited on
also you need using std::cin;

and if (choice == 'y' || 'Y') should be more like if (choice == 'y' || choice == 'Y')
Ok, I'm getting some other errors now.
I have to print the letters from 'A' or 'a' to the user's choice, or exit if the
user's choice was invalid
I'm keep getting an error for a brace but I can't find it.

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

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main() {
	char choice;  // for the user's input


	// prompt for the user's choice
	// - in this small block specifically, some things are unnecessary (and
	//   therefore not the best coding practice).  but do they cause compiler
	//   errors? :)

	cout << "Do you like"
		" green eggs and ham?" ""
		<< "Enter 'y' or 'Y' for"
		" yes, 'n' or 'N' for no: ";
	cin >> choice;

	cout << endl;


	// respond to the user's choice!

	if (choice == 'y' || choice == 'Y')
		cout << "Me too!\n";
	else if (choice == 'n' || choice == 'N')
	cout << "Me neither?\n";
	else {
		cout << "I don't understand :(\n";

		cout << endl;


		// analyze the user's choice

		switch (choice) {
		case 'y':
		case 'n': cout << "You entered a lower case letter.\n"; break;
		case 'Y':
		case 'N': cout << "You entered an upper case letter.\n"; break;
		default: cout << "You entered an invalid letter!\n"; break;
		}

		cout << endl;


		// print the letters from 'A' or 'a' to the user's choice, or exit if the
		// user's choice was invalid

		char start;  // the character to begin printing from

		if (choice == 'Y' || choice == 'N') {
			start = 'A';
		}
		else if (choice == 'y' || choice == 'n') {
			start = 'a';
		}
		else {
			cout << "I am unhappy. Terminating.\n";
			return 1;  // error
		}

		cout << "The letters from '" << start << "' to '" << choice << "' are:\n";

		for (char c = start; c <= choice; c++)
			cout << c << " ";

		cout << endl;

		return 0;  // success
	}
Last edited on
Line 31 else { remove this brace
ok thanks
Topic archived. No new replies allowed.