Need HELP with Program

This is a discussion on Need HELP with Program within the C Programming forums, part of the General Programming Boards category; Ok..I got my second assignment in my programming class last week and I am getting frustrated. Ive gotten as far ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    24

    Need HELP with Program

    Ok..I got my second assignment in my programming class last week and I am getting frustrated. Ive gotten as far as displaying the menu but i am stuck and have no clue what to do next. I am still very new at this and im not very good. Can anyone help? If you need to see my code i can post what i have. Thanks


    The assignment is

    1. create a program that displays a menu with 6 options as seen in the example below.

    2. Accept numbers from the user and calculate the selected total, product, average, minimum, or maximum.

    Sample Screen Interaction:
    Black is the program's output.
    Blue is the user's input.

    1 - Calculate the Total
    2 - Calculate the Product
    3 - Calculate the Average
    4 - Calculate the Minimum
    5 - Calculate the Maximum
    9 - Exit
    Enter selection: 1

    Enter numbers (Enter 0 to stop):
    2
    3
    4
    5
    0

    You entered 4 numbers with a Total of 14.

    1 - Calculate the Total
    2 - Calculate the Product
    3 - Calculate the Average
    4 - Calculate the Minimum
    5 - Calculate the Maximum
    9 - Exit

    Enter selection: 9

    Thank you come again.


    The program should include the following functions:

    void findTotal( )
    This function will accept numbers from the user until 0 has been entered.
    When 0 has been entered, the number of values entered will be displayed followed by the sum of all the numbers entered, as seen in the above example.

    void findProduct( )
    This function will accept numbers from the user until 0 has been entered.
    When 0 has been entered, the number of values entered will be displayed followed by the product of all the numbers entered, as seen in the above example.

    void findAverage( )
    This function will accept numbers from the user until 0 has been entered.
    When 0 has been entered, the number of values entered will be displayed followed by the average of all the numbers entered, as seen in the above example.

    void findMinimum( )
    This function will accept numbers from the user until 0 has been entered.
    When 0 has been entered, the number of values entered will be displayed followed by the minimum of all the numbers entered, as seen in the above example.

    void findMaximum( )
    This function will accept numbers from the user until 0 has been entered.
    When 0 has been entered, the number of values entered will be displayed followed by the maximum of all the numbers entered, as seen in the above example.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,170
    Get a response from the user and then do something like this:
    Code:
    switch(choice)
    {
      case '1':
        findTotal();
        break;
      case '2':
        findProduct();
        break;
      .
      .
      .
      default:
        puts("Invalid selection");
    
    }
    That's the way I'd do it anyway. That's the beauty of programming...there's usually a bunch of different ways to accomplish the same task. That's just one idea that I gave you, but I can think of at least 3 other ways to do it off the top of my head.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    24
    Ok so i got this. Whats wrong with the way i have it. I dont really understand how to use the switch staments. Its obviously not correct cause it wont compile. What else do i need to include?

    Code:
    #include <stdio.h>
    
    int main()
    
    {
    printf(" 1-Calculate the Total\n 2-Calculate the Product\n 3-Calculate the Average\n 4-Calculate the Minimum\n 5-Calculate the Maximum\n 9-Exit");
    
    printf("\n\nEnter Selection: ");
    }
    
    
    
    switch(choice)
    
     { case '1':
        findTotal();
        break;
     
      case '2':
        findProduct();
        break;
      
      case '3':
      	 findAverage();
        break;
      
      case '4':
        findMinimum();
    	 break;
      
      case '5':
       findMaximum();
       break;
      
      
      default:
        puts("Invalid selection");
    
    }

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,170
    Well, you need to actually get the input from the user first. Check out getchar() or scanf() or fgets() or something.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    24
    Ok i got the user imput. Now how do i actually calculate all this?

    Code:
    #include <stdio.h>
    void main()
    
    {
    
    int choice;
    printf(" 1-Calculate the Total\n 2-Calculate the Product\n 3-Calculate the Average\n 4-Calculate the Minimum\n 5-Calculate the Maximum\n 9-Exit");
    
    printf("\n\nEnter Selection: ");
    scanf("%d",&choice);
    }
    
    
    switch(choice)
    
     { case '1':
        findTotal();
        break;
     
      case '2':
        findProduct();
        break;
      
      case '3':
      	 findAverage();
        break;
      
      case '4':
        findMinimum();
    	 break;
      
      case '5':
       findMaximum();
       break;
      
      
      default:
        puts("Invalid selection");

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    10
    To calculate all that I believe(I'm new here so don't quote me on it, just what I have learned till now) you would need something in the order of:

    Code:
    printf (" The sum is %d\n", a+b+c+d);
    printf (" The Average is %d\n", (a+b+c+d)/4);
    printf (" The Product is %d\n", a*b*c*d);
    You would of course need to declare the variables.

    For the minimum and largest I know a way of doing it but it is not very efficient and I actually have a post on it just now, but anywayz here it is:

    Code:
    smallest = a;
        if (b < smallest)
            smallest = b;
        if (c < smallest)
            smallest = c;
        if (d < smallest)
            smallest = d;
           
           printf ( results, "smallest is %d\n", smallest);
           
        largest = a;
        if (b > largest)
            largest = b;
        if (c >largest)
            largest = c;
        if (d >largest)
            largest = d;
       
            printf ( results, "largest is %d\n", largest);
    There you go, hope this helps you out in some way

  7. #7
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    The problem with your code is that your user input is in the form of an int, but your case statements are checking for a char. '1' is not the same as 1. Remove the single quotes and the case statements should get executed.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    24
    I obviously dont understand what i am doing, can someone tell me where i am going wrong and tell me what i need to do i would greatly apreciate it. Thanks

    Code:
    #include <stdio.h>
    void main()
    
    {
    char choice;
    int a,b,c,d,e,total,product,average;
    
    printf(" 1-Calculate the Total\n 2-Calculate the Product\n 3-Calculate the Average\n 4-Calculate the Minimum\n 5-Calculate the Maximum\n 9-Exit");
    printf("\n\nEnter Selection: ");
    scanf("%d",&choice);
    
    Total = a + b + c + d + e;
    Product = a * b * c * d * e;
    Average = ( a + b + c + d + e) / 4;
    }
    
    
    switch(choice)
    
     { case 1:
        findTotal();
        break;
     
      case 2:
        findProduct();
        break;
      
      case '3':
      	 findAverage();
        break;
      
      case '4':
        findMinimum();
    	 break;
      
      case '5':
       findMaximum();
       break;
          
      
      default:
        puts("Invalid selection");
    
    }

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,170
    Well, it being your second assignment and all, I'm assuming the instructor has taught you all you need to know to complete the assignment. So, take the stuff you've learned and apply this basic theory to some workable code.

    1. display menu
    2. get choice from user
    3. if choice is equal to 1 call function that prints total.
    4. otherwise if choice is equal to 2 call function that prints product.
    5. otherwise if choice is equal to 3 call function that prints average.
    6. otherwise if choice is equal to 4 call function that prints minimum.
    7. otherwise if choice is equal to 5 call function that prints maximum.
    8. otherwise if choice is equal to 6 exit program.
    9. return to step 1.

    As I mentioned earlier, there's several ways to accomplish this. Take what you've learned and apply it. For instance, if you've learned if/else, but you haven't learned switch/case then forget using switch/case because it's only going to confuse you. It can be written just as well using if/else and that's probably what you've learned by your second assignment.

    The actual computations and printing will happen in the individual functions (findTotal(), etc.) since the instructor told you those functions will return type void. Don't let us confuse you by giving you code that you haven't learned yet. Just take what you've learned from your instructor and run with it
    Last edited by itsme86; 09-21-2004 at 09:55 PM.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    Your case statements are still comparing an int against a char. Remove the single quotes from '3', '4' and '5'. Ok, take this example. A switch block can be converted into if/else:

    Code:
    if (1 == '1')
        printf("true");
    else
        printf("false");
    If you run this, you'll notice that it will print "false". Why? Because the char '1' is a character. Each character maps to a numerical value on the ASCII chart. If you did:

    Code:
    printf("%d\n", '1');
    it prints out 49. So when your input from the user is an integer value 1 and you compare it against character '1', then the result is false, because it's comparing if 1 == 49.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    24
    Ok this is what i have so far. Now that i can accept input from the user , what do i do to make it figure what it is suppose to figure. Where do i put the void findTotal() for example.

    Code:
    #include <stdio.h>
    main()
    
    {
    int selection,total,product,average,minimum,maximum;
    
    printf(" 1-Calculate the Total\n 2-Calculate the Product\n 3-Calculate the Average\n 4-Calculate the Minimum\n 5-Calculate the Maximum\n 9-Exit");
    printf("\n\nEnter Selection: ");
    scanf("%d",&selection);
    
    switch(selection)
    {
    case 1: printf("\n\nEnter Numbers (Enter 0 to stop):");
    break;
    
    
    case 2: printf("\n\nEnter Numbers (Enter 0 to stop):");
    break;
    
    
    case 3: printf("\n\nEnter Numbers (Enter 0 to stop):");
    break;
    
    case 4: printf("\n\nEnter Numbers (Enter 0 to stop):");
    break;
    
    
    case 5: printf("\n\nEnter Numbers (Enter 0 to stop):");
    break;
    
    case 9: printf("\n\nThank You, Come Again!");
    break;
    
    default: printf("\n\nInvalid Entry!");
    
    }
    Last edited by Jeff; 09-22-2004 at 05:22 PM.

  12. #12
    Registered User
    Join Date
    Sep 2004
    Posts
    24
    Can someone help me please

  13. #13
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    Ok, Ill show you an example for the first case statement.

    Code:
    /* declare function prototypes for each function you create */
    void findTotal();
    
    int main(void)
    {
        .
        .
        .
        case 1: findTotal(); break
        .
        .
        .
    }
    
    
    void findTotal()
    {
        int total = 0;
        int input = 0;
    
        printf("Enter numbers (0 to stop)\n");
        do
        {
            scanf("%d", &input);
            total += input;
        } while (input != 0);
    
        printf("Total: %d\n", total);
    }
    You should be able to figure the rest from here.
    Last edited by Nessarose; 09-23-2004 at 12:45 AM.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Jeff
    Can someone help me please
    Don't bump your threads. Go read the Announcements over again, if you even read them the first time. Here, I'll make it clear for you:
    5. Don't bump your threads, (Bumping: posting messages on your own threads to move them up the list).
    We are not your personal servents. We don't get paid for this. We do it because we enjoy it. We do not enjoy people breaking the rules, even though it happens all the time. Just because it happens, doesn't mean it's right.

    While it is nice you actually used code tags, assuming someone didn't edit your post for you and add them, you should indent your code so it's actually readable.

    The "void findtotal" you talk about, as listed in your homework's description, is a rough prototype of what the function should be called, and that it doesn't need to return anything.

    You do know how to make functions, right? Well how about making one? In case you don't know how, just look at main, because it too is a function. So you do the same sort of thing.

    You put your functions either:
    a) Before main, so main can see them.
    b) Or you put them after it, and prototype them.

    Give it a try, pust your latest attempt, including properly indented and readable code, after you go and read the Announcements again.

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

  15. #15
    Registered User
    Join Date
    Sep 2004
    Posts
    24
    ok i figured all of it out but the max and min. this is what i have for the min. i dont think it is correct? does anyone know
    Code:
    void findMinimum()
    {
    	int minimum = 0;
    	int input = 0;
    	int count = 0;
    	printf("\nEnter Numbers (Enter 0 to Stop): ");
    	
    do
    {
    	scanf("%d",&input);
    	if (input <= minimum)
    	count ++;
    }	
    	while (input != 0);
    	printf("\nYou entered %d numbers with a minimum of: %d",count, minimum);
    	
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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