Solved

what should i do for my code dont stop?

Posted on 2016-10-16
8
38 Views
Last Modified: 2016-10-17
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
Comment
Question by:Sepehr Ghaffari
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 6

Expert Comment

by:DPatel
ID: 41846086
Where is the code?
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41846221
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
 
LVL 33

Expert Comment

by:sarabande
ID: 41846555
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Author Comment

by:Sepehr Ghaffari
ID: 41846568
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
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41846612
I think this pattern was outlined in my earlier post
0
 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 41846641
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
 
LVL 1

Author Closing Comment

by:Sepehr Ghaffari
ID: 41847057
tnx i undrestand my problem i will slove it
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question