Note that there are some explanatory texts on larger screens.

plurals
  1. POC program switch statement
    primarykey
    data
    text
    <p>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:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;ctype.h&gt; #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) ---&gt; "); } 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; } } </code></pre> <p>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...</p> <pre><code>void printall() { int i; printf("\nCollections: \n"); for(i = 0; i &lt; tail; i++) { printf("\nName: %s", Collection[i].name); printf("\nRanking (Hits): %d", Collection[i].ranking); printf("\nURL: %s", Collection[i].url); printf("\n"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload