Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

vavdiyaharesh

macrumors newbie
Original poster
May 15, 2008
24
0
Hello
i m doing one small program that takes two number and one operator and does some calculation
following is my code
#include <stdio.h>
#include "Fraction.h"

main()
{

Fraction *frac=[[Fraction alloc] init];
float a;
printf("Enter first number:");
scanf("%f\n",&a);
float b;
//printf("Enter second number:");
//scanf("%f\n\n",&b);
printf("Press from the following\n");
printf("1 for Addition\n");
printf("2 for Substcration\n");
printf("3 for Multiplication\n");
printf("4 for Division\n");
int op;
scanf("%d\n",&op);


printf("%f %f %d\n",a,b,op);
[frac calc: a : b : op];
[frac free];

}


but whenever execute this program
actually this program should give like "Enter first number " & then "Enter Second number " and "Enter operator" but it gives me like enter first number then i have to enter two number then it goes to next and then enter second number and then enter your choice
so it takes 4 values instead of 3
pls help me
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
Please use [code] and [/code] tags in the future.

scanf() will typically leave the newline character in the input buffer. You can use getchar() to put it off prior to doing the second scanf() (which you currently have commented out).

Your description of the problem is, like, incoherent.
 

vavdiyaharesh

macrumors newbie
Original poster
May 15, 2008
24
0
Yes buddy,
Before i have written "%f\n" and thats why it asks for values twice and then after i have removed "\n" then it works perfectly.

Thankx buddy.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.