Page 1 of 1

C++ Console app "Calculator" syntax error!

Posted: Tue Dec 15, 2009 9:37 pm
by Fallental
I get this error when debugging my program - (69) : error C2059: syntax error : '}' :cry: (btw i think line 69 = last line)

Here is my code(new to programming)! please help! - written in c++ console

Code: Select all

#include <iostream>
using namespace std;

int main(void)

{

	system("TITLE Calculator");
	system("Color 2");
	char cChar;
	double dfirstnumber;
	double dsecondnumber;
	char cDoagain;

do
{
	system("CLS");
	cout << "Enter your first number: " << endl;
	cin >> dfirstnumber;
	cout << "Enter the operation you want to complete: " << " (+,-,*, or /)" << endl;
	cin >> cChar;

	cout << "Enter your second number" << endl;

	switch (cChar)

	{
	case '+':
			cout << "Answer: " << dfirstnumber << " + " << dsecondnumber << " = " << (dfirstnumber + dsecondnumber) << endl;
          break;

	case '-':
			cout << "Answer: " << dfirstnumber << " - " << dsecondnumber << " = " << (dfirstnumber - dsecondnumber) << endl;
           break;

	case '*':
			cout << "Answer: " << dfirstnumber << " * " << dsecondnumber << " = " << (dfirstnumber * dsecondnumber) << endl;

	case 'X':
			cout << "Answer: " << dfirstnumber << " X " << dsecondnumber << " = " << (dfirstnumber * dsecondnumber) << endl;

	case 'x':
			cout << "Answer: " << dfirstnumber << " x " << dsecondnumber << " = " << (dfirstnumber * dsecondnumber) << endl;

           break;

	case '/':
		if(dsecondnumber == 0){
				   cout << " Invalid Operation! " << endl;
		} else {
			cout << "Answer: " << dfirstnumber << " / " << dsecondnumber << " = " << (dfirstnumber / dsecondnumber) << endl;
    break;
	}
	break;
	default: 
		cout << "Invalid Operation! " << endl;
		break;


cout << "Would you like to reset? (Y/N)" << endl;

cin >> cDoagain;
} 
{while  (cDoagain == 'Y' || cDoagain == 'y');
system("Pause");
return 0;
}
}
}





Re: C++ Console app "Calculator" syntax error!

Posted: Tue Dec 15, 2009 10:00 pm
by Maknis
Hey there,

I am firstly going to say that i do not know 1 line of c++, have never studied it but i am more than likely wrong

but i can see this..

Code: Select all

cin >> cDoagain;
} 
{while  (cDoagain == 'Y' || cDoagain == 'y');
system("Pause");
return 0;
}
}
}

to me (again i don't know c++) you have an opening { infront of the while look
either this is not meant ot be there and if it is you haven't closed it

Re: C++ Console app "Calculator" syntax error!

Posted: Tue Dec 15, 2009 10:36 pm
by Fallental
hmmm thank you that was wrong too, but when i correct it even more errors pop up lawl

Re: C++ Console app "Calculator" syntax error!

Posted: Tue Dec 15, 2009 10:40 pm
by hurstshifter
Do not place a semi-colon after the condition of your while loop...

GOOD

Code: Select all

while(1)
{
     Do this;
}
 

BAD

Code: Select all

while(1);
{
     Do this;
}
 

You also might want to think about organizing your code better. Proper indentation, white-space, and commenting will help others to understand your code.

Re: C++ Console app "Calculator" syntax error!

Posted: Wed Dec 16, 2009 6:56 am
by Bakkon
Hurst, that's a do-while loop. Semicolon after the while is correct syntax.

The problem is your curly braces are a mess. Hopefully I can list them all for you:
- one MISSING at the end of your switch
- one EXTRA on the same line as the while statement
- two EXTRA at the very end

I got this to compile. Hopefully this maintained your logic.

Code: Select all

#include <iostream>
using namespace std;

int main(void)
{
   system("TITLE Calculator");
   system("Color 2");
   char cChar;
   double dfirstnumber;
   double dsecondnumber;
   char cDoagain;

	do
	{
	   system("CLS");
	   cout << "Enter your first number: " << endl;
	   cin >> dfirstnumber;
	   cout << "Enter the operation you want to complete: " << " (+,-,*, or /)" << endl;
	   cin >> cChar;

	   cout << "Enter your second number" << endl;

	   switch (cChar)
	   {
		   case '+':
		         cout << "Answer: " << dfirstnumber << " + " << dsecondnumber << " = " << (dfirstnumber + dsecondnumber) << endl;
		          break;
		   case '-':
		         cout << "Answer: " << dfirstnumber << " - " << dsecondnumber << " = " << (dfirstnumber - dsecondnumber) << endl;
		           break;
		   case '*':
		         cout << "Answer: " << dfirstnumber << " * " << dsecondnumber << " = " << (dfirstnumber * dsecondnumber) << endl;
		   case 'X':
				 cout << "Answer: " << dfirstnumber << " X " << dsecondnumber << " = " << (dfirstnumber * dsecondnumber) << endl;

		   case 'x':
		        cout << "Answer: " << dfirstnumber << " x " << dsecondnumber << " = " << (dfirstnumber * dsecondnumber) << endl;
				break;

		   case '/':
		      if(dsecondnumber == 0)
			  {
		               cout << " Invalid Operation! " << endl;
		      }
			  else
			  {
		         cout << "Answer: " << dfirstnumber << " / " << dsecondnumber << " = " << (dfirstnumber / dsecondnumber) << endl;
		    		break;
	   			}
	   			break;
	   		default:
	      			cout << "Invalid Operation! " << endl;
	      		break;
		}

		cout << "Would you like to reset? (Y/N)" << endl;

		cin >> cDoagain;
	}
	while  (cDoagain == 'Y' || cDoagain == 'y');

	system("Pause");
	return 0;
}