Solved

what should i do for my code dont stop?

Posted on 2016-10-16
8
43 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
[X]
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
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 7

Expert Comment

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

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 34

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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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 58

Expert Comment

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

624 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