Thread: Calulator program issues

  1. #1
    Registered User
    Join Date
    Aug 2012
    Location
    United States
    Posts
    8

    Calulator program issues

    2.cppHello I will be grateful with any assistance. I kept thinking I had the program and now its almost due and it can't compile.

    the output should be
    1. addition
    2. subtraction
    3. multiplciation
    4. division
    5. exit

    please enter the operation?
    please enter two numbers?
    the result of %f and %f is %f.

    I am using a switch and while statemeant. I still cant get the numbered menu, the book has plenty of letters but no numbers! I also have trouble declaring and assigning.
    I attached what i do have...

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I'll post the code for everyone -
    Code:
    #include<stdio.h>
    float N1;
    float N2;
    float result;
    int op;
    int get_choice();
    int get_first();
    int main()
    
    
    { 
        int choice;
    
    
        printf("Welcome to Colleen Gonzales's Handy Calculator\n\n\n");
        printf("1. Addition\n");
        printf("2. Subtraction\n");
        printf("3. Multipication\n");
        printf("4. Division\n");
        printf("5. Exit\n");
    
    
        printf("What would you like to do?\n");
        scanf("%d", &choice);
    
    
        printf("\n\nPlease enter two numbers seperated by a space:\n ");
        scanf("%f %f", &N1, &N2);
    
    
        {
            int choice;
    
            switch (choice)
            {
                case '1' : result = N1 + N2;
                    break;
                case '2' : result = N1-N2;
                    break;
                case '3' : result = N1 * N2;
                    break;
                case '4' : result = N1/N2;
                    break;
                case '5' : printf("Goodbye");
                    break;
                default  : printf("Program Error!\n");
                    break; /*end of switch*/
            }
    
    
        else
            printf("Please respond with 1, 2, 3, 4, or 5. \n");
    
    
        while (   (')
        }
    
    
        printf("Goodbye");
    
    
        return 0;
    }
    
    
    		
    {
            float N1;
            float N2;
            float result;
    
    
    	
            printf("The result of adding %f and %f is %.2f\n", &N1, &N2, &result);
            return 0;
    }

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    1 - You can't have an else statement after a switch statement
    2 - The "While" statement has been incorrectly written (look at the brackets)
    3 - You appear to have a function after main that has no heading - You don't have to use it, but you do have to set it up right


    Code:
    else
        printf("Please respond with 1, 2, 3, 4, or 5. \n");
    
    
    while (   (')
        }
    
    
        printf("Goodbye");
    
    
        return 0;

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Your scanf call on line 21 uses a "%d" to read in an integer (choice). Your switch statement checks that against the characters '1', '2', ..., '5', which are different than 1, 2, ..., 5. Look at an ASCII table to understand the difference. You want the number version, i.e.
    Code:
    case 1: // notice, no ' ' around the number
    Also, to Click_here's point about not using else with a switch, that is the purpose of the default case. Instead of just printing "Program Error!", print "Please respond with 1, 2, 3, 4, or 5. \n".

    Lastly, if you want the program to repeat when the user enters incorrect input, you need to wrap everything that needs to repeat in a while loop. Or better yet, use a do-while loop, since they always run once (they always ask the user for input once before deciding what to do):
    Code:
    do {
        // print menu choices
        // read input
    while (input not valid);
    // calculate & print output

  5. #5
    Registered User
    Join Date
    Aug 2012
    Location
    United States
    Posts
    8
    Thank you so much for responding and posting up the code. the else...was a thought i took it out. But the while is what i'm unsure how to modify for numbers. I understand if the list was letter you could use (just copying a portion from a textbook I have)
    Code:
    {
    int ch;
    a. addition
    b. subtraction
    c. multiplication
    d. division
    e. exit
    ch = get_first();
    while (   ch< 'a' || ch> 'd') && ch !='q')
    {
    printf( "please respond with a, b, c, d, or e\n);
    ch = get_ first();
    }
    return ch;
    }
    but what im lost on is how would you convert for integers? I tried isdigit() and just replaced ch for int- both failures. Also how would you assign each number to correct operation.

  6. #6
    Registered User
    Join Date
    Aug 2012
    Location
    United States
    Posts
    8
    Thank you for reponding. I saw ur from long beach...im an ee major at long beach state, my first programming class. Thought that was cool .

  7. #7
    Registered User
    Join Date
    Aug 2012
    Location
    United States
    Posts
    8
    I fixed it! it has zero errors but does not compile. Anyone know what is happening?
    Code:
    #include<stdio.h>
    float choice;
    float result;
    float num1, num2;
    int get_choice();
    int main()
    {
    {
    float num1, num2;
    int choice;
    float result;
    do
    {
    printf("Welcome to Colleen Gonzales's Handy Calculator");
    printf("1. Addition\n");
    printf("2. Subtract\n");
    printf("3. Multipilcation\n");
    printf("4. Division\n");
    printf("5. Exit\n");
    printf("\n\nPlease pick an operation.\n");
    scanf("%f", &choice);
    }
    while( (choice = 1,2,3, 4 , 5) );
    printf("Please choose two numbers seperated by a space:");
    scanf("%f %f", &num1, &num2);
    return 0;
    }
    { 
    float num1, num2, result;
    int choice;
    while( (choice=get_choice()) !=5)
    {
    switch (choice)
    {
    case 1 : result = num1 + num2;
    break;
    case 2 : result = num1-num2;
    break;
    case 3 : result = num1 * num2;
    break;
    case 4 : result = num1/num2;
    break;
    case 5 : printf("Goodbye");
    break;
    default :printf("Please respond with 1, 2, 3, 4, or 5\n");
    break; /*end of switch*/
    }
    }
     
    printf("The result of %f and %f is %.2f\n", &num1, & num2, &result);
    return 0;
    }
    }
    
     #include<stdio.h>
    float choice;
    float result;
    float num1, num2;
    int get_choice();
    int main()
    {
    {
    float num1, num2;
    int choice;
    float result;
    do
    {
    printf("Welcome to Colleen Gonzales's Handy Calculator");
    printf("1. Addition\n");
    printf("2. Subtract\n");
    printf("3. Multipilcation\n");
    printf("4. Division\n");
    printf("5. Exit\n");
    printf("\n\nPlease pick an operation.\n");
    scanf("%f", &choice);
    }
    while( (choice = 1,2,3, 4 , 5) );
    printf("Please choose two numbers seperated by a space:");
    scanf("%f %f", &num1, &num2);
    return 0;
    }
    { 
    float num1, num2, result;
    int choice;
    while( (choice=get_choice()) !=5)
    {
    switch (choice)
    {
    case 1 : result = num1 + num2;
    break;
    case 2 : result = num1-num2;
    break;
    case 3 : result = num1 * num2;
    break;
    case 4 : result = num1/num2;
    break;
    case 5 : printf("Goodbye");
    break;
    default :printf("Please respond with 1, 2, 3, 4, or 5\n");
    break; /*end of switch*/
    }
    }
     
    printf("The result of %f and %f is %.2f\n", &num1, & num2, &result);
    return 0;
    }
    } 


  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your program looks like something the cat dragged in?

    Formatting your code is VERY important. As you code more, your brain gets to recognize hundreds of C syntax patterns, based in large part, on their formatting.

    You just wiped that out - I would not even start to work through it, in it's current mess.

    Don't mean to be mean - just the way it is.

  9. #9
    Registered User
    Join Date
    Aug 2012
    Location
    United States
    Posts
    8
    No worries . Thank you for replying. So its that bad. I would start from scratch but i've been woking on the program for about a week and its due in a few hours. I am sorry and even embarresed experts like you see first time programs like mine..but we all start somewhere. I've been working on it and made some corrections...
    Code:
    #include<stdio.h>
    float result;
    float num1, num2;
    int get_choice();
    int main()
    {
    {
    float num1, num2;
    int choice;
    float result;
    do
    {
    printf("Welcome to Colleen Gonzales's Handy Calculator");
    printf("1. Addition\n");
    printf("2. Subtract\n");
    printf("3. Multipilcation\n");
    printf("4. Division\n");
    printf("5. Exit\n");
    printf("\n\nPlease pick an operation.\n");
    scanf("%f", &choice);
    }
    while( (choice = 1,2,3, 4 , 5) );
    printf("Please choose two numbers seperated by a space:");
    scanf("%f %f", &num1, &num2);
    return 0;
    }
    { 
    float num1, num2, result;
    int choice;
    while( (choice=get_choice()) !=5)
    {
    switch (choice)
    {
    case 1 : result = num1 + num2;
    break;
    case 2 : result = num1-num2;
    break;
    case 3 : result = num1 * num2;
    break;
    case 4 : result = num1/num2;
    break;
    case 5 : printf("Goodbye");
    break;
    default :printf("Please respond with 1, 2, 3, 4, or 5\n");
    break; /*end of switch*/
    }
    }
     
    printf("The result of %f and %f is %.2f\n", &num1, & num2, &result);
    return 0;
    }
    }
    

  10. #10
    Registered User
    Join Date
    Aug 2012
    Location
    United States
    Posts
    8
    yikes you saw the 2nd draft. well have a nice day=)

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm not asking you to start over - just take 10 minutes and format it!

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    Code:
    while( (choice = 1,2,3, 4 , 5) );
    As Adak said in the previous thread, this doesn't do what you want. however it is legal C code and an interesting case since it will compile. but it uses the comma operator (google it) which will evaluate each expression in the comma list beginning with 'choice=1'. the others are legal expressions but they don't do anything. the result of the entire expression is the last value in the list, '5'. and since it is nonzero it will continue to loop. and because you have a semicolon at the end, it will just go into an infinite loop doing nothing but executing the same expression over and over. try this to see the results.
    Code:
    #include <stdio.h>
    int main(int argc,char *argv[])
    {
    	int choice;
    	int x;
    
    	printf("%d\n",choice=1);			// this sends one argument to printf, the results value of the expression choice=1
    	printf("%d\n",choice=1,2);			// this sends two arguments to printf, but it only uses the first one due to the format statement only having one %d
    	printf("%d\n",(choice=1,2));		// this sends one argument to printf, the result of the comma operator expression
    	printf("%d\n",choice=1,2,3,4,5);	// this sends five arguments to printf, but it only uses the first one
    	printf("%d\n",(choice=1,2,3,4,5));	// this sends one argument to printf, the result of the comma operator expression
    	printf("%d\n",choice=(1,2,3,4,5));	// this sends one argument to printf, the result of the assignment expression choice = (...
    	// in all cases except the last one the value of 'choice' will be 1. 
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. First Program Issues
    By sfr1 in forum C++ Programming
    Replies: 4
    Last Post: 07-29-2009, 06:43 PM
  2. issues with program need help
    By clipsit in forum C Programming
    Replies: 6
    Last Post: 04-03-2008, 09:24 AM
  3. Well having issues with this other program here...
    By Junior89 in forum C++ Programming
    Replies: 2
    Last Post: 05-06-2007, 10:39 PM
  4. Very larger integer calulator
    By Dewayne in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2004, 07:25 PM