Thread: C programming assignment

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    4

    C programming assignment

    Programming has to use
    ➢ The use of decision structures
    ➢ The use of repetition structure.

    The Problem is how to do

    Present a main menu to the user about the following options:
    1) Calculate resistance value
    2) Help
    3) Exit

    The “help” option presents some information about the program and how to use it, and re-displays the main menu options. The “Exit” option terminates the program. The “Calculate resistance value” leads the user to a secondary menu as shown in Step 2.


    The program need to be build is for this

    Thanks alot
    just telling me how to do might help me
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
    	int band1, band2, band3, band4 ,dig1, dig2, dig3, dig4 ,exit;
    	
    	do
    	{
    		
    		
    		
    		printf(" 0) Black \n 1) Brown \n 2) Red \n 3) Orange \n 4) Yellow \n 5) Green \n 6) Blue \n 7) Violet \n 8) Gray \n 9) White \n\n");
    		printf("\n Please choose the colour of band1: ");
    		scanf("%i",&band1);
    		
    		if ((band1 < 0) || (band1 > 9))
    		{
    			do
    			{
    				printf(" 0) Black \n 1) Brown \n 2) Red \n 3) Orange \n 4) Yellow \n 5) Green \n 6) Blue \n 7) Violet \n 8) Gray \n 9) White \n\n");
    				printf("\n\a!!You have entered an invalid number!! \n\n");	  
    				printf("\n Please choose the colour of band1: ");
    				scanf("%i",&band1);
    			}
    			while ((band1 < 0) || (band1 > 9));
    		}
    		
    		
    		printf(" 0) Black \n 1) Brown \n 2) Red \n 3) Orange \n 4) Yellow \n 5) Green \n 6) Blue \n 7) Violet \n 8) Gray \n 9) White \n\n");
    		printf("\n Please choose the colour of band2: ");
    		scanf("%i",&band2);
    		
    		if ((band2 < 0) || (band2 > 9))
    		{
    			do
    			{
    				printf(" 0) Black \n 1) Brown \n 2) Red \n 3) Orange \n 4) Yellow \n 5) Green \n 6) Blue \n 7) Violet \n 8) Gray \n 9) White \n\n");
    				printf("\n\a!!You have entered an invalid number!! \n\n");
    				printf("\n Please choose the colour of band2: ");
    				scanf("%i",&band2);
    			}
    			while ((band2 < 0) || (band2 > 9));
    		}
    		
    		printf(" 0) Black \n 1) Brown \n 2) Red \n 3) Orange \n 4) Yellow \n 5) Green \n 6) Blue \n 7) Violet \n 8) Gray \n 9) White \n 10) Gold \n 11) Silver \n\n");
    		printf("\n Please choose the colour of band3: ");
    		scanf("%i",&band3);
    		
    		if ((band3 < 0) || (band3 > 11))
    		{
    			do
    			{
    				printf(" 0) Black \n 1) Brown \n 2) Red \n 3) Orange \n 4) Yellow \n 5) Green \n 6) Blue \n 7) Violet \n 8) Gray \n 9) White \n 10) Gold \n 11) Silver \n\n ");
    				printf("\n\a!!You have entered an invalid number!! \n\n");
    				printf("\n Please choose the colour of band3: ");
    				scanf("%i",&band3);
    			}
    			while ((band3 < 0) || (band3 > 11));
    		}
    		
    		
    		printf("\n 0) Gold \n 1) Silver \n");
    		printf("\n Please choose the colour of band4: ");
    		scanf("%i",&band4);
    		
    		if ((band4 < 0) || (band4 > 1))
    		{
    			do
    			{
    				printf("\n 0) Gold \n 0) Silver \n");
    				printf("\n\a!!You have entered an invalid number!! \n\n");
    				printf("\n Please choose the colour of band4: ");
    				scanf("%i",&band4);
    			}
    			while ((band4 < 0) || (band4 > 1));
    		}
    		
    		
    		if (band1 == 0)     dig1 = 0; 
    		if (band1 == 1)     dig1 = 1; 
    		if (band1 == 2)     dig1 = 2;
    		if (band1 == 3)     dig1 = 3; 
    		if (band1 == 4)     dig1 = 4; 
    		if (band1 == 5)     dig1 = 5;
    		if (band1 == 6)     dig1 = 6;
    		if (band1 == 7)     dig1 = 7;
    		if (band1 == 8)     dig1 = 8;
    		if (band1 == 9)     dig1 = 9;
    		
    		
    		
    		
    		if (band2 == 0)     dig2 = 0; 
    		if (band2 == 1)     dig2 = 1; 
    		if (band2 == 2)     dig2 = 2;
    		if (band2 == 3)     dig2 = 3; 
    		if (band2 == 4)     dig2 = 4; 
    		if (band2 == 5)     dig2 = 5;
    		if (band2 == 6)     dig2 = 6;
    		if (band2 == 7)     dig2 = 7;
    		if (band2 == 8)     dig2 = 8;
    		if (band2 == 9)     dig2 = 9; 
    		
    		
    		
    		if (band3 == 0)     dig3 = 0; 
    		if (band3 == 1)     dig3 = 1; 
    		if (band3 == 2)     dig3 = 2;
    		if (band3 == 3)     dig3 = 3; 
    		if (band3 == 4)     dig3 = 4; 
    		if (band3 == 5)     dig3 = 5;
    		if (band3 == 6)     dig3 = 6;
    		if (band3 == 7)     dig3 = 7;
    		if (band3 == 8)     dig3 = 8;
    		if (band3 == 9)     dig3 = 9;
    		if (band3 == 10)    dig3 = -1;
    		if (band3 == 11)    dig3 = -2;
    		
    		
    		if (band4 == 0)     dig4 = 5;
    		if (band4 == 1)		dig4 = 10;
    		
    		
    		
    		
    		
    		
    		printf(" 0) Black \n 1) Brown \n 2) Red \n 3) Orange \n 4) Yellow \n 5) Green \n 6) Blue \n 7) Violet \n 8) Gray \n 9) White \n\n");
    		printf(" Band1 is: %i \n Band2 is: %i \n Band3 is: %i \n Band4 is: %i \n", band1,band2,band3,band4); 
    		
    		
    		
    		
    				
    		if (dig3 >= 3)
    		{
    			dig3 = dig3 - 3;
    			printf("\n\n\a The Resistance is %i%i x 10^%i ± %i KΩ  ",dig1,dig2,dig3,dig4);
    		}
    		else
    		{
    			printf("\n\n\a The Resistance is %i%i x 10^%i ± %i Ω ",dig1,dig2,dig3,dig4);
    		}
    		
    		
    		// Ask user if they want to exit or not: 
    		printf("\n\n\n\n Press 1 to exit, Press 2 for help or press any number to continue. ");
    		scanf("%i",&exit);
    				
    	}
    	
        while (exit != 1 );
    		
        system("exit");
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I would change that, to take advantage of (and get practice with), using simple functions. Instead of "doing everything in main()".

    The sooner you start working with functions, the sooner you'll get beyond crawling in your C programming.

    some idea's:

    Code:
    ...
    
    void menu(void);  //prototype for function menu
    
    int main()  {
    
      menu();  //call the menu display function
      
      print Program terminated, press enter when ready message
    
      return 0;
    }
    void menu(void) {  //same as the prototype, but remove the semi-colon, and add a curly brace
    
        int mchoice; colchoice; //menu choice, and color choice 
       //"bad beer rots our young guts but vodka goes well"
      char *colors = {"black", "brown", "red", "orange" } //etc.
    
    
    
      do {
        print  \n\n\t\tWelcome to the Menu/n/n
        print  Choose an option: \n\n
        print  1. Calculate a Resistance Value \n
        print  2. Help \n 
        print  3. Exit
        scanf( %d, &choice);
    
        if(choice == 1) {
          //logic for choice #1, here
          // get users color choice, perhaps where 0 = black, 1 = brown, 2 = red, etc.
          //get it? - if not print colors(0), colors(1), etc.
          //lots of ways to work this logic. Having another function (and moving colors[] to it,   
          //would be excellent. Keeps this menu logic short and clear.
    
        }
        else if(choice == 2) {
          //logic for choice #2, here
    
        }
        // #3 will exit the do while loop, and return to main, ending the program
    
    
      }while(choice != 3)
    This is not runnable code, just idea's, in rough code.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    The thing is we havent done function yet so we arent allowed to used it

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    anyone ????

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    "Anyone" what? If you have a specific issue, say what it is. We're not going to do the whole thing for you.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by nebula1 View Post
    The thing is we havent done function yet so we arent allowed to used it
    This is the kind of specific information that you definitely should have mentioned, in the first post!

    So instead of using function calls, you'll have to use some large if statements, that the code can "drop" into, when they're needed. For instance, when you need to show the secondary menu.

    What are you stuck on, right now?

    How forums really work is - if you go away for a couple of days or so, and don't respond with updated code, or questions, then we are start thinking that you've solved your programming question somehow, and won't be back. Then everyone loses interest in your programming topic.

    So if you want help, be *specific* about what you need, and *keep updating* your thread, with your updated code. That way, we can see what the heck is going on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM