C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 6th, 2005, 12:13 AM
Not You Not You is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 10 Not You User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 25 sec
Reputation Power: 0
Can you help me with this calculator?

The calculator works fine. The problem comes when exiting. I get only 1 error, and that is (in function "int z1();" near the bottom), it says, "syntax error before '<<' token." Here's the code:
Code:
  
  #include <iostream>
  
  	int addition ( int x, int y)
  	{
  		return x + y;
  	}
  
  	int subtraction ( int x, int y)
  	{
  		return x - y;
  	}
  
  	int multiplication ( int x, int y)
  	{
  		return x * y;
  	}
  
  	int division ( int x, int y)
  	{
  		return x / y;
  	}
  
  	int z1 ( int z)
  	{
  		return z + 1;
  	}
  
  using namespace std;
  
  int add();
  int sub();
  int mul();
  int div();
  int z1();
  int main()
  {
  	int input, z;
  	z = 0;
  	do {
 		cout<<"1. add\n2. subtract\n3. multiply\n4. divide\n5. exit\nEnter your choice: ";
  		cin>> input;
  			switch ( input ) {
  
  			case 1:
  				add();
  			break;
  
  			case 2:
  				sub();
  			break;
  
  			case 3:
  				mul();
  			break;
  
  			case 4:
  				div();
  			break;
  
  			case 5:
  				z1();
  			break;
  
  			default:
  				cout<<"Error\n";
  			break;
  			}
  		cin.get();
  	} while ( z != 1 );
  }
  	int sub(){
  	int x, y;
  		cout<<"Enter the first number to subtract: ";
  			cin>> x;
  			cin.ignore();
  		cout<<"Enter the second number to subtract: ";
  			cin>> y;
  			cin.ignore();
  		cout<<"The difference is: "<<subtraction (x , y ); 
  		cin.get();
  	}
  
  	int add(){
  	int x, y;
  		cout<<"Enter the first number to add: ";
  			cin>> x;
  			cin.ignore();
  		cout<<"Enter the second number to add: ";
  			cin>> y;
  			cin.ignore();
  		cout<<"The sum is: "<<addition (x, y);
  		cin.get();
  	}
  
  	int mul(){
  	int x, y;
  		cout<<"Enter the first number to be multiplied: ";
  			cin>> x;
  			cin.ignore();
  		cout<<"Enter the second number to be multiplied: ";
  			cin>> y;
  			cin.ignore();
  		cout<<"The product is: "<<multiplication (x, y);
  		cin.get();
  	}
  
  	int div(){
  	int x, y;
  		cout<<"Enter the first number to be divided: ";
  			cin>> x;
  			cin.ignore();
  		cout<<"Enter the second number to be divided: ";
  			cin>> y;
  			cin.ignore();
  		cout<<"The quotiant is: "<<division (x, y);
  		cin.get();
  	}
  
  	int z1(){
  	int z;
  		<<z1 (z, 1);
  		cin.get();
  	}
  
        

I might have done something so blatantly obvious I can't see it. So please point it out to me.

Reply With Quote
  #2  
Old March 6th, 2005, 01:41 AM
BoolBooB BoolBooB is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 36 BoolBooB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 35 m 42 sec
Reputation Power: 4
The compiler is telling you it doesn't understand the line:
<<z1 (z, 1);
Neither do I. What are you trying to do here? cout??? left shift???

Reply With Quote
  #3  
Old March 6th, 2005, 09:33 AM
Anibal Anibal is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 176 Anibal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 20 m 48 sec
Reputation Power: 4
It's reasonable to assume cout...don't you think?

Quote:
Originally Posted by BoolBooB
The compiler is telling you it doesn't understand the line:
<<z1 (z, 1);
Neither do I. What are you trying to do here? cout??? left shift???

Reply With Quote
  #4  
Old March 6th, 2005, 12:41 PM
Not You Not You is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 10 Not You User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 25 sec
Reputation Power: 0
Trying to get it to add 1 to z so that it will exit out of the loop. Did I do it wrong? Okay, so that's a given, how would I do it right?

Reply With Quote
  #5  
