too many characters in constant?

Hi, so i was practicing c++ variables and switch statements today and I was almost positive I did this correctly. However, when i try to write in a 'case' for my switch statement, it said I used too many characters. You can clearly see that I entered how many characters it can hold in the code. Now i'm stumped, any help? (I used visual studio if that helps) Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <conio.h> 
using namespace std;

int main()
{
	char soda [30];
	
	cout << "enter what soda you want. We got sprite, fanta, coca cola, sams cola, and root beer." << endl;
	cin >> soda;
	
	switch (soda [30])
	case 'sprite':
	cout << "here's your sprite" << endl;


	getch();
	return 0;
}
Last edited on
You can only use integers with a switch.
well that's embarrasing. thanks though
for starters it appears that 'sprite' is too long. I am a beginnger myself but so far so good.
switching to integers looks like this
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
#include <iostream>
using namespace std;

int main()
{
	int soda = 0;
	int sprite = 0;
	int fanta = 0;
	
	cout << "Enter what soda you want. We got sprite, fanta, coca cola,\n";
	cout << "sams cola, and root beer." << endl;
	cin >> soda;
	cout << endl;
	{
	
	if ( soda == sprite )
		cout << "Here's your Sprite";
	else; 
	if ( soda == fanta )
		cout << "Here's your Fanta";
}
	


	return 0;
}


just finish the rest with what you want
Last edited on

what happen to your switch statement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int soda = 0;
	
cout << "Enter what soda you want. We got sprite, fanta, coca cola,\n";
cout << "sams cola, and root beer." << endl;

cin >> soda;
cout << endl;

switch( soda )
{
  case 0: cout << "Sprite"; break;
  case 1: cout << "Fanta"; break;
  case 2: cout << "Coca Cola"; break;
  case 3: cout << "sams cola"; break;
  case 4: cout << "root beer"; break;
  default:  cout  << "Invalid Selection"; break;
}

cout << endl;
Unfortunately a switch statement can't take a string. Not in C++, anyway, as I believe you can use strings for switch statements in C#.

You could use an enumerator:
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
#include <iostream>

enum SODA { SPRITE, FANTA, COCA_COLA };
int main()
{
	int soda = 0;

	std::cout << "(0) Sprite" << std::endl;
	std::cout << "(1) Fanta" << std::endl;
	std::cout << "(2) Coca Cola" << std::endl;
	std::cout << "Enter the number for the soda you want: ";
	std::cin >> soda;

	switch (soda)
	{
	case SPRITE:
		std::cout << "Here's your Sprite" << std::endl;
		break;

	case FANTA:
		std::cout << "Here's your Fanta" << std::endl;
		break;

	case COCA_COLA:
		std::cout << "Here's your Coca Cola" << std::endl;
		break;
	}

	return 0;
}


Edit:
You can only use integers with a switch.

This isn't true, you can also use characters. For example:
1
2
3
4
5
switch (character)
{
case 'a':
case 'b':
}
Last edited on
Well i'm not very familiar with enumerators, so i'll look into that.
Enumerators are fairly straight forward so you shouldn't have too much trouble understanding them. :) They can be pretty handy.
thanks, this could definitely come in handy in the future.
Topic archived. No new replies allowed.