Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 24th, 2005, 6:22 PM   #1
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4 Jessehk is on a distinguished road
simple calculator

Okie dokie, time to show off my beginner C skills :p

I made this calculator using simple commands, and basic C language.

Using the functions and commands that I used, could you recommend any changes?


/*This is a calculator program. It may be  edited, but the original author must be mentioned.  *
*it was written on 2005-06-23 by Jesse H-K */

#include <stdio.h>

#define FALSE 0
#define TRUE !FALSE

int main()
{
	
	float num1, num2, total;
	char oper;
	int trigger, choice, done;
	
	done = FALSE;
	while(!done) 
	{
                 printf("\n\n\n\t\t~~ C  A  L  C  U  L  A  T  O  R ~~");
		printf("\n\nThis will calculate the answer to a SIMPLE math question (eg: 2 * 3)");
		printf("\nOperators are as follows:");
		putchar('\n');
		putchar('\n');
		puts(" / = divide "); 
		puts(" * = multiply ");
		puts(" + = add");
		puts(" - = subtract");
		putchar('\n');
		printf("Enter the first number: "); 
		scanf("%f", &num1);
		fflush(stdin);
		printf("Enter the operator: "); /* enter +, -, etc */
		oper = getchar();
		fflush(stdin);
		printf("Enter the second number: ");
		scanf("%f", &num2);
		if(oper == '+')
		{
			total = num1+num2;
			printf("\n\n%.2f + %.2f = %.2f",num1, num2, total);
		}	
		else if(oper == '-')
		{
			total = num1-num2;
			printf("\n\n%.2f - %.2f = %.2f", num1, num2, total);
			putchar('\n');
		}
		else if(oper == '*')
		{
			total = num1 * num2;
			printf("\n\n%.2f * %.2f = %.2f", num1, num2, total);
			putchar('\n');
		}
		else if(oper == '/')
		{
			total = num1 / num2;
			printf("\n\n%.2f / %.2f = %.2f", num1, num2, total);
			putchar('\n');
		}	
		else
		{
			printf("Follow the directions directions next time");
		}
		putchar('\n');
		putchar('\n');
		printf("Repeat?\n\n");
		puts("no = 1");
		puts("yes = 2");
		fflush(stdin);
		printf("\tEnter choice: ");
		scanf("%d", &choice);
		switch(choice)
		{
			case 1:
				done = TRUE; 
				break;
			case 2:
				break;
		}
	}
	fflush(stdin);
	return(0); 
} /* end of program */

comments?

Last edited by Jessehk; May 24th, 2005 at 9:35 PM.
Jessehk is offline   Reply With Quote
Old May 24th, 2005, 9:48 PM   #2
melbolt
Hobbyist Programmer
 
melbolt's Avatar
 
Join Date: Feb 2005
Location: PA, USA
Posts: 233
Rep Power: 4 melbolt is on a distinguished road
Send a message via AIM to melbolt Send a message via Yahoo to melbolt
looks good

don't forget comments, those are very important too :p
melbolt is offline   Reply With Quote
Old May 25th, 2005, 8:56 AM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I would change the main section to this:
		bool valid = true;
		switch (oper)
		{
			case '+':
				total = num1 + num2;
				break;
			case '-':
				total = num1 - num2;
				break;
			case '*':
				total = num1 * num2;
				break;
			case '/':
				total = num1 / num2;
				break;
			default:
				printf("Follow the directions next time!\n");
				valid = false;
				break;
		}
		
		if (valid)
		{
			printf("\n\n%.2f %c %.2f = %.2f\n", num1, oper, num2, total);
		}

The general code, however, is pretty damn good.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 25th, 2005, 3:56 PM   #4
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4 Jessehk is on a distinguished road
Quote:
Originally Posted by Ooble
I would change the main section to this:
		bool valid = true;
		switch (oper)
		{
			case '+':
				total = num1 + num2;
				break;
			case '-':
				total = num1 - num2;
				break;
			case '*':
				total = num1 * num2;
				break;
			case '/':
				total = num1 / num2;
				break;
			default:
				printf("Follow the directions next time!\n");
				valid = false;
				break;
		}
		
		if (valid)
		{
			printf("\n\n%.2f %c %.2f = %.2f\n", num1, oper, num2, total);
		}

The general code, however, is pretty damn good.
thanks to both of you!

I'l think about those suggestions Ooble ( although I didn't know there was a case placeholder )
Jessehk is offline   Reply With Quote
Old May 25th, 2005, 4:39 PM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Just realised you're using C. Change bool to int, and stick these two lines just underneath the headers:
#define true 1
#define false 0
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:24 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC