Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

what should i do for my code dont stop?

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

Expert Comment

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

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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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 55

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

839 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