Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 53
  • Last Modified:

what should i do for my code dont stop?

my program stop and finish when user choose and answer the question of my menu item but i wamt user choose another item how can i do this ?
tnx
0
Sepehr Ghaffari
Asked:
Sepehr Ghaffari
  • 2
  • 2
  • 2
  • +1
1 Solution
 
D PatelD Patel, Software EngineerCommented:
Where is the code?
0
 
Julian HansenCommented:
Usually you need to put your menu inside a loop something like this
void showMenu();

int main() 
{
  char option = 0;

  while(toupper(option) != 'Q') {
    showMenu();
    scanf("%c", &option);
    switch(option) {
      case '1':
        doOptionOne();
      break;
      case '2':
        doOptionTwo();
       break;
      //... handle remaining options
     }
  }
}

void showMenu()
{
  printf("Menu\n");
  printf("1) Option 1\n");
  printf("2) Option 2\n");
  // Other options
  printf("Q] Quit\n");
}

Open in new window

0
 
sarabandeCommented:
but i want user choose another item how can i do this ?
you better would not add menu items to a menu which the user shouldn't choose.

there are 3 ways:

- delete the menu item from menu if menu was built by a resource.
  (before it could be clicked on by the user)
- make the menu item gray (not clickable).
- don't add it to the menu if it is a dynamic menu (for example a context menu).

@Rix: sorry if it seems that i ignored your comment. it is not so, i only answered to the question in the original post which doesn't necessarily require code.

Sara
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Sepehr GhaffariAuthor Commented:
tnx all this is my code:

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


void Function1();
void Function2();
void Function3();
void Function4();
int ans ;

int main()
{
  int menu_item =0;
  gotoxy(20,5);
  printf("Welcom to this game <<football Info>>");
  while(1==1)  {
    clrscr();
    printf("\nMenu:");
    printf("\n\t[1] Goalkeeper");
    printf("\n\t[2] Defence");
    printf("\n\t[3] Midfilder");
    printf("\n\t[4] Forward");
    printf("Enter your choise:");

    scanf("%d",&menu_item);


    if(menu_item ==0) {
      break;
    }//end of if
    else {
      clrscr();
      switch(menu_item) {
        case 1:
          Function1();
        break;
        case 2:
          Function2();
        break;
        case 3:
          Function3();
        break;
        case 4:
          Function4();
        break;
        default:
          continue;
      }//end of switch
     
      printf("press any key to continue...");

      getch();
    }//end of else
  }//end of while
 
  return 0;
}


void Function1()
{
  //int ans1 ;

  printf("\n Who was the  best goalkeeper last year? \n");
  printf("-------------------------------------------");
  scanf("%d",&ans);

  printf("\n Who is the legendary Italian goalkeeper? \n");
  printf("-------------------------------------------");
  scanf("%d",&ans);

  getche();
}


void Function2()
{

  //int ans2 ;

  printf("\n Who trasfer from manchester united to barcelona in 2008? \n");
  printf("-----------------------------------------");
  scanf("%d",&ans);

  printf("\n Who plays on the left side Borussia Dortmund? \n");
  printf("\n----------------------------------------");
  scanf("%d",&ans);

  getche();
}

void Function3()
{
  //int ans3 ;

  printf("\n How many years Xavi play in Bracelona? \n");
  printf("----------------------------------------");
  scanf("%d",&ans);

  printf("\n Which irainian Medfilder play in Osasuna? \n");
  printf("\n---------------------------------------");
  scanf("%d",&ans);

  getche();
}


void Function4()
{
  //int ans4 ;
  printf("\n Zelatan play in which club? \n");
  printf("\n----------------------\n");
  scanf("%d",&ans);

  printf("who won 5golden ball? \n");
  printf("\n----------------------\n");
  scanf("%d",&ans);

  getche();
}

Open in new window

0
 
Julian HansenCommented:
I think this pattern was outlined in my earlier post
0
 
sarabandeCommented:
i tried your code and got other Problems since i was in an endless loop

scanf("%d",&ans);

Open in new window

you asked for names but scanf wants to read integers.

in my program this leads to an endless loop since the input stream goes into fail state (where it never would recover from) and menu_item therefore never goes to 0 (while condition is endless and the break condition never was true).

you should use the 'quit' (see code of Julian) for to break the while condition and you should use a character buffer to read with scanf when some string values are expected for answer:

char szinput[128] = "";
scanf("%s", szinput);

Open in new window


nevertheless there is still a problem how you could avoid that a wrong answer is spoiling the input stream stdin.

Sara
0
 
Sepehr GhaffariAuthor Commented:
tnx i undrestand my problem i will slove it
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now