GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 31-May-2005, 12:55
ChaosTheory ChaosTheory is offline
New Member
 
Join Date: May 2005
Posts: 2
ChaosTheory is on a distinguished road

problem with switches


i am having problem with this switch i made. it is the first time i have ever made one and am doing it for an assignment in a class. once the menu pops up at the very beginning you can enter a variable listed to have it do certain things. everything works fine except every time it has finished doing what is in each case it prints the menu out once, then prints the default, and the menu once more. it still works after that but i dont understand why it prints the menu and default out like it does. i have asked my teacher but he does not know whats wrong with it either. could somebody on this forum please tell me how to fix the problem, or correct it and tell me whats wrong with it.

thanks

================================================== ======
CPP / C++ / C Code:
#include<stdio.h>
#include<time.h>
#include<math.h>
#include<stdlib.h>

int main()

{

int A[99]={0},X[99]={0},Size=0,z,menu=1;
int small,large,Num;
char pick;
srand(time(NULL));

while(menu==1)
{
 printf("                ::::::::::::::::::::::::::::::::::::::::::::::\n");
 printf("                ::    -a- Manually load array               ::\n");
 printf("                ::    -b- Randomly load array               ::\n");      
 printf("                ::    -c- Print out array                   ::\n");          
 printf("                ::    -d- Copy values of [A] into [X]       ::\n");           
 printf("                ::    -e- Print out the values of [A]       ::\n");         
 printf("                ::    -f- Print out the values of [X]       ::\n");     
 printf("                ::    -g- Find smallest value of [A]        ::\n");     
 printf("                ::    -h- Find the largest value of [A]     ::\n");   
 printf("                ::    -i- Reverse the order of [A]          ::\n");    
 printf("                ::    -j- Sort [A] in ascending order       ::\n");     
 printf("                ::    -k- Sort [A] in descending order      ::\n");   
 printf("                ::    -l- Set every value of [A] to 0       ::\n");    
 printf("                ::    -m- Set every value of [X] to 0       ::\n");   
 printf("                ::    -n- Find the mean of [A]              ::\n");   
 printf("                ::    -o- Find the median of [A]            ::\n");      
 printf("                ::    -p- Exit program                      ::\n");     
 printf("                ::::::::::::::::::::::::::::::::::::::::::::::\n"); 
 scanf("%c",&pick);

 switch (pick)
 {

 case 'a'://manually load array
  printf("How big do you want the [A] to be?\n");
  scanf("%d",&Size);//scans the number entered which sets the size of [A]
  printf("Enter your numbers:\n"); 
  for (z=0;z<Size;z++)scanf("%d",&A[z]);//allows the user to assign each value to [A]
  break;
     
 case 'b'://randomly loads array
  {
  printf("How big do you want [A] to be?\n"); 
  scanf("%d",&Size);//scans the number entered which sets the size of [A]
  for (z=0;z<Size;z++)A[z]=rand()%100;//puts in random values for each value of [A]
  }
  break;
     
 case 'c'://prints [A] 
  {
 // if (A[z]>=1)
  for (z=0;z<Size;z++)printf("%d\n",A[z]);//prints out each value of [A]
 // else printf("0");
  }
  break; 
  
 case 'd'://copies values of [A] to [X] 
  for (z=0;z<Size;z++)X[z]=A[z];//copies each value of [A] to [X]
  break;

 case 'e'://prints out values of [A]
  for (z=0;z<Size;z++)printf("%d\n",A[z]);//prints out [A]
  break;

 case 'f'://prints out values of [X]
  for (z=0;z<Size;z++)printf("%d\n",X[z]);//prints out [X]
  break;

 case 'g'://smallest value
  small=A[0];//defines small
  for (z=1;z<Size;z++)if(A[z]<small)small=A[z];//tests each number to find the smallest
  printf("%d\n",small);//prints out the smallest number 
  break;

 case 'h'://largest value
  large=A[0];//defines large
  for (z=1;z<Size;z++)if(A[z]>large)large=A[z];//tests each number to find the largest
  printf("%d\n",large);//prints out the largest number 
  break;

 case 'i'://reverse order
  for(z=Size-1;z>=0;z-=1)printf("%d\n",A[z]);//prints out [A] in the reverse order
  break;

 case 'j'://ascending order
    break;

 case 'k'://descending order
   break;

 case 'l'://sets values of [A] to 0

  break;

 case 'm'://sets values of [X] to 0

  break;

 case 'n'://mean

  break;

 case 'o'://median

  break;

 case 'p'://exits program
  printf("Thanks for using the program.\n");
  menu=0;//sets menu to 0 so the program will close
  break;

 default://default option if no known value is selected
  printf("Invalid option selected\n");
  menu=0;
  break;
 }
}


return 0;
}
Last edited by LuciWiz : 31-May-2005 at 14:28. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 31-May-2005, 13:06
imbtf imbtf is offline
Junior Member
 
Join Date: Aug 2004
Posts: 41
imbtf will become famous soon enough
http://www.gidforums.com/t-4272.html - Scroll down a bit for a discussion on scanf
  #3  
Old 31-May-2005, 13:38
ChaosTheory ChaosTheory is offline
New Member
 
Join Date: May 2005
Posts: 2
ChaosTheory is on a distinguished road
Quote:
Originally Posted by imbtf
www.gidforums.com - Scroll down a bit for a discussion on scanf

thank you, i never knew they didnt handle characters very well. my instructor even said that we were supposed to do the assingment using characters, i was the only one who actually did though.
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 12:31
commands out of sync problem (c++ builder) Largowww MS Visual C++ / MFC Forum 0 28-May-2005 03:40
String problem vaha C Programming Language 3 24-May-2005 18:21
Css design problem JUNK KED Web Design Forum 4 25-Jan-2005 17:45
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 07:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:41.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.