473,387 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

switch statement problem

hello...
Im new here n i hav question dat i am unable to complete...Plzz help me...

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. int main(void)
  4.  
  5. {
  6.     int id, quantity, limit, order_limit, temp;
  7.     int ch, components, i, j,total,count;
  8.  
  9.     limit = 5;            
  10.     order_limit = 10;
  11.     int components_id[3];
  12.     int components_quantity[3],price;
  13.  
  14.  
  15.  
  16.  
  17.     int choice;
  18.     i=3;
  19.     while(i< order_limit)   
  20.     {
  21.         printf("\n\n\n Available components in Unitar: \n\n");            
  22.         printf("1. PC Components\n");
  23.         printf("2. Network\n");
  24.         printf("3. Exit\n\n\n");
  25.  
  26.     }
  27.  
  28.        printf("Press 1 to place order \nPress 2 to finish order:    ");
  29.         scanf("%d",&ch);
  30.         printf("***************************\n\n");
  31.         printf("                        -------------\n");
  32.         printf("                        PC components\n");
  33.         printf("                        -------------\n");
  34.         printf("                       1.Monitor:Rm100\n"); 
  35.         printf("                       2.Keyboard:Rm95\n");
  36.         printf("                       3.Mouse:Rm55\n");
  37.         printf("                       4.RAM:Rm237\n"); 
  38.         printf("                       5.Hard Disk:Rm76\n"); 
  39.         printf("                       6.ROM drive:RM189\n"); 
  40.         printf("                       7.Processor:Rm340\n");
  41.         printf("                       8.Others\n");
  42.         printf("                       9.Exit\n");
  43.  
  44.         printf("                       --------\n");
  45.         printf("                       Neyworks\n");
  46.         printf("                       --------\n");
  47.  
  48.         if(ch == 1)
  49.         {
  50.  
  51.             printf("\n enter component id:   \n");
  52.             scanf("%d", &temp);
  53.             components_id[i] = temp; 
  54.  
  55.             if(components_id[i] > limit)
  56.             {
  57.                 printf("\n invalid id entered...try again\n");
  58.                 continue;
  59.             }
  60.  
  61.             printf("\n enter quantity of component required:   \n");
  62.             scanf("%d", &temp);
  63.             components_quantity[i] = temp;
  64.  
  65.             printf("\n id: %d quantity: %d, i:%d \n", components_id[i], components_quantity[i], i);
  66.             i++;
  67.                 printf("Total cost of each type of component is:%d\n");
  68.             price * components_quantity[i];
  69.             printf("%f",total);
  70.  
  71.         }
  72.  
  73.         else if(ch == 3)
  74.             break;
  75.         else
  76.         {
  77.             printf("\n invalid choice enterd...try again\n");
  78.             continue;
  79.         }
  80.  
  81.  
  82.  
  83.     }
  84.  
  85.  
  86.  
  87.     printf("\n\n\n ordered placed: \n");            
  88.     for(j=0; j<i; j++)
  89.         printf("\n %d: component id:%d  quantity: %d", j, components_id[i], components_quantity[i]);
  90.  
  91.  
  92.  
  93.     getchar();
  94.     return 1;
  95. }
  96.  
these r my codes but its not really correct...im confused how to create switches in it....Can u plz help me with it?...
Mar 26 '09 #1
2 1576
JosAH
11,448 Expert 8TB
I moved your question to its own thread; it is considered rude to hijack someone else's thread. I also fixed your [code] ... [/code] tags. One more bit of advice: write proper English, e.g. 'please' is not written 'plz'.

kind regards,

Jos (moderator)
Mar 26 '09 #2
donbock
2,426 Expert 2GB
@sweetipie
scanf will only change the value of ch if it finds that your input character stream is consistent with the format string. If you type 'A' rather than '5' then ch retains its previous value, which will cause you to repeat the action associated with the last time through the loop.

@sweetipie
It would be a lot easier for potential helpers if you explained why this code is not satisfactory.

Are you getting compiler errors? If so, then cut and paste them into your post.

Is the program malfunctioning? If so, describe the erroneous behavior.

Do you want to refactor the program ... perhaps replace the if/elseif/else cascade with a switch statement? If so, describe your desires and constraints.
Mar 26 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. ...
2
by: Cathleen C via DotNetMonster.com | last post by:
I'm a semi-beginner with c# and am having a problem effectively implementing a switch statement. I've created an asp.net app that runs a report depending on which item was selected from a drop...
2
by: Macca | last post by:
Hi, I have a switch statement that has 5+ case statements. Each of these case statements copies form one array to another. Rather than doing a separate try..catch statement for each case...
2
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} ...
5
by: Phuff | last post by:
Hey all, I need some direction help. I have a switch case statement that is seemingly my only option right now, but its too large and not easy to maintain the code. Here goes... I have part...
12
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
6
by: asadikhan | last post by:
Hello, I have a bit of a design issue around this application I am developing, and I just want to run it through some of the brains out here. So I have a table called ErrorCheck which...
2
osward
by: osward | last post by:
Hello there, I am using phpnuke 8.0 to build my website, knowing little on php programing. I am assembling a module for my member which is basically cut and paste existing code section of...
2
by: Phillip B Oldham | last post by:
What would be the optimal/pythonic way to subject an object to a number of tests (based on the object's attributes) and redirect program flow? Say I had the following: pets = {'name':...
7
by: Rohit | last post by:
Hi, I am working on a switch module which after reading voltage through a port pin and caterogizing it into three ranges(open,low or high), passes this range to a function switch_status() with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.