PDA

View Full Version : my first C app (sort of)



Shuttleu
02-09-2009, 11:32 PM
well i have been learning C for about a day now and i have come up with my sort of first app
its a calculator (pretty shitty acctually)
anyway please coment on when you think

~shut

EDIT: as requested
the source
#include <stdio.h>

int main()
{
int x, i, y, a, b;

printf("How many calculations do you have: ");
scanf("%d", &x);
for(i=0; i<x; ++i){
printf("would you like to do?\n");
printf("1: *\n");
printf("2: /\n");
printf("3: +\n");
printf("4: -\n");
printf("Enter your choice: ");
scanf("%d", &y);
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
switch(y){
case 1:
printf("%d * %d = %d\n", a, b, a*b);
break;
case 2:
printf("%d / %d = %f\n", a, b, (float)a/b);
break;
case 3:
printf("%d + %d = %d\n", a, b, a+b);
break;
case 4:
printf("%d - %d = %d\n", a, b, a-b);
break;
default:
printf("Incorrect number\n");
break;
}
}
getch();
return 0;
}

anonymity
02-09-2009, 11:52 PM
Hey, I thought it worked great... I will admit that I was a little confused when it asked what I wanted to do... I kept typing in the symbol ..... not the number to what I wanted to do... but I thought that it was a great program... genius... how did you make it... If I may ask?

Shuttleu
02-09-2009, 11:54 PM
Hey, I thought it worked great... I will admit that I was a little confused when it asked what I wanted to do... I kept typing in the symbol ..... not the number to what I wanted to do... but I thought that it was a great program... genius... how did you make it... If I may ask?

well im sure anyone can do this so i will add the scource to the main post

~shut

Iroki
02-10-2009, 06:41 PM
you need to add failsafe when you're dividing
cus you can't divide by 0 :)

JAD
02-10-2009, 08:51 PM
Yeah you should practice input validation as said above. Also, if you wanted to make it so that somebody could put in a long equation (such as 4 / 2 * 17 - 14 + 3), you could use getline to take the whole line in, and then break down and read the string part by part and do calculations based on that. That would be something a little more complicated to work on.

Just a suggestion. I don't even know if there is a getline in c or if there is a string manipulation class, but in C++ I made something similar to what I talked about.

Good job!

Shuttleu
02-10-2009, 09:01 PM
Yeah you should practice input validation as said above. Also, if you wanted to make it so that somebody could put in a long equation (such as 4 / 2 * 17 - 14 + 3), you could use getline to take the whole line in, and then break down and read the string part by part and do calculations based on that. That would be something a little more complicated to work on.

Just a suggestion. I don't even know if there is a getline in c or if there is a string manipulation class, but in C++ I made something similar to what I talked about.

Good job!
there is a getline in C
its fgets
also i made it so it can now divide properly

~shut