0
0
Cprogramming~20 mins

Use cases - Mini Project: Build & Apply

Choose your learning style9 modes available
Use Cases in C Programming
📖 Scenario: You are building a simple program to decide what message to show based on a user's role in a system.
🎯 Goal: Create a C program that uses switch statements to print different messages for different user roles.
📋 What You'll Learn
Create an integer variable called role with a specific value
Create a switch statement using the variable role
Handle cases for roles 1, 2, and 3 with different messages
Include a default case for unknown roles
Print the message corresponding to the role
💡 Why This Matters
🌍 Real World
Switch statements are used in programs to make decisions based on different conditions, like user roles, menu choices, or error codes.
💼 Career
Understanding switch statements helps in writing clear and efficient code for decision-making in many software development jobs.
Progress0 / 4 steps
1
Create the role variable
Create an integer variable called role and set it to 2.
C
Need a hint?

Use int role = 2; inside the main function.

2
Add the switch statement
Add a switch statement using the variable role inside the main function.
C
Need a hint?

Write switch(role) { } inside main.

3
Add cases for roles 1, 2, and 3
Inside the switch(role), add cases for 1, 2, and 3. For each case, add a printf statement with these exact messages:
"Admin access granted\n" for case 1,
"User access granted\n" for case 2,
"Guest access granted\n" for case 3.
Don't forget to add break; after each printf.
C
Need a hint?

Use case 1:, case 2:, case 3: with printf and break;.

4
Add a default case and print the message
Add a default case inside the switch(role) that prints "Unknown role\n". Then, add a printf statement to print the message for the current role when the program runs.
C
Need a hint?

Use default: with printf("Unknown role\n"); and break;.