C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-09-2010, 12:33 AM   #1
Registered User
 
Join Date: Feb 2010
Posts: 25
need help

iam creating a program with diffrenent option and i need to exit the program when i press number 4 how can i do that i try using a while loop but it did not work. my program . i try using using a while loop
Code:
include <stdio.h>
#include <stdlib.h>



int mainMenu(void)
{
   int option ;


   printf("1. Register\n");
   printf("2. Unregister\n");
   printf("3. Query\n");
   printf("4. Exit\n");

   printf("\nOption: ");
   scanf("%d", &option);

while (option!=3) // loop goes to infinite when i press one. i think i made a mistake here
{

   switch (option)
   {
      case 1:
         printf("hi");
         break;
      case 2:
         printf("run");
         break;
      case 3:
      printf("love");
         break;
      
}

}

Last edited by mouse666666; 02-09-2010 at 12:42 AM.
mouse666666 is offline   Reply With Quote
Old 02-09-2010, 12:58 AM   #2
Registered User
 
Join Date: Feb 2010
Posts: 4
Try this:

Code:
while(1)
{
   printf("\nOption: ");
   scanf("%d", &option);

   switch (option)
   {
      case 1:
         printf("hi");
         break;
      case 2:
         printf("run");
         break;
      case 3:
      printf("love");
         break;
      case 4:
      exit(1);
      default:
   printf("1. Register\n");
   printf("2. Unregister\n");
   printf("3. Query\n");
   printf("4. Exit\n");

}
      
}
Cameron2 is offline   Reply With Quote
Old 02-09-2010, 04:52 AM   #3
Registered User
 
Join Date: Feb 2010
Posts: 6
yah you so forgot your scanf/getc calls. lol and getc is faster then scanf just a little helpful hint.
scanf("%d",&i);
getc(i);
kille6525 is offline   Reply With Quote
Old 02-09-2010, 05:47 AM   #4
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,785
People, people, people. Don't hand out solutions or they newbies won't learn!
The answer to the question is: what the heck do you think your program does? Write it down on paper, step by step, on a flowchart, and see what's wrong.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 05:38 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22