How to link a Switch statement to a if/else.

Hello, I'm just a newbie to programming, could you help me solve this problem of mine. The sample output of this program would be:
Please select:
1 - Year
2 - Day
3 - Month
Input choice: 1
Input Number: 2
I'm a Sophomore!

Please select:
1 - Year
2 - Day
3 - Month
Input choice: 2
Input Number: 2
It's Tuesday.

Please select:
1 - Year
2 - Day
3 - Month
Input choice: 3
Input Number: 2
February. Heart's month!

Here is my code, I only typed the Year(levels) yet because when I tried to input 2(day) and 3(month) for the Input choice and after that, Inputting a number to 1-4 would yield the same result to case 1's year levels.

#include <stdio.h>
#include <conio.h>

using namespace std;

int main()

{
int y, d, m, x;

printf("Input choice: ");
scanf("%d", &x, &y, &d, &m);

switch (x){
case 1:
printf("\nYear");
break;
case 2:
printf("\nDay");
break;
case 3:
printf("\nMonth");
break;

default:
printf("Invalid Statement");}

if (y)
printf("\nInput number: ");
scanf("%d", &y);

if (y==1)
printf("I'm a freshman!");

if (y==2)
printf("I'm a Sophomore!");

if (y==3)
printf("I'm a Junior!");

if (y==4)
printf("I'm a Senior!");

else if (y>=5)
printf("Invalid Statement!");







getch();

return 0;

}

Can anyone elaborate to me how to link the case 2 to days and case 3 to months? Thank you very much.
Last edited on
scanf("%d", &x, &y, &d, &m);

This will never change y, d, or m because it's only scanning a single number.

I think you should restructure the program a little. I'd get BOTH numbers first, and then figure out what to do with them. Also, use arrays of strings for the data. Here is a start:

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
#include <stdio.h>

char *years[4] = {
    "Freshman", "Sophomore", "Junior", "Senior"
};

char *months[12] = {
    "January", "February", "March",
    "April", "May", "June",
    "July", "August", "September",
    "October", "November", "December"
};

char *days[7] = {
    "Sunday", "Monday", "Tuesday", "Wedensday",
    "Thursday", "Friday", "Saturday"
};

	
int main(int argc, char **argv)
{
    int number = 5;		// an illegal value
    int choice = -1;

    // Get the input
    printf("Please select:\n"
	   "1 - Year\n"
	   "2 - Day\n"
	   "3 - Month\n"
	   "Input choice: ");
    fflush(stdout);
    scanf("%d", &choice);

    printf("Input Number: ");
    fflush(stdout);
    scanf("%d", &number);

    // Do something with the input
    switch(choice) {
    case 1:			// year
	if (number < 1 || number > 4) {
	    printf("illegal number\n");
	} else {
	    printf("%s\n", years[number-1]);
	}
	break;
    case 2:
	break;
    case 3:
	break;
    default:
	break;
    }
    return 0;
}
Topic archived. No new replies allowed.