0

I've written a special calculator which prompts the user for 2 numbers then displays a menu which basicly asks the user what to do with that input. It works great however no matter what numbers I input the result is 0. What am I doing wrong?

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>



int main()
{
    char a, rad, patrat;
    float x, y, media, radical, pat1, pat2;
    patrat = 253;
    rad = 251;
    loop:
    printf("Input 2 numbers...\n");
    scanf("%f %f", &x, &y);
    media = (x+y)/2;
    radical = sqrt(x+y);
    pat1 = x*x;
    pat2 = y*y;
    loop2:
    printf("\n \nA - Arithmetic media.\n");
    printf("B - Square root.\n");
    printf("C - Sqare of the 2 numbers.\n");
    printf("D - Write other numbers.\n");
    printf("E - Terminate the program.\n");
    a = getch();
    switch(a) {
    case'a':
    system("cls");
    printf("Media of the 2 numbers is %f", &media);
    goto loop2;

    case'b':
    system("cls");
    printf("%c%f + %f = %f", &rad, &x, &y, &radical);
    goto loop2;

    case'c':
    system("cls");
    printf("%f%c = %f, %f%c = %f", &x, &patrat, &pat1, &y, &patrat, &pat2);
    goto loop2;

    case'd':
    goto loop;

    case'e':
    return 0;
    }
    }
Bugster
  • 1,552
  • 8
  • 34
  • 56
  • 3
    You may want to read first [this article](http://www.cs.utexas.edu/~EWD/transcriptions/EWD02xx/EWD215.html) and/or [this question](http://stackoverflow.com/questions/46586/goto-still-considered-harmful) and try to restructurate your code ;) – Kevin Jan 04 '12 at 09:00
  • EDIT: I'll look into it. – Bugster Jan 04 '12 at 09:00
  • Wow! Goto - not seen one of those in a while. – Tom Jan 04 '12 at 09:02
  • I actually use goto alot when I have long programs, for debugging purpose since I don't want to open my program 100 times I just make it loop :-) – Bugster Jan 04 '12 at 09:05
  • 1
    The read the links above from Kevin and stop. – Tom Jan 04 '12 at 09:08
  • In your program, "square" is spelled wrong – alexyorke Jan 04 '12 at 12:21

4 Answers4

1

You're using & in your printf statements, you shouldn't be. Scanf has it as it's writing so takes pointers.

Tom
  • 43,583
  • 4
  • 41
  • 61
1

Why are you using the operator & to your printf arguments?

printf doesn't take pointer arguments for %f conversion specification

float b;
scanf("%f", &b);

but

float a = 42;
printf("%f\n", a);
ouah
  • 142,963
  • 15
  • 272
  • 331
1
printf("Media of the 2 numbers is %f", &media);

should be

printf("Media of the 2 numbers is %f", media);

similarly for all other printf()

Generally goto statements are considered harmful when they are called backwards! So please avoid them. The same functionality can be done by a while(1) for for(;;) loop with proper termination condition.

Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
0

In your printf statement, you are using

    printf("Media of the 2 numbers is %f", &media);

&media is the address of your variable media. In scanf, we provide the address of the variable as parameter so that it stores the values at that address. But in printf, we provide the variable value and not the variable address. If you provide variable address then it would print the address and not the value. So the correction should be

   printf("Media of the 2 numbers is %f", media);
Froyo
  • 17,947
  • 8
  • 45
  • 73