• Log in or sign up for an account here: https://www.sythe.org/login

My Second C Program... A Calculator...

Zamurick

Member
Joined
Jun 4, 2006
Sythe Gold
0
Messages
53
My Second C Program... A Calculator...
Okay, so I've made two programs in C so far... I skipped the hello world thing as usual for me, but anyway, this is my second program... It opens up in the command prompt. Well, anyway here it is! The source is here:
Code:
#include <stdio.h>

main()

{

	int Loop=0;
	int Type=0;
	float AddToNum=0;
	float AddNum=0;
	float AddResult=0;
	float SubtractFromNum=0;
	float SubtractNum=0;
	float SubtractResult=0;
	float MultiplyByNum=0;
	float MultiplyNum=0;
	float MultiplyResult=0;
	float DivideByNum=0;
	float DivideNum=0;
	float DivideResult=0;

	printf("\nSimple Calculator, by Zamurick\n");
while ( Loop < 1 ) {

	printf("\nHere are your options: \n");
	printf("1	Add\n2	Subtract\n3	Multiply\n");
	printf("4	Divide\n5	Exit");
	printf("\nType Your Choice (1-5):  ");
	scanf("%d", &Type);

if (Type==1) {

	printf("\nEnter the number you would like to add to:  ");
	scanf("%f", &AddToNum);
	printf("\nEnter the number you want to add:  ");
	scanf("%f", &AddNum);
	AddResult = AddToNum + AddNum;
	printf("\nThe sum of those numbers is %.8f.\n", AddResult);
}
else if (Type==2) {


		printf("\nEnter the number you would like to subtract from:  ");
	scanf("%f", &SubtractFromNum);
	printf("\nEnter the number you want to subtract:  ");
	scanf("%f", &SubtractNum);
	SubtractResult = SubtractFromNum + SubtractNum;
	printf("\nThe difference of those numbers is %.8f.\n", SubtractResult);
}
else if (Type==3) {

		printf("\nEnter the number you would like multiply:  ");
	scanf("%f", &MultiplyByNum);
	printf("\nEnter the other number you want to multiply:  ");
	scanf("%f", &MultiplyNum);
	MultiplyResult = MultiplyByNum * MultiplyNum;
	printf("\nThe result of multiplying those numbers is %.8f.\n", MultiplyResult);

}
else if (Type==4) {

		printf("\nEnter the number you would like to divide:  ");
	scanf("%f", &DivideNum);
	printf("\nEnter the number you want to divide by:  ");
	scanf("%f", &DivideByNum);
	DivideResult = DivideNum / DivideByNum;
	printf("\nThe result of dividing those numbers is %.8f.\n", DivideResult);

}
if (Type==5) {
	Loop++;
	printf("Goodbye.");
}
}
}
 
My Second C Program... A Calculator...
Not a bad program, however, what happens when a user enters a letter, or a number outside of the range 1-5? Not a big problem, especially seeing as it's your second program, but a problem none the less.

It's pretty easy to fix. I believe I have solved it in a thread about my paperscissorsrock game. It took me awhile to think of it, and in the end i needed help doing it. But yeah, you should be able to use the code as is, or with a few simple modifications so your C compiler doesn't chuck a hissy at it.

Anyway, good luck with your learning C.
 
My Second C Program... A Calculator...
I have already compiled it and it worked fine. But I will see if I can figure out how to use the isdigit function in the program, for the sake of learning.
 
My Second C Program... A Calculator...
Haha, what I mean is run the program and type in h or any other letter.
 
My Second C Program... A Calculator...
Yeah, it goes crazy... It's like it does an infinite loop so fast I can't see anything clearly...
 
My Second C Program... A Calculator...
Offtopic: Zamurick, do you believe C to be easier than C++?

Please let me know. I hate C++ in windows, that's why.
 
My Second C Program... A Calculator...
I haven't tried C++, but every book on C tells me to learn C before C++...
 
Runescape Gamblingrunescape gambling
Back
Top