Solved

what should i do for my code dont stop?

Posted on 2016-10-16
8
34 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 5

Expert Comment

by:D Patel
ID: 41846086
Where is the code?
0
 
LVL 51

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 32

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 51

Expert Comment

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

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now