# Thread: Really close to solve an exercise. I have an easy question of switch case

1. ## Really close to solve an exercise. I have an easy question of switch case

Hi,

I am now with structures. After a hard day, I have a program that is able to get a menu with players data: name, age, height. The program works perfectly but I need an improvement. To get the menu, I use switch case but with 0,1,2,3. I must use A,B,C and I have in mind using 0 to exit the program. This is a very easy question: if I put A, B, C, 0 I get an error that those are undefinied variables. How can I make to put A,B,C in each case instead of 1,2,3? This is only a section of my code, the int main ()

Code:
```int main()
{
struct players data[10];
int n, choice;

printf("Insert the number of registrations of players: ");
scanf("%d", &n);
do {
printf("Press 1 to print every name.\n");
printf("Press 2 to print every age.\n");
printf("Press 3 to print every height.\n");
printf("Press 0 to exit\n");
printf("\nInsert an option (1-3), 0 to exit : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
print_names(data, n);
break;
case 2:
print_ages(data,n);
break;
case 3:
print_heights(data,n);
break;
}
}
while (choice != 0);

return 0;
}```
This is only int main () if you need all the code (which is ok) I can put it. I need to substitute case 1, case 2, case 3 with case A, case B, case C (0 to exit).

2. if I put A, B, C, 0 I get an error that those are undefinied variables.
If you put A, B, C, or 0 where and how exactly? Do you know how to use character constants?

Jim

3. Jim, you know that I suck with chars and strings

This is my idea:

Code:
```int main()
{
struct players data[10];
int n, choice;

printf("Insert the number of registrations of players: ");
scanf("%d", &n);
do {
printf("Press A to print every name.\n");
printf("Press B to print every age.\n");
printf("Press C to print every height.\n");
printf("Press 0 to exit\n");
printf("\nInsert an option (1-3), 0 to exit : ");
scanf("%d", &choice);
switch (choice)
{
case A:
print_names(data, n);
break;
case B:
print_ages(data,n);
break;
case C:
print_heights(data,n);
break;
}
}
while (choice != 0);

return 0;
}```
I know I need to declare A,B,C. I'm not quite sure if I know enough of character constants, is something like... const char... and that's all I know.

4. So you then you don't realize that in "case A:" the compiler thinks you're trying to use a variable instead of a character constant like 'A'?

What book are you using that doesn't explain the difference between a variable name, a character constant, and a string constant?

Jim

5. Yes, I know it, if I change 1 to A, 2 to B, 3 to C I know the compiler thinks I am trying to use a variable. So, I guess I have to declare this variable. Is the first time I use letters instead of numbers in switch case.

6. Did you even read my last post?

You should use a character constant instead of a variable.

Jim

7. Originally Posted by jimblumberg
Did you even read my last post?

You should use a character constant instead of a variable.

Jim
I'm not following a book, I am making a course. This is my last attempt:

Code:
```int main()
{
struct players data[10];
int n;
char choice;

printf("Insert the number of registrations of players: ");
scanf("%d", &n);
do {
printf("Press 'A' to print every name.\n");
printf("Press 'B' to print every age.\n");
printf("Press 'C' to print every height.\n");
printf("Press '0' to exit\n");
printf("\nInsert an option (A-C), 0 to exit : ");
scanf("%c", &choice);
switch (choice)
{
case 'A':
print_names(data, n);
break;
case 'B':
print_ages(data,n);
break;
case 'C':
print_heights(data,n);
break;
}
}
while (choice!=0);

return 0;
}```
I'm not convinced with the output:

Press A to print every name
Press B to print every age
Press C to print every height
Press 0 to exit

Insert an option (A-C), 0 to exit :
Press A to print every name
Press B to print every age
Press C to print every height
Press 0 to exit

Moreover, the program doesn't exit with 0...

8. I'm not following a book, I am making a course.
What does "making a course" mean?

I'm not convinced with the output:
What does that mean?
Try having scanf() skip the leading whitespace by placing a space before your specifier:
Code:
`scanf(" %c", &choice); // Note the space.`
Moreover, the program doesn't exit with 0...
You can't have it both ways, you either need to use character constants or numeric constants, you must match the type of the variable you used in your switch().

Jim

9. Originally Posted by jimblumberg
What does "making a course" mean?
I am an unemployed engineer, so I am taking a course to gain some programming skills. I paid money for taking a 5-hour class a week and get lots of exercises to solve each week, not much funy but stressful. I decided to take C because seems to be useful to understand other languages as well.

I finally solved this exercise. This is the part of the code we are talking about:

Code:
```int main()
{
struct players data[10];
int n;
char choice;

printf("Insert the number of registrations of players: ");
scanf("%d", &n);
do {
printf("Press 'A' to print every name.\n");
printf("Press 'B' to print every age.\n");
printf("Press 'C' to print every height.\n");
printf("Press 'E' to exit\n");
printf("\nInsert an option (A-C), E to exit : ");
scanf(" %c", &choice);
switch (choice)
{
case 'A':
print_names(data, n);
break;
case 'B':
print_ages(data,n);
break;
case 'C':
print_heights(data,n);
break;
}
}
while (choice!='E');

return 0;
}```
Thank you for let me know I can't merge characters and numeric constants. My program now exits with E. I added a space in scanf but I would like to understand why this happened and what that space makes to prevent that the menu appears twice.

Jorge

10. I'm gonna take a gamble..

The
Code:
`scanf("%d", &n);`
read an integer from the input buffer (standard input, STDIN), but left the '\n' (newline, return) character that you entered, is still in the buffer.

Thus when you subsequently call
Code:
`scanf("%c", &choice);`
You should get a '\n' char into the "choice" variable.

What you've done by adding the extra space into
Code:
`scanf(" %c", &choice);`
is to correct for that extra newline char.

11. Remember, everything you write into the a buffered input stream, remains in the buffer, until it is read back out.*

* This is a non-technical discussion, but at least holds for this case.

12. Another option is to make "choice" an "int" variable, and simply use another "scanf(%d, &choice)". IIRC, the %d specifier is defined to skip any leading whitespace in the input buffer. However, for %c, no character are skipped, and the next unread character is simply returned immediately.

13. Originally Posted by MacNilly
Another option is to make "choice" an "int" variable, and simply use another "scanf(%d, &choice)". IIRC, the %d specifier is defined to skip any leading whitespace in the input buffer. However, for %c, no character are skipped, and the next unread character is simply returned immediately.
I don't think I would do that. The cases are characters, so making the conversion a %d would not work and be confusing. The %d conversion does not play nice with letters. For proof:
Code:
```#include <stdio.h>
int main(void)
{
int d;
int rv;

rv = sscanf("ABCAB", "%d", &d);
if (rv != 1)
{
puts("conversion failed");
}
return 0;
}

conversion failed```