GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Jun-2005, 03:43
SpinDizzy SpinDizzy is offline
New Member
 
Join Date: Jun 2005
Posts: 3
SpinDizzy is on a distinguished road

C++ Calculator Problem. Any help much appreciated.


Right then here is the code for it.

CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>

void menu();
float add(float a);
float multiply(float m);
float divide(float d);
float subtract(float s);
float equals;
void subtract();
void main()
{
	menu();
}
float input1,input2,ans;
void menu()
{
  int choice;


  clrscr();

  printf("\n\n\n please enter a number: ");
  fflush(stdin);
  scanf("%f",&input1);
  ans = input1;

 do
 {
  printf("\n\n1.  Add ");
  printf("\n\n2.  Subtact ");
  printf("\n\n3.  Multiply ");
  printf("\n\n4.  Divide ");
  printf("\n\n5.  Quit ");

  printf("\n\nChoose what operation to do  ");

  fflush(stdin);
  scanf("%d", &choice);

  switch(choice)
	{
	 case 1: add(ans);
	 break;
	 case 2: subtract(ans);
	 break;
	 case 3: multiply(ans);
	 break;
	 case 4: divide(ans);
	 break;
	 default: printf("\n  Invalid Option");
	 break;
	 case 5:
	 break;
	 }
  }while(choice!=5);
  getch();
}
float add(float ans)
{
	float input2;

	printf("\n\n\n please enter a number: ");
	fflush(stdin);
	scanf("%f",&input2);

	ans += input2;
	printf("\n\nThe Answer is %f",ans);
	return (ans);

}
float multiply(float ans)
{
	float input2;

	printf("\n\n\n please enter a number: ");
	fflush(stdin);
	scanf("%f",&input2);

	ans *= input2;
	printf("\n\nThe Answer is %f",ans);
	return (ans);
}
float subtract(float ans)
{
	float input2;

	printf("\n\n\n please enter a number: ");
	fflush(stdin);
	scanf("%f",&input2);

	ans -= input2;
	printf("\n\nThe Answer is %f",ans);
	return (ans);
}
float divide(float ans)
{
	float input2;

	printf("\n\n\n please enter a number: ");
	fflush(stdin);
	scanf("%f",&input2);
	do
	{
	ans /= input2;
printf("\n\nThe Answer is %f",ans);
	return (ans);
	}
	while(input2!=0||input1!=0);
	return (ans);
}

As you can probably tell from things I've done wrong and things I should have done first, such as making a lib file for all the values as the top, I am an utter beginner.

The problem with this calculator is that it almost works, however, it doesn't work after the first few numbers are inputted, to run it you would get something like this.


Please enter a number 4

1. Add
2. Divide
3. Multiply
4. Subract
5. Quit

Choose an operation 1

Please enter a number 4

The answer is 8

1. Add
2. Divide
3. Multiply
4. Subract
5. Quit

Choose an operation 3

Please enter a number 3

The answer is 32


And of cour 8x3 is not 32, and I realise that it's storing only the first number ir get's into the value input2 and so I thought that I must need to flush or erase it's memory for it somehow, but I have no command that does that does it. Can anyone help?

I know the program layout is a little confusing, planning to add some pauses for each part.

Help please?
Last edited by LuciWiz : 29-Jun-2005 at 05:22. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 29-Jun-2005, 06:14
SpinDizzy SpinDizzy is offline
New Member
 
Join Date: Jun 2005
Posts: 3
SpinDizzy is on a distinguished road
What I mean is you can flush buffers and strings, but is it possible to erare a float value?
  #3  
Old 29-Jun-2005, 07:43
maprich maprich is offline
Member
 
