![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Sep 2005
Posts: 58
Rep Power: 4
![]() |
Console Calc
Ok, I can make it do the actual operation itself, but I would like the user to be able to select which operation. Here is the source code I have written thus far, showing what would happen if someone chose multiplication:
#include <iostream>
using namespace std;
int main()
{
string opchoice;
int numberone;
int numbertwo;
int answer;
cout<<"Choose your operation\n\nMultiplication = x\nSubtraction = -\nAddition = +\nDivision = /\n";
cin>>opchoice;
if ( opchoice == x ) {
cout<<"Please input the first number you would like to multiply.\n\n";
cin>>numberone;
cin.get();
cout<<"\n\nPlease input the second number you would like to multiply.\n\n";
cin>>numbertwo;
cin.get();
answer = numberone * numbertwo;
cout<<"\nThe final answer is "<<answer << "\n";
cin.get();
}
}My problem is how do I make it so when someone types x and hits enter, that it does the operation. I'm assuming it has something to do with string x; Please point me in the right direction ![]() Thanks. |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
well... just put every operation in its own function, to make your code nice, and do something like:
string s; getline(cin, s, '\n'); if (s == "Operation") do_something(); elseif (s == "Another Operation") do_something_else(); do you mean that? |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Sep 2005
Posts: 58
Rep Power: 4
![]() |
I changed opchoice to an integer, and put each number infront of the operation, and now it works.
// User: ViOLATiON
// Date: September 15th, 2005
// Comments: Basic Calculator
#include <iostream>
using namespace std;
int main()
{
int opchoice;
int numberone;
int numbertwo;
int answer;
// Choose your operation
cout<<"Choose your operation\n\n1 - Multiplication\n2 - Subtraction\n3 - Addition\n4 - Division\n\n"<<endl;
cin>>opchoice;
// Multiplication
if (opchoice==1)
{
cout<<"\nPlease input the first number you would like to multiply.\n\n";
cin>>numberone;
cin.get();
cout<<"\n\nPlease input the second number you would like to multiply.\n\n";
cin>>numbertwo;
cin.get();
answer = numberone * numbertwo;
cout<<"\nThe final answer is "<<answer << "\n";
cin.get();
return 0;
}
// Subtraction
if (opchoice==2)
{
cout<<"\nPlease input the first number you would like to subtract.\n\n";
cin>>numberone;
cin.get();
cout<<"\n\nPlease input the second number you would like to subtract.\n\n";
cin>>numbertwo;
cin.get();
answer = numberone - numbertwo;
cout<<"\nThe final answer is "<<answer << "\n";
cin.get();
return 0;
}
// Addition
if (opchoice==2)
{
cout<<"\nPlease input the first number you would like to add.\n\n";
cin>>numberone;
cin.get();
cout<<"\n\nPlease input the second number you would like to add.\n\n";
cin>>numbertwo;
cin.get();
answer = numberone + numbertwo;
cout<<"\nThe final answer is "<<answer << "\n";
cin.get();
return 0;
}
// Division
if (opchoice==2)
{
cout<<"\nPlease input the first number you would like to divide.\n\n";
cin>>numberone;
cin.get();
cout<<"\n\nPlease input the second number you would like to divide.\n\n";
cin>>numbertwo;
cin.get();
answer = numberone / numbertwo;
cout<<"\nThe final answer is "<<answer << "\n";
cin.get();
return 0;
}
} |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I am going to suggest a stretching exercise for you. One can gather the user input and process it properly whether it's one, two, three, or more items. For a typical (non-polish) calculator one gathers operators and operands and performs the operation when one perceives a 'go' condition. The use of a stack or two is often the approach. If you want to delve into that and have problems with it, there's help on the forum aplenty.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Same Output. Code reduced by 1/2
// User: ViOLATiON
// Date: September 15th, 2005
// Comments: Basic Calculator
#include <iostream>
using namespace std;
int main()
{
int opchoice;
int numberone;
int numbertwo;
int answer;
// Choose your operation
cout<<"Choose your operation\n\n1 - Multiplication\n2 - Subtraction\n3 - Addition\n4 - Division\n\n"<<endl;
cin>>opchoice;
cout<<"\nPlease input the first number\n\n";
cin>>numberone;
cin.get();
cout<<"\n\nPlease input the second number\n\n";
cin>>numbertwo;
cin.get();
switch(opchoice)
{
case 1:
// Multiplication
answer = numberone * numbertwo;
break;
case 2:
// Subtraction
answer = numberone - numbertwo;
break;
// Addition
case 3:
answer = numberone + numbertwo;
break;
case 4:
//division
answer = numberone / numbertwo;
}
cout<<"\nThe final answer is "<<answer << "\n";
cin.get();
return 0;
}
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Sep 2005
Posts: 58
Rep Power: 4
![]() |
Holy :eek:
|
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I know I sound like a broken record to you guys, but this is germane to being a good programmer. Check your functions for success. I broke this program with my first keystroke. See the attached image (quality reduced because of compression).
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Sep 2005
Posts: 58
Rep Power: 4
![]() |
Hmm.. I'll give it a shot before asking for help
![]() |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Sep 2005
Posts: 58
Rep Power: 4
![]() |
I'm confused what does it mean by "The variable 'answer' is being used without being defined"
Isn't it defined by the operation, ie: answer = numberone * numbertwo; or is it defined at the int answer; line ![]() |
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
No, it is not defined there. Your input failed and zooped right past that code because nothing qualified. Let me make some points. You are not going to have personal access to all your users for training purposes, or to tell them that they are ignorant SOBs. Let your code detect untrained or inattentive or malicious users. Let you code tell them that they are ignorant SOBs.
Let me say again that a good function, whether written by you, or someone else, does not promise to deliver the goods. It promises to deliver or tell its user that it failed to do so. If you don't ask it which of those conditions obtains, how will you know? Let me stress again that some code, when it fails, can damage or kill someone. That may sound far-fetched; it is not. In a case like that one accrues criminal liability, even if it's just due to carelessness. Coding is part due consideration and part habit. The longer you code, the larger the habit part gets. It's best not to build bad habits. If you decide to present code as an example, and want to keep it short, you may decide OVERTLY to leave out some important things. If you do, it might be wise to say so. Otherwise, any knowledgeable reader is entitled to judge the author to be a schlocky programmer. If you read the documentation regarding iostreams, you will see how to make sure things went properly. If you want example code, post back.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|