Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with user input in Objective-C Foundation command line utility

I'm trying to make an application that solves the quadratic formula in Objective-C command line. My code so far is:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]){

double a, b, c, sol1, sol2;

NSLog(@"Welcome to the quadratic solver");

NSLog(@"Please enter the value of a");
scanf("%f", &a);

NSLog(@"Please enter the value of b");
scanf("%f", &b);

NSLog(@"Please enter the value of c");
scanf("%f", &c);

sol1 = (-b + sqrt( (double)pow(b,2) - 4 * a *c) ) / 2 * a;
sol2 = (-b - sqrt( (double)pow(b,2) - 4 * a *c) ) / 2 * a;

NSLog(@"If a is: %f, b is %f, and c is %f.", a, b, c);

NSLog(@"Solution one is: %f", sol1);
NSLog(@"Solution two is: %f", sol2);

}

But my output is not receiving any of the information.

2012-01-26 17:23:53.585 test[4595:707] Welcome to the quadratic solver
2012-01-26 17:23:53.588 test[4595:707] Please enter the value of a
1
2012-01-26 17:23:56.030 test[4595:707] Please enter the value of b
3
2012-01-26 17:23:56.558 test[4595:707] Please enter the value of c
-10
2012-01-26 17:23:58.670 test[4595:707] If a is: 0.000000, b is 0.000000, and c is 0.000000.
2012-01-26 17:23:58.672 test[4595:707] Solution one is: -0.000000
2012-01-26 17:23:58.672 test[4595:707] Solution two is: -0.000000

Any input is greatly appreciated!

like image 745
user1172534 Avatar asked Oct 08 '22 21:10

user1172534


1 Answers

You have inconsistent typing... you are scanning into %f, so you need to declare:

float a, b, c, sol1, sol2;

Your program then works fine on my machine.

like image 140
Conrad Shultz Avatar answered Oct 12 '22 11:10

Conrad Shultz