Repeating text if statement?

Hello,

I am trying to create a program that will rate certain values with a specific number. However, there are about 200 different items that need to be rated and they are text values.

In my code below, I rate the first item "rateit1" and the if statement will print the appropriate result as necessary.

My problem is that I would need to copy paste this code 200 times changing the variable "rateit1" to "rateit2", "rateit3". etc... for each time I am copy pasting it.

There has to be a better way to code this no? As I understand while, do and for loops they only work with numbers. Is there some way to do this with text values?

Any help is appreciated.

Thanks,
Raphael



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
int main()
{
	system("TITLE");
	
	int zero (0);
	int one (1);
	int two (2);
	int three (3);
	int four (4);
	int five (5);
	int six (6);
		
	cout << endl;
	cout << "Zero  = " << zero << endl;
	cout << "One   = " << one << endl;
	cout << "Two   = " << two << endl;
	cout << "Three = " << three << endl;
	cout << "Four  = " << four << endl;
	cout << "Five  = " << five << endl;
	cout << "Six   = " << six << endl;
	cout << endl;
	cout << endl;
	
	int rateit1;
	cout << "Rate the value A: ";
	cin >> rateit1;
	cout << endl;

	int rateit2;
	cout << "Rate the value B: ";
	cin >> rateit2;
	cout << endl;

	if (rateit1 == zero)
	{
		cout << "A = Zero";
	}
	else
	{
		if (rateit1 == one)
		{
			cout << "A = One";
		}
		else
		{
			if (rateit1 == two)
			{
				cout << "A = Two";
			}
			else
			{
				if (rateit1 == three)
				{
					cout << "A = Three";
				}
				else
				{
					if (rateit1 == four)
					{
						cout << "A = Four";
					}
					else
					{
						if (rateit1 == five)
						{
							cout << "A = Five";
						}
						else
						{
							if (rateit1 == six)
							{
								cout << "A = Six";
							}
						}
					}
				}
			}
		}
	}

	return 0;
}
As I understand while, do and for loops they only work with numbers. Is there some way to do this with text values?


Loops aren't limited in this way. They are control structures that allow you to perform a number of iterations, they don't care what data they're using.

Speaking of which, you shouldn't care about what data you're using, either. I'm referring to your use of a logic-driven design as opposed to a data-driven design, which is most intuitive for beginners, but not practical for a lot of applications. When I say "logic-driven", in your case, I mean that for each condition there exists another branch of code, which, as you noticed, becomes overwhelming quickly.

Not exactly sure what you're trying to do, but perhaps this will help. I've tried to mimic what I think is the intended behavior of your program. I realize the following snippet may contain some concepts you don't understand yet, so feel free to ask questions - ignore the lack of error checking:

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
#include <iostream>
#include <string>

int main() {

	const int num_chars = 5;//The number of characters to rate

	const int min_rating = 0;//Smallest rating we can give
	const int max_rating = 9;//Largest rating we can give

	char chars[num_chars] = {'A', 'B', 'C', 'D', 'E'};//An array of characters we want to rate

	int ratings[num_chars] = {0};//Our ratings, one for each character

	std::string number_strings[max_rating + 1] = {
		"zero",
		"one",
		"two",
		"three",
		"four",
		"five",
		"six",
		"seven",
		"eight",
		"nine"
	};

	for (int i = 0; i < num_chars; ++i) {
		std::cout << "Rate character " << chars[i] << " (" << i+1 << "/" << num_chars << "):\t";
		do {
			std::cin >> ratings[i];
		} while (ratings[i] < min_rating || ratings[i] > max_rating);
	}

	std::cout << std::endl;

	for (int i = 0; i < num_chars; ++i) {
		std::cout << chars[i] << ":\t" << number_strings[ratings[i]] << std::endl;
	}

	//Pause

	return 0;
}


Ideally, one should strive to write code which scales well, which, in your case means that you should be able to change the number of characters you're rating on the fly without having to rewrite your whole program. Your code shouldn't care about each and every permutation of every path of every condition, or how much data it's processing. Learning to write code in a data-driven design requires you to think more abstractly.
Last edited on
Having looked at your code I can't imagine what the purpose would be, maybe because it's not finished, and there are no comments anywhere. So I'm going to suggest you rethink your problem.

What your asking has been done already, I see no reason to reinvent the wheel.

Check out http://www.asciitable.com/.

If I understand you want to associate a letter to a number, then In short, all keys that can be produced on a keyboard are already "rated" 0-127. Most likely You will only need to use numbers 32 and up.