Join Date: May 2005
Posts: 163
maprich has a spectacular aura aboutmaprich has a spectacular aura about
Your code declares variables globally:
CPP / C++ / C Code:
float input1,input2,ans;
so you shouldn't pass variables to your functions. your code:
CPP / C++ / C Code:
float add(float ans)
{
  float input2;

  printf("\n\n\n please enter a number: ");
////etc
should be just
CPP / C++ / C Code:
float add()
{
   printf("\n\n\n please enter a number: ");
// etc etc
to avoid variable name overloading and confusion.
  #4  
Old 29-Jun-2005, 09:34
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by SpinDizzy
As you can probably tell from things I've done wrong and things I should have done first, such as making a lib file for all the values as the top, I am an utter beginner.

The problem with this calculator is that it almost works, however, it doesn't work after the first few numbers are inputted, to run it you would get something like this.


Please enter a number 4

1. Add
2. Divide
3. Multiply
4. Subract
5. Quit

Choose an operation 1

Please enter a number 4

The answer is 8

1. Add
2. Divide
3. Multiply
4. Subract
5. Quit

Choose an operation 3

Please enter a number 3

The answer is 32


And of cour 8x3 is not 32, and I realise that it's storing only the first number ir get's into the value input2 and so I thought that I must need to flush or erase it's memory for it somehow, but I have no command that does that does it. Can anyone help?

I know the program layout is a little confusing, planning to add some pauses for each part.

Help please?
The first thing I'd do is remove the input from each function and place one input before the switch. Remove fflush(stdin); because it's not correct. Here's why

Then, print your inputs as soon as they are entered to prove they are correct.

Next, move the display of the answer after the switch.

Pass the answer and input into each function. This way the function has a single purpose -- execute the operation.

And why is there a do/while loop in divide?
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #5  
Old 29-Jun-2005, 09:44
SpinDizzy SpinDizzy is offline
New Member
 
Join Date: Jun 2005
Posts: 3
SpinDizzy is on a distinguished road
Quote:
Originally Posted by maprich
Your code declares variables globally:
CPP / C++ / C Code:
float input1,input2,ans;
so you shouldn't pass variables to your functions. your code:
CPP / C++ / C Code:
float add(float ans)
{
  float input2;

  printf("\n\n\n please enter a number: ");
////etc
should be just
CPP / C++ / C Code:
float add()
{
   printf("\n\n\n please enter a number: ");
// etc etc
to avoid variable name overloading and confusion.

I can get rid of float ans from inside the brackets of the divide, subtract, add and multiply fuctions because as you say I declared them globally. I cannot get rid of float ans from inside the brackets at the start though, or else the global lines at the beginning aren't defined.

WaltP: The DO IF statement in the divide section is because of the anything divided by zero crashing the program, creating an infinite loop or something.
  #6  
Old 29-Jun-2005, 09:57
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by SpinDizzy
I can get rid of float ans from inside the brackets of the divide, subtract, add and multiply fuctions because as you say I declared them globally. I cannot get rid of float ans from inside the brackets at the start though, or else the global lines at the beginning aren't defined.
You should probably not have the variable global but defined inside main(). It's a getter programming technique to limit the number of globals. Preferably to none if possible.


Quote:
Originally Posted by SpinDizzy
WaltP: The DO IF statement in the divide section is because of the anything divided by zero crashing the program, creating an infinite loop or something.
IMHO it would be better to just use an if statment in this case and an error message before returning with the function undone.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #7  
Old 03-Jul-2005, 19:06
Jeremiah Jeremiah is offline
New Member
 
Join Date: Jul 2005
Posts: 11
Jeremiah is on a distinguished road
CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>
/*Prototypes*/

float Calculator(int operation, float input1, float input2);
int   PrintMenu();

/*Functions*/

float Calculator(int operation,float input1,float input2){
switch (operation){
case 1: return(input1 + input2);
case 2: return(input1 - input2);
case 3: return(input1 * input2);
case 4: return(input1 / input2);
case 5: break;
     }
}
int PrintMenu(){
  int final;
  printf("\n\n1.  Add ");
  printf("\n\n2.  Subtact ");
  printf("\n\n3.  Multiply ");
  printf("\n\n4.  Divide ");
  printf("\n\n5.  Quit ");
  printf("\n\nChoose what operation to do  ");
  scanf("%d",&choice);
  final = &choice;
  return final;
}


/*Main Execution*/
int main()
{

  int input1, input2,choice;
  float ans;

  choice = PrintMenu();
  printf("\n\n\n please enter a number: \n");
  scanf("%d", &input1);
  printf("\n\n\n please enter a second. number the first was",input1,":\n\n");
  scanf("%d", &input2);
  ans = Calculator(choice,input1,input2);
  printf("\n\nThe Answer is %f",ans);


 }


You should reorganize your code into something like this I think. Theres no need for so many functions when they all do simple things. The references/pointers needs fixing, though. You'll probably want to loop it too.
  #8  
Old 03-Jul-2005, 20:42
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Jeremiah
You should reorganize your code into something like this I think. Theres no need for so many functions when they all do simple things. The references/pointers needs fixing, though. You'll probably want to loop it too.

Simpler organization is a Good Thing. (But I think the Original Poster had questions that mainly were due to problems with his loop, which you didn't cover.)

Here are a few Bad Things about your code.

1. Won't compile. (Did you try it?)

2. The following is wrong:
CPP / C++ / C Code:
  printf("\n\n\n please enter a second. number the first was",input1,":\n\n");

3.The following is wrong, wrong (even if choice were defined here):
CPP / C++ / C Code:
int PrintMenu(){
  int final;
...
  scanf("%d",&choice);
  final = &choice;
  return final;

4. The following is wrong, since some return paths don't return a value. Bad style even if you had checked for valid operation values in PrintMenu().
CPP / C++ / C Code:
float Calculator(int operation,float input1,float input2){
switch (operation){
...
...
case 5: break;
     }
}

5. Many people read these threads to pick up programming techniques even though they never post any requests. Since your post was about style, I would offer the comment that I think it is good style to have main() return a value. Now, I know that this is a special case that is covered in the C standard (main() returns zero if there is no explicit return or exit()), so it's not strictly illegal, but I think if we are attempting to help those with less experience, that we should be very careful about every detail, and make every effort to set a good example.

(And at the very least make sure the program compiles and gives some kind of expected output.)

Regards,

Dave
  #9  
Old 03-Jul-2005, 22:47
Jeremiah Jeremiah is offline
New Member
 
Join Date: Jul 2005
Posts: 11
Jeremiah is on a distinguished road
Quote:
Originally Posted by davekw7x
Here are a few Bad Things about your code.
1. Won't compile. (Did you try it?)

Ooops. This is where I slap myself. My first posting of code. I just hope its my worst.

Okay I'll admit it. I'm a slacker. I went through about three edits. I rearranged the code to make it look prettier and that was all I thought about at the time. I made one compilable version, but the results were undesirable. I edited again but I assumed it would compile because I only made few changes.

Rearranging does not solve the problem, apparently. I know. I know. Flawed logic.

Well to quote our Original Poster

"As you can probably tell from things I've done wrong and things I should have done first,..,I am an utter beginner."

--SpinDizzy

I'll fix it later. Or give up.
  #10  
Old 11-Jul-2005, 02:56
Jeremiah Jeremiah is offline
New Member
 
Join Date: Jul 2005
Posts: 11
Jeremiah is on a distinguished road
I fixed it, atlast. It works much better now(as it actually compiles). This one only handles integers however(scanf is bugging me). You get the general idea though.

CPP / C++ / C Code:
#include <stdio.h>

/*Prototypes*/

int Calculator(int operation, int input1, int input2);
void  PrintMenu();

/*Functions*/

int Calculator(int operation,int input1,int input2){
printf("\n\n Operation : %i Input1 : %i Input2 : %i",operation,input1,input2);
switch (operation){
case 1: return(input1 + input2);
case 2: return(input1 - input2);
case 3: return(input1 * input2);
case 4: return(input1 / input2);
 }
}

void PrintMenu(){
int inp1,inp2,choice;
int ans;
printf("\n\n1.  Add ");
printf("\n\n2.  Subtact ");
printf("\n\n3.  Multiply ");
printf("\n\n4.  Divide ");
printf("\n\n5.  Quit ");
printf("\n\nChoose what operation to do  ");
scanf("%i",&choice);
if ((choice <= 5) && (choice > 0))
{
if (choice == 5)
{
printf("Exiting");
return;
}
printf("\n\n\n please enter a number: \n");
scanf("%i",&inp1);
printf("\n\n\n please enter a second number. The first was %i: \n",inp1);
scanf("%i",&inp2);
printf("\n\n\n You have just entered %i",inp2);
ans = Calculator(choice,inp1,inp2);
printf("\n\nThe Answer is %i",ans);
PrintMenu();
}
else {printf("Invalid Option!!!!"); PrintMenu();}
}

/*Main Execution*/

int main()
{
  PrintMenu();

 }


Three cheers for recursion!!!
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
probably a stupid mistake....lil help appreciated wbsquared03 C++ Forum 9 06-Dec-2004 15:43
Need help with a program if anyone can help it would be appreciated Krc784 C++ Forum 1 03-Nov-2004 20:55
Minor Problem with my program. Any help greatly appreciated. agentxx04 C Programming Language 6 24-Oct-2004 15:04

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:48.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.