1

I was supposed to make a program that would be a calculator using functions to provide results. I got it down and everything is working but I'm having a slight issue with the printing function out of everything which is humorous; for some reason that I can't seem to get my head around whenever the program prints a solution it also prints the first selection which is add(num1, num2) and I can't for the life of me figure out why. Any input would be greatly appreciated! Thank you! Here is my code:

//Function to Add
int add (int a, int b);
//Function to Substract
int sub (int a, int b);
//Function to Multiply
int mul (int a, int b);
//Function to Divide
int div (int a, int b);
//Function to get remainder
int rem (int a, int b);
//Function to print menu
int printmenu();

//Begin main
int main()
{
    //Initialize Variables
    int num1=0;
    int num2=0;
    unsigned int selection =0;
    
    //Loop
    while ((selection=printmenu()) != 6)
    {
        switch (selection)
        {
        //Addition Case
        case 1: 
            printf("Enter two numbers: ");
            scanf("%d %d", &num1 , &num2 );
            printf("%d added to %d is %d",num1,num2,add(num1,num2));
            break;
        case 2:
            printf("Enter two numbers: ");
            scanf("%d %d", &num1 , &num2 );
            printf("%d substracted to %d is %d",num1,num2,sub(num1,num2));
            break;
        case 3:
            printf("Enter two numbers: ");
            scanf("%d %d", &num1 , &num2 );
            printf("%d multiplied to %d is %d",num1,num2,mul(num1,num2));
            break;  
        case 4:
            printf("Enter two numbers: ");
            scanf("%d %d", &num1 , &num2 );
            printf("%d divided to %d is %d",num1,num2,div(num1,num2));
            break;
        case 5:
            printf("Enter two numbers: ");
            scanf("%d %d", &num1 , &num2 );
            printf("%d divided to %d has a remainder of %d",num1,num2,rem(num1,num2));
            break;
        case 6:
            return 0;
        default:
            printf("That is not a valid choice.");
            break;
        }//End switch
    }//End while
    
    return 0;   
}//End main


//Function to add
int add (int a, int b)
{
    int total=0;
    total = a + b;
    return total;
}

//Function to substract
int sub (int a, int b)
{ 
    int total=0;
    total = a - b;
    return total;
}

//Function to multiply
int mul (int a, int b)
{
    int total=0;
    total = a * b;
    return total;
}

//Function to Divide
int div (int a, int b)
{
    int total=0;
    total = a / b;
    return total;
}

//Function to get Remainder
int rem (int a, int b)
{
    int total=0;
    total = a % b;
    return total;
}

//Function to Print Menu
int printmenu()
{
    int selection=0;
    //Print menu
    printf("1.Add\n");
    printf("2.Substract\n");
    printf("3.Multiply\n");
    printf("4.Divide\n");
    printf("5.Remainder\n");
    printf("6.Exit\n");
    printf("Selection: ");
    scanf("%d", &selection);
    return selection; 
}
Mestica
  • 1,489
  • 4
  • 23
  • 33

1 Answers1

2

When you print the result of the operation, you aren't including a newline at the end of the output. So when you print the menu, the first line of the menu gets printed on the same.

Add \n at the end of each result printing statement, ex.:

printf("%d added to %d is %d\n",num1,num2,add(num1,num2));
dbush
  • 205,898
  • 23
  • 218
  • 273