Advertisement

First Program

Started by July 28, 2002 11:34 AM
7 comments, last by stevelevesque 22 years, 1 month ago
I am makeing my first program which is a calculator and i know i am doin something wrong with my case statemants becase I have like 15 error. i will post what i have and hopefully people will show me my error. #include <iostream> int main() { using namespace std; int input1, input2, oper, quit; float answer; cout<<"Welcome to Steve Levesque''s Calculator!!! "; cout<<"\n Please enter the first number."; cin>>input1; cout<<"\nNow enter the sign for the problem(+,-,*,/)"; cin>>oper; cout<<"\n Please enter the second number to the equation "; switch (oper) { case ''+'': cout << "Your answer is "(input1 + input2)\n; case ''-'': cout << "Your answer is "(input1 - input2)\n; case ''*'': cout << "Your answer is "(input1 * input2)\n; case ''/'': cout << "Your answer is "(input1 / input2)\n; break; default: cout " Unknown operation.\n "; break; } cout<< "\n\n"; cout<< "Do you what to quit(y or n)\n "; cin>> quit; if(quit=y) { return 0; } if(quit=n) { return main(); } } When The Only Thing You Have Is A Hammer, Everything Looks Like A Nail
~When The Only Thing You Have Is A Hammer, Everything Looks Like A Nail.www.instinctz.net
Here is an update on what i fixed.

#include <iostream>

int main()
{

using namespace std;

int input1, input2, oper, quit, n, y;

float answer;

cout<<"Welcome to Steve Levesque''s Calculator!!! ";
cout<<"\n Please enter the first number.";
cin>>input1;

cout<<"\nNow enter the sign for the problem(+,-,*,/)";
cin>>oper;
cout<<"\n Please enter the second number to the equation ";

switch (oper)
{
case ''+'': cout << "Your answer is "(input1 + input2)std::endl;
break;
case ''-'': cout << "Your answer is "(input1 - input2)std::endl;
break;
case ''*'': cout << "Your answer is "(input1 * input2)std::endl;
break;
case ''/'': cout << "Your answer is "(input1 / input2)std::endl;
break;
default: cout " Unknown operation.\n ";
break;
}
cout<< "\n\n";
cout<< "Do you what to quit(y or n)\n ";
cin>> quit;
if(quit=y)
{
return 0;
}
if(quit=n)
{
return main();
}
}

When The Only Thing You Have Is A Hammer, Everything Looks Like A Nail
~When The Only Thing You Have Is A Hammer, Everything Looks Like A Nail.www.instinctz.net
Advertisement
First, The design of your program is wrong if you want it to loop.

Second, I'd change the switch's cases to their ascii egualivalent integer value.

Ahh, screw it...here you go.

    #include <iostream> #define PLUS 0x2b#define SUBT 0x2d#define MULT 0x2a#define DIVI 0x2f#ifndef TRUE#define TRUE 1#endif#ifndef FALSE#define FALSE 0#endifint main(){	using namespace std;		int input1, input2, oper;	char quit;	int done;	float answer;		do	{		cout << "Welcome to Steve Levesque's Calculator!!! ";		cout << "\n Please enter the first number.";		cin >> input1;				cout << "\nNow enter the sign for the problem(+,-,*,/)";		cin >> oper;		cout << "\n Please enter the second number to the equation ";		cin >> input2;				switch (oper)		{	//  This will clear up about 12 of your 15 errors			//										..you must place '<<' between expressions			case PLUS:				cout << "Your answer is " << (input1 + input2) << "\n";				break;	// be sure to break between cases, otherwise every case is							// executed until a break is encountered.  Your first break							// was at division.			case SUBT:				cout << "Your answer is " << (input1 - input2) << "\n";				break;			case MULT:				cout << "Your answer is " << (input1 * input2) << "\n";				break;			case DIVI:				cout << "Your answer is " << (input1 / input2) << "\n";				break;			default:				cout << " Unknown operation.\n ";				break;		}				cout << "\n\n";		cout << "Do you want to quit(y or n)\n ";		cin >> quit;				// When your checking a value for equal the symbol is:   		==  (ie: 1 == 1; )		// WHen your assigning a value to a variable the symbol is:		=   (ie: var = 1;)				if(quit=='y' || quit == 'Y' )			done = FALSE;	// done == FALSE		if(quit=='n' || quit == 'N' )			done = TRUE;	// done == TRUE	while( !done );// <-- this is the not symbol '!', as in NOT DONE. if done == 1  one notted equals zero.						// remember, any non-zero value will equate to true.  Zero is false.}     


I dont have a compiler on this pc to check the syntax but I think this will help a lot.


[edited by - Like2Byte on July 28, 2002 1:32:37 PM]
- Advice, eh? Well, besides working on your swing...you know, besides that...I'd have to think.
quote: Original post by Like2Byte
Second, I''d change the switch''s cases to their ascii egualivalent integer value.

There is absolutely no reason to do that. The program is more readable with explicit characters instead of numerical ASCII codes (even if you #define macros for them).
You idiot. Case statements have to be integer values.
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
Andrew, you idiot, the variables are defined as integer values, and a char is nothing but and unsigned short int.

[edited by - Vikato on July 28, 2002 6:26:43 PM]
Manufacturing metaphores in my melancholy mind.
Advertisement
Hmmm... forgot it was a char not a char string. NVM. Sorry. Bleh. People make mistakes.
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
On the first code you posted, this jumped out at me,

switch (oper)
{
case ''+'': cout << "Your answer is "(input1 + input2)\n; <-- missing "
case ''-'': cout << "Your answer is "(input1 - input2)\n; <-- same
case ''*'': cout << "Your answer is "(input1 * input2)\n; <-- ditto
case ''/'': cout << "Your answer is "(input1 / input2)\n; <-- again
break;
default: cout " Unknown operation.\n ";
break;
}

and in the second code listing, you didnt put an insertion operator in front of endl.
Manufacturing metaphores in my melancholy mind.

quote: Original post by Like2Byte
Second, I''d change the switch''s cases to their ascii egualivalent integer value.

quote: Original post by Dactylos
There is absolutely no reason to do that. The program is more readable with explicit characters instead of numerical ASCII codes (even if you #define macros for them).


There isn''t, huh? If he had declared his oper variable as a char I''d have left it alone and let the cases remain char values. However, he declared oper as an int; therefore, IMHO, keeping the datatypes the same (whether one is promoted to int or not) it keeps, in the forefront of the programmers mind, to do his own type-checking (with the added benifit of understanding why C/C++ do type-checking in the first place) before the compiler catches it (and throws a warning at compile time) or misses it altogether. In this case, it was harmless - in another situation, this could have caused him hours of frustrating debugging efforts.

Sorry for the longwinded statement.

Peace all!
- Advice, eh? Well, besides working on your swing...you know, besides that...I'd have to think.

This topic is closed to new replies.

Advertisement