Basic array

Pages: 12
Wow. Actually, it is wrong. You get some warnings, but this works (reason, strings are the same as char str[] and everything ultimately equates to integral data):
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
#include <iostream>
#include <string>

int main()
{
	char str[2][50] = {{"Camera\0"}, {"Tripod\0"}};
	std::string name[3] = {{"Dog"}, {"Cat"}, {""}};
	for (int i = 0; i < 3; i++)
	{
		switch(str[i][0])
		{
			case 'C':
				std::cout << "Camera\n";
				break;
			case 'T':
				std::cout << "Tripod\n";
				break;
			default:
				std::cout << "No data\n";
				break;
		}
	}
	
	for (int i = 0; i < 3; i++)
	{
		switch(name[i][0])
		{
			case 'D':
				std::cout <<"Dog\n";
				break;
			case 'C':
				std::cout << "Cat\n";
				break;
			default:
				std::cout << "No data\n";
				break;
		}
	}
        return 0;
}
You aren't doing a switch on a string, you're doing a switch on a character.

And my warnings come from the way you're initializing your variables.
Last edited on
LowestOne wrote:
You aren't doing a switch on a string, you're doing a switch on a character.

And my warnings come from the way you're initializing your variables.

It is a character in a string, but it shows you can in fact use strings with switchs, which you said you couldn't. Yes, I know what the warnings were, but I wasn't worried about that as I knew it would still function like I intended it to.

If a character is a byte and a string is a sequence of characters, then you can not use a string in a switch. If you want to call a character a string, then by all means, use a string in a switch.
Last edited on
Sorry I foolishly thought that since you were pulling the character out of the string that it was still using a string in switch statements. You are right, you can't use string in switch, but you can use strings and use a character from the string in switch (or c-string for that matter) for say File menus in a menu system (F)ile (O)pen, etc. Again sorry, I made the wrong assumption.
I still do not know how I would make a sort of search engine.
Topic archived. No new replies allowed.
Pages: 12