Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Calculator default not working

calculator c simple calculator

Best Answer dargueta, 21 July 2014 - 12:58 PM

You have a breakpoint set there manually, probably by accident (the big blue arrow on the side). Just click on it to disable or remove it.

Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1 djsepstep

djsepstep

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 21 July 2014 - 10:01 AM

Hi guys I am working on this simple calculator for a coding class I am working on and would greatly appreciate some help. It is working almost completely fine except for this one problem. Here is the code:

#include <stdio.h>

int main()
{
    int choice;
    float number1;
    float number2;
    float total;
    char response;
    
    do
    {
        
        printf("\n\nWelcome to Sepehr Araghi's Handy Calculator\n\n");
        printf("\t1. Addition\n");
        printf("\t2. Subtraction\n");
        printf("\t3. Multiplication\n");
        printf("\t4. Division\n");
        printf("\t5. Exit\n\n");
        
        printf("\nWhat would you like to do? ");
        scanf("%d", &choice);
        
        switch (choice)
        {
            case 1:
                printf("\nPlease enter two numbers to add, seperated by a space: ");
                
                while ((scanf("%f %f", &number1, &number2)) != 2)
                {
                    while (getchar() != '\n')
                        continue;
                    printf("\nYou entered a letter, please re-enter both values: ");
                }
                
                total = number1 + number2;
                    
                printf("\nResult of adding %.2f and %.2f is %.2f\n", number1, number2, total);
                break;
                
            case 2:
                printf("\nPlease enter two numbers to subtract, seperated by a space: ");
                
                while ((scanf("%f %f", &number1, &number2)) != 2)
                {
                    while (getchar() != '\n')
                        continue;
                    printf("\nYou entered a letter, please re-enter both values: ");
                }
                
                total = number1 - number2;
                
                printf("\nResult of subtracting %.2f and %.2f is %.2f\n", number1, number2, total);
                break;
                
            case 3:
                printf("\nPlease enter two numbers to multiply, seperated by a space: ");
                
                while ((scanf("%f %f", &number1, &number2)) != 2)
                {
                    while (getchar() != '\n')
                        continue;
                    printf("\nYou entered a letter, please re-enter both values: ");
                }
                
                total = number1 * number2;
                
                printf("\nResult of multiplying %.2f and %.2f is %.2f\n", number1, number2, total);
                break;
                
            case 4:
                
                printf("\nPlease enter two numbers to divide, seperated by a space: ");
                
                do
                {
                    while ((scanf("%f %f", &number1, &number2)) != 2)
                    {
                        while (getchar() != '\n')
                            continue;
                        printf("\nThat is not a valid choice, please re-enter both numbers: ");
                    }
                    if (number2 == 0)
                    {
                        printf("\nYou can’t divide by zero, please re-enter both numbers: ");
                    }
                } while (number2 == 0);
                
                total = number1/number2;
                printf("\nResult of dividing %.2f and %.2f is %.2f\n", number1, number2, total);

                break;

            case 5:
                printf("\nThank you for using Sepehr Araghi's Handy Calculator.\n\n");
                break;
            
            default:  printf("Invalid selection choice!\n");
        }
        
        printf("\nWould you like to continue (y/n)? ");
        
        scanf("%c", &response);
        
        if(response == 'n' || response == 'N');
        
    }
    while (choice != 5 && choice != getchar());
    {
        printf("\nPlease select a numeric value from the main menu or else select 5 to quit\n");
    }
    printf("\n\n");
    return 0;
}

The issue I am having is that when I run it and put in a letter or a number that isn't one of the options in the menu, it stops the program and says that the default is the problem,  but I am not sure what is wrong with it exactly... Any help is greatly appreciated thanks! Also I am coding with xcode if that of any relevance.



#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 21 July 2014 - 12:30 PM

Put a continue after the printf in your default statement.

 

Never mind, ignore that. What do you mean by "the default is the problem?" 


Edited by dargueta, 21 July 2014 - 12:31 PM.

sudo rm -rf / && echo $'Sanitize your inputs!'


#3 djsepstep

djsepstep

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 21 July 2014 - 12:44 PM

When I run the program and the menu comes up to select an option, I put in a letter or value that isnt supposed to be part of the menu, and it should ask the user to re-input a new selection. When I do that however, it highlights the default in the switch code and stops the program. Here is a picture of it I hope this helps a bit. I tried to change it a bit but to no avail

 

2m5hr2w.png


Edited by sepsep96, 21 July 2014 - 12:48 PM.


#4 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 21 July 2014 - 12:58 PM   Best Answer

You have a breakpoint set there manually, probably by accident (the big blue arrow on the side). Just click on it to disable or remove it.


sudo rm -rf / && echo $'Sanitize your inputs!'


#5 djsepstep

djsepstep

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 21 July 2014 - 01:03 PM

That was it! Thanks so much for the help!






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download