Old March 8th, 2005, 06:26 PM
Not You Not You is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 10 Not You User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 25 sec
Reputation Power: 0
Okay, I revised it, but, when you press 'E' it says error invalid input. . . And it still won't loop. I don't know what I'm doing, but can someone help me?
Code:
  
  #include <iostream>
  
  	int addition ( int x, int y )
  	{
  		return x + y;
  	}
  
  	int subtraction ( int x, int y )
  	{
  		return x - y;
  	}
  
  	int multiplication ( int x, int y )
  	{
  		return x * y;
  	}
  
  	int division ( int x, int y )
  	{
  		return x / y;
  	}
  
  using namespace std;
  
  int add();
  int sub();
  int mul();
  int div();
  int main()
  {
  	int input, z;
  	z = 0;
  	do
  	{
 		cout<<"1. Add\n2. Subtract\n3. Multiply\n4. Divide\nE. Exit\nEnter your choice: ";
  		cin>> input;
  			switch ( input ) {
  
  			case 1:
  				add();
  			break;
  
  			case 2:
  				sub();
  			break;
  
  			case 3:
  				mul();
  			break;
  
  			case 4:
  				div();
  			break;
  
  			case 'E':
  				z = ( z + 1 );
  			break;
  
  			default:
 				cout<<"Error: Unknown Input\n";
  			break;
  			}
  		return 0;
  	} while ( z != 1 );
  }
  	int sub(){
  	int x, y;
  		cout<<"Enter the first number to subtract: ";
  			cin>> x;
  			cin.ignore();
  		cout<<"Enter the second number to subtract: ";
  			cin>> y;
  			cin.ignore();
  		cout<<"The difference is: "<<subtraction ( x , y );
  		cout<<"\n";
  		return 0;
  	}
  
  	int add(){
  	int x, y;
  		cout<<"Enter the first number to add: ";
  			cin>> x;
  			cin.ignore();
  		cout<<"Enter the second number to add: ";
  			cin>> y;
  			cin.ignore();
  		cout<<"The sum is: "<<addition ( x, y );
  		cout<<"\n";
  		return 0;
  	}
  
  	int mul(){
  	int x, y;
  		cout<<"Enter the first number to be multiplied: ";
  			cin>> x;
  			cin.ignore();
  		cout<<"Enter the second number to be multiplied: ";
  			cin>> y;
  			cin.ignore();
  		cout<<"The product is: "<<multiplication ( x, y );
  		cout<<"\n";
  		return 0;
  	}
  
  	int div(){
  	int x, y;
  		cout<<"Enter the dividend: ";
  			cin>> x;
  			cin.ignore();
  		cout<<"Enter the divisor: ";
  			cin>> y;
  			cin.ignore();
  		cout<<"The quotiant is: "<<division ( x, y );
  		cout<<"\n";
  		return 0;
  	}
  
  

Reply With Quote
  #6  
Old March 9th, 2005, 03:05 AM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
Is it ' E' or 'E' ? In former case it is giving valid error.

Secondly, your program won't stop looping due to execution of default case again and again.The condition of while loop will always be TRUE as z = 0.

Reply With Quote
  #7  
Old March 9th, 2005, 08:55 AM
Anibal Anibal is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 176 Anibal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 20 m 48 sec
Reputation Power: 4
It's not that bad...the loop would end if you, in fact, entered 'E'. The thing is C/C++ is case sensitive, so if you're entering e instead of E, you won't get the while to stop looping (it never enters that case)!!

Another thing: if you want to exit when z reaches 1, why not setting Z = 1 ? after all, you're not using that variable as a counter (there's no point doing z = z + 1).

And finaly a question: what is that bloody return 0 doing inside the loop??

Good Luck!!

Anibal.

Reply With Quote
  #8  
Old March 9th, 2005, 03:50 PM
BoolBooB BoolBooB is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 36 BoolBooB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 35 m 42 sec
Reputation Power: 4
Why not just make z a bool, set it equall to true before you enter the loop, then just set it false to exit the loop? Also, why not give it a descriptive name? Don't make this harder than it needs to be. And like Anibal said, get rid of the return 0 insdide the loop.

bool keep_looping = true;
do {
.
.
.
case 'whatever your exit condition is':
keep_looping = false;
break;
} while (keep_looping);

Reply With Quote
  #9  
Old March 9th, 2005, 06:33 PM
Not You Not You is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 10 Not You User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 25 sec
Reputation Power: 0
Thank you all so much. That damn return 0; I knew it should be lower, and the help with bool and all that. Thanks.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Can you help me with this calculator?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT