...

View Full Version : Need Some Suggestions



Recklein3
09-18-2009, 07:13 PM
I need to write a program that does four functions:

-read a number
-read an operator
-read another number
-then do the operation

The program needs to work with integers doing four functions:

-Add
-Subtract
-Divide
-Multiply

Other things required:

-When the user enters C the result is cleared (user re-enters a number)
-When the user enters X it closes the program.
-All operations will follow with the enter key.
-The program should prompt the user on what to do (including C and X)

Any suggestions would be great.:thumbsup:

Edit:
I want to use the least amount of code for this calculator (Like things clean;))

TheShaner
09-18-2009, 08:25 PM
Alright, so how about you start coding and we'll correct if need be. We don't do your homework, as per the rules and guidelines of these forums. We also can't guess the programming language you're using, so you need to make sure you note that in your posts.

Suggestions:

You will need a loop that will only terminate if the letter 'X' is entered
Inside the loop, use a variable to track which step you're on. If step 1, you need a number, 'C', or 'X'; if step 2, an operator, 'C', or 'X'; if step 3, a number, 'C', or 'X'. Once step 3 is complete, you perform the operation, display the result, and reset back to step 1.
The letter 'C' always resets you back to step 1 and the letter 'X' always exits the loop.

-Shane

Recklein3
09-18-2009, 09:13 PM
I'm doing this in C++, sorry for not mentioning in it in my original post.

And I'm not asking any of you to do my homework.
Just asking if you guys have any advice on how to tackle the future development at hand.

I'll post the code as soon has a foundation is set.

Thanks again. :thumbsup:

oracleguy
09-18-2009, 09:41 PM
Try pseudo coding it. Figure out the steps you need to do in order to achieve what you want. Write them down. Then you can start to think what code you would have to write to do that.

Recklein3
09-20-2009, 02:57 AM
So I Just planned out a little that needs to be done and started to add some code in.
I know it's messy and has errors, but what do you think of the foundation?

"Read side comments"


#include <iostream>

using namespace std;

void main ()



{


long double num1; /* Good start with using long double num1 and num2 and char choice? */
long double num2;
char choice;s


for () /* I need to do a for statement */


do /* A do statement */


{

cout << "Welcome to ******** Four Function Calculator (v1.2)" << endl;
cout << "Please choose an option by inputting the number and pressing the enter key, press x to quit and c to clear." << endl;
cout << "1 : Addition" << endl;
cout << "2 : Subtraction" << endl;
cout << "3 : Multiplication" << endl;
cout << "4 : Division" << endl;

cin >>choice; /* Output choice */

}

while ( choice < '1' || choice > '4' && choice != 'q'); /* attempted a while with using the ****ch (case) , still don't know how to fully make that work */

if (choice == 'q') break; /* Need to break and exit out of the program */

if (choice == 'c') /* I need to clear and restart the loop*/


switch (choice)


{


case '1':

cout<< "Please enter a number" << endl;

cin>>num;

cout<< "Another number to be added" << endl;

cin>>num2;

cout<<num + num2;

cout<< " " << endl;

break;


case '2':


cout<< "Please enter a number" << endl;

cin>>num;

cout<< "Another number to be subtracted" << endl;

cin>>num2;

cout<< num - num2;

cout<< " " << endl;

break;


case '3':

cout<< "Please enter a number" << endl;

cin>>num;

cout<< "Another one to be multiplied" << endl;

cin>>num2;

cout<< num * num2;

cout<< " " << endl;

break;


case '4':

cout<< "Please enter a number" << endl;

cin>>num;

cout<< "Another one to be divided" << endl;

cin>>num2;

cout<< num / num2;

cout<< " " << endl;

break;



default:
cout<<"That is an incorrect operator, please enter another choice: ";



}


}

voyage09
09-20-2009, 03:30 AM
Initially your requirement is
-read a number
-read an operator
-read another number
-then do the operation

Why your coding is read the operator first before read the numbers? It does not fulfill the initialy requirement already. Get all the data using Cin first before using the switch function.

Recklein3
09-20-2009, 04:41 AM
lol , just noticed that.
let me fix that :D
post some more progress Sunday.

thanks a :thumbsup:lot

oracleguy
09-20-2009, 05:16 PM
I would get rid of the two if statements before the case, there is no reason to do it that way. Just put them as part of the switch statement.

Also, you are duplicating a lot of code. Read the first number, ask for the operator and then just have a generic "Enter a second number" type prompt before you do the switch statement.

Does that make sense?

Recklein3
09-21-2009, 08:31 AM
Sorry I didn't respond back, been busy all day.
Didn't even get to look at C++ today, just Physics / Physics Lab and chores :/

Ya, that makes sense.
Def. working on it Monday.
So I'll post and update the code.

Thanks Again

Recklein3
09-22-2009, 09:51 PM
Okay so I did what you said.
Better now?

Now since that's done, I'm confused on what I need to do for the for statement and get the switch working in the right manner.



#include <iostream>

using namespace std;

void main ()

{

long double num1;
long double num2;
char choice;

for ()

do

{

cout << "Welcome to Clifford's Four Function Calculator (v1.5)" << endl;

cout << endl << endl << endl;

cout << "Please enter a number: " << endl;

cout << endl;

cin>>num1;

cout << "Please choose an option below and press the enter key to continue, press x to quit and c to clear." << endl;
cout << "1 : Addition" << endl;
cout << "2 : Subtraction" << endl;
cout << "3 : Multiplication" << endl;
cout << "4 : Division" << endl;

cin >>choice;

cout << endl;

cout << "Please enter another number: " << endl;

cin>>num2;

}

while (choice < '1' || choice > '6' && choice != 'q');

switch (choice)

{


case '1': cout << num1 + num2;

cout << " " << endl;

break;


case '2': cout << num1 - num2;

cout << " " << endl;

break;


case '3': cout << num1 * num2;

cout << " " << endl;

break;


case '4': cout << num1 / num2;

cout << " " << endl;

break;

case '5': cout << " " << endl;

cin;

break;


case '6': cout << "Shutting Down......" << endl;

break;


default: cout<< "That is an incorrect operator, please enter another choice: " <<endl;

}


}

Recklein3
09-24-2009, 04:13 AM
Okay so I worked on it more over night and this is what stands....
The only problem I have is that when you enter the operator wrong, it either asks again once you put another operator in or it dies completely...

Due in an hour, be glad if someone can help :(

#include <iostream>

using namespace std;

void main ()
{
int a=0;
int b=0;
int clear=1;
int oper=0;
int loop=0;
char c;

cout << "Welcome to Clifford's Four Function Calculator (v1.803)" <<endl;

cout << endl;

do{
do
{
if (clear==1)
{cout<< "Please enter a whole number: ";
cin>>a;}
oper=0;

cout << endl;

cout<< "Please enter an operator (+,-,*,/) or press X to turn off and C to clear information: ";
do

{

clear=0;
cin>>c;
switch (c)

{

case '+':
case '-':
case '*':
case '/':
clear=0;
break;
case 'C':
case 'c':
clear=1;
cout << "History cleared..." << endl;
break;
case 'X':
case 'x':
cout << "Shutting down..." << endl;
exit (0);
break;
default:
oper=1;
cout << "That is an incorrect operator, please choose one of the following (+,-,*,/) or press X to turn off and C to clear information:: ";
}
}while (oper==1);
}while (clear==1);
cout << "Please enter another whole number: ";
cin>>b;
if(c=='/'&&b==0)
cout << "Error: Trying to divide by zero." << endl;
else
{
cout <<a<<""<<c<<""<<b<< "=";
switch (c)
{
case '+':
a=a+b;
break;
case '-':
a=a-b;
break;
case '*':
a=a*b;
break;
case '/':
a=a/b;
break;

}
cout << a << endl;
}
}while(loop==0);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum