Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C program switch statement

I'm new to programming in C. I have a quick question about Switch Statements. I have a menu that presents a list of options like so:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX 100

struct Video { 
char name[1024];        // Yvideo name
int ranking;                // Number of viewer hits
char url[1024];             // YouTube URL
};

struct Video Collection[MAX];

int tail = 0;

//-- Forward Declaration --// 
void printall();
void insertion();
void savequit();
void load();
void branching(char);
void menu(); 
int main()
{
char ch; 

load();     // load save data from file

printf("\n\nWelcome\n");

do {
    menu();
    fflush(stdin);            // Flush the standard input buffer 
    ch = tolower(getchar()); // read a char, convert to lower case
    branching(ch);
} while (ch != 'q');

return 0; 
}
void menu()
{
    printf("\nMenu Options\n");
printf("------------------------------------------------------\n");
    printf("i: Insert a new favorite\n");
    printf("p: Review your list\n"); 
    printf("q: Save and quit\n");
    printf("\n\nPlease enter a choice (i, p, or q) ---> "); 
}

void branching(char option)
{
switch(option)
{
    case 'i':
        insertion();
        break;

    case 'p':
        printall();
        break;

    case 'q':
        savequit();
        break;

    default:
        printf("\nError: Invalid Input.  Please try again..."); 
        break;
}
}

so far entering 'i' (for inserting a new entry) and q (for save and quit) work perfectly. However every time I enter 'p' I get the default case. (Error: Invalid Input. Please try again...). What is it that I am doing wrong? I believe the syntax for the switch is correct? I've tried changing the 'p' to a different letter and I still got the the default case. Here is my printall() method if that helps...

void printall()
{
int i; 

printf("\nCollections: \n"); 

for(i = 0; i < tail; i++)
{
    printf("\nName: %s", Collection[i].name);
    printf("\nRanking (Hits): %d", Collection[i].ranking);
    printf("\nURL: %s", Collection[i].url);
    printf("\n");
}
}
like image 631
accraze Avatar asked Jan 25 '26 12:01

accraze


2 Answers

What about something like:

char b[5];
do {
    menu();
    if(fgets(b,5,stdin)==NULL)
        return -1;

    ch = tolower(b[0]); // read a char, convert to lower case
    while(strlen(b)>=4&&b[3]!='\n'){
         check=fgets(b,5,stdin);
         if(check==NULL)
            return -1;
    }

    branching(ch);
} while (ch != 'q');
like image 189
Simone-Cu Avatar answered Jan 28 '26 06:01

Simone-Cu


You can output the invalid char in your default case. That may help you understand how your input are handled.

default:
    printf("\nError: Invalid Input ('%c').  Please try again...", option); 
    break;
like image 20
Yanok Avatar answered Jan 28 '26 06:01

Yanok



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!