Divan
Divan

Reputation: 402

switch menu in C

I'm trying to use a menu in this C program but I keep getting an error:

3.c: In function 'main':

q3.c:99:3: error: expected declaration or statement at end of input

  }

  ^

q3.c:99:3: error: expected declaration or statement at end of input

The code

#include <stdio.h>

int userInput();

void printList();
void editStud();
void delStud();
void addStud();

struct studentRec
{
    char name[25];
    char init[25];
    char pNu[25];    
    int studNum;
    float bBalance;
};

  int main()
  {
    //no constants

    struct studentRec students;  

    FILE *fp,*fw,*ft;

    int sel = 1; //select number for menu


    while(userInput)
    {
      sel = userInput();

      switch(sel)
    case1:
      printList();
      break;

    case2:
      editStud();
      break;

    case3:
      delStud();
      break;

    case4:
      addStud();
      break;

    case0:
      break;  

    {

  }

  int userInput()
  {
    int choice;

    printf("===========================\n");
    printf("(1). View all students\n");
    printf("(2). Edit student details\n");
    printf("(3). Delete student\n");
    printf("(4). Add new student\n");
    printf("(0). Exit\n");
    printf("===========================\n\n");

    printf("Enter your choice Please\n");
    scanf("%d", &choice);

    return choice;
  }

  void printList()
  {
     printf("*Prints list*\n");
  }

  void editStud()
  {
     printf("*edits entry*\n");
  }

  void delStud()
  {
     printf("*deletes entry*\n");
  }

  void addStud()
  {
     printf("*adds entry\n");
  }

Thanx for all the responses, what I meant to do:

.
.
.

int userInput();

void printList();
void editStud();
void delStud();
void addStud();
.
.
.


int sel = 1; //select number for menu


while(sel)
{
  sel = userInput();

  switch(sel)
  {
case 1:
  printList();
  break;

case 2:
  editStud();
  break;

case 3:
  delStud();
  break;

case 4:
  addStud();
  break;

case 0:
  break;

default:
  printf("That is not a valid selection!\n");
  }

}

.
.
.

errors were: 1. incorrect while braces 2. used while(userInput) instead of while(sel) 3. no space between case and number

Upvotes: 0

Views: 11679

Answers (4)

Fadi Hanna AL-Kass
Fadi Hanna AL-Kass

Reputation: 629

what is case1? My only guess is that you mean to compare against 1, in which case you'd have to leave a space between the keyword case and the actual case value. Here's how your switch case should look like:

switch (sel) {

case 1:
  printList();
  break;

case 2:
  editStud();
  break;

case 3:
  delStud();
  break;

case 4:
  addStud();
  break;

case 0:
  break;  

}

Upvotes: 1

palerdot
palerdot

Reputation: 7642

1) Your braces inside while is meaningless (the one before ending while brace)

2) your switch statement does not have enclosing braces.

3) Also, there should be a space between case keyword and the switch input

Correct way is

 while(userInput){

  sel = userInput();

  switch(sel){

    case 1:
    printList();
    break;

    case 2:
    editStud();
    break;

    case  3:
    delStud();
    break;

    case 4:
    addStud();
    break;

    case 0:
    break;  

   }

}

Upvotes: 0

Bill Lynch
Bill Lynch

Reputation: 81916

  1. After a Switch statement, you want a open brace.

    switch(sel) {
    
  2. case is a keyword, which means that you probably want whitespace around it.

    case 1:
    case 2:
    ...
    
  3. You have an open brace where you probably want a close brace. The number of open braces should match the number of close braces.

    }
    
  4. Your while statement is quite odd. You probably want to do something else with that...

So, main() should actually look like:

int main()
{
    struct studentRec students;  
    FILE *fp,*fw,*ft;
    int sel = 1; //select number for menu

    while(true)
    {
        sel = userInput();

        switch(sel) {
            case 1:
                printList();
                break;

            case 2:
                editStud();
                break;

            case 3:
                delStud();
                break;

            case 4:
                addStud();
                break;

            case 0:
                break;  
        }
    }
}

Upvotes: 0

ouah
ouah

Reputation: 145829

Change:

case1:

to

case 1:

etc.

also a switch statement requires a starting { and an ending }.

Upvotes: 0

Related Questions