Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Sep 15th, 2005, 7:37 AM   #1
ViOLATiON
Programmer
 
Join Date: Sep 2005
Posts: 58
Rep Power: 4 ViOLATiON is on a distinguished road
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.
ViOLATiON is offline   Reply With Quote
Old Sep 15th, 2005, 8:03 AM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
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?
Polyphemus_ is offline   Reply With Quote
Old Sep 15th, 2005, 8:26 AM   #3
ViOLATiON
Programmer
 
Join Date: Sep 2005
Posts: 58
Rep Power: 4 ViOLATiON is on a distinguished road
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;
    }
}
ViOLATiON is offline   Reply With Quote
Old Sep 15th, 2005, 8:35 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Sep 15th, 2005, 8:44 AM   #5
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
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.
InfoGeek is offline   Reply With Quote
Old Sep 15th, 2005, 8:48 AM   #6
ViOLATiON
Programmer
 
Join Date: Sep 2005
Posts: 58
Rep Power: 4 ViOLATiON is on a distinguished road
Holy :eek:
ViOLATiON is offline   Reply With Quote
Old Sep 15th, 2005, 10:18 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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).
Attached Images
File Type: jpg broken.jpg (19.3 KB, 56 views)
__________________
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
DaWei is offline   Reply With Quote
Old Sep 15th, 2005, 10:26 AM   #8
ViOLATiON
Programmer
 
Join Date: Sep 2005
Posts: 58
Rep Power: 4 ViOLATiON is on a distinguished road
Hmm.. I'll give it a shot before asking for help
ViOLATiON is offline   Reply With Quote
Old Sep 15th, 2005, 10:37 AM   #9
ViOLATiON
Programmer
 
Join Date: Sep 2005
Posts: 58
Rep Power: 4 ViOLATiON is on a distinguished road
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

ViOLATiON is offline   Reply With Quote
Old Sep 15th, 2005, 10:51 AM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei 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 2:10 PM.

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