In case you haven't seen that info before,
0-9 are 48-57
A-Z and a-z are 65-90 and 97-122 depending on if they are upper or lower case.

Thank you so much for your help!

xismn: Your code does almost exactly what I need it to, except that I was just using A, B, C as examples. They need to be full words. When I try to substitute A for Apple (for example). It's telling me that there are too many characters in the constant.

I only barely understand char, but is it possible to substitute the letters with strings somehow? I've been messing with it, but without success. Creating a string first and then inserting into the array does not seem to work.

The idea is that users can rate certain items (for the sake of continuity, lets say apples).

So we have Macintosh, Gala, Golden Delicious, Granny Smith, etc...

Each of them need to be rated on a scale of 0-6.

Eventually, I want to be able to compare two (or more) users' input. User 1 rates 200 types of apples, User 2 rates 200 types of apples and then the program displays their ratings side by side.

I am only beginning to learn C++ so I'd like to do one thing at a time, but I figured I'd explain what the end goal is.

I've modified your code a little (hope you don't mind), to maybe help make it more clear what I am trying 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
#include <iostream>
#include <string>

using namespace std;

int main() {

	const int num_chars = 5;// what should this number be? The number of items, or the total number of characters in the entire array?

	const int min_rating = 0;//Smallest rating we can give
	const int max_rating = 6;//Largest rating we can give
	
	string macintosh;
	
	char chars[num_chars] = {macintosh, 'Gala', 'Golden Delicious', 'Granny Smith', 'Fuji'}; //Can this array consist of strings?

	int ratings[num_chars] = {0}; //Our ratings, one for each character

	string number_strings[max_rating + 1] = {
		"zero",
		"one",
		"two",
		"three",
		"four",
		"five",
		"six",
		"seven",
		"eight",
		"nine"
	};

	for (int i = 0; i < num_chars; ++i) {
		cout << "Rate character " << chars[i] << " (" << i+1 << "/" << num_chars << "):\t";
		do {
			cin >> ratings[i];
		} while (ratings[i] < min_rating || ratings[i] > max_rating);
	}

	cout << std::endl;

	for (int i = 0; i < num_chars; ++i) {
		cout << chars[i] << ":\t" << number_strings[ratings[i]] << std::endl;
	}

	//Pause

	return 0;
}
Hi again thecrazygerman,

I see! Now that I know what you're trying to achieve, it's much easier to be specific, thanks for clarifying :)

You're welcome to use any part of my code. I've posted my own code again with some changes and comments to help you:

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
#include <iostream>
#include <string>

int main() {

	const int num_apples = 5;//This number represents the number of apples you wish to rate. We want to rate five apples.

	const int min_rating = 0;//Smallest rating we can give.
	const int max_rating = 6;//Largest rating we can give.


	//Now, we declare and define an array of strings. Let's call it apple_names.
	//Each element of the array is a string, which represents the name of our available apples - we have five apples, so this array will also have five strings.
	std::string apple_names[num_apples] = { "Macintosh", "Gala", "Golden Delicious", "Granny Smith", "Fuji" };
	//Note, that double quotes ("hello world") are for strings, and single quotes ('a') are for single characters.

	int ratings[num_apples] = { 0 };//Our ratings, one for each apple. Each rating is initialized to zero.

	//Next, another string array. These strings will be used as our ratings.
	std::string number_strings[max_rating + 1] = {
		"zero",
		"one",
		"two",
		"three",
		"four",
		"five",
		"six"
	};

	for (int i = 0; i < num_apples; ++i) {//For each apple...
		std::cout << "Rate apple \"" << apple_names[i] << "\" (" << i + 1 << "/" << num_apples << "):\t";
		do {
			std::cin >> ratings[i];//Enter rating.
		} while (ratings[i] < min_rating || ratings[i] > max_rating);//If the rating is invalid, enter rating again.
	}

	std::cout << std::endl;

	for (int i = 0; i < num_apples; ++i) {//For each apple...
		std::cout << apple_names[i] << ":\t" << number_strings[ratings[i]] << std::endl;//Print the name and the rating next to it.
	}

	return 0;
}


The only other things left to do would be to align everything into nice columns , and of course to enable several people to give ratings for each apple - and then to display those results.
double quotes ("hello world") are for strings, and single quotes ('a') are for single characters.


Ahhh! I was so close! Haha!

Thank you so much! This helped immensely!

The only other things left to do would be to [...] and of course to enable several people to give ratings for each apple - and then to display those results.


Yes, and I think I can do that using another loop. But I'll research and mess around with that on my own to see if I can figure it out!

Thank you again!
Topic archived. No new replies allowed.