C++ Console app "Calculator" syntax error!

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Fallental
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 43
Joined: Mon Mar 02, 2009 6:13 pm

C++ Console app "Calculator" syntax error!

Post 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;
}
}
}




Maknis
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 2
Joined: Tue Dec 15, 2009 9:30 pm

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

Post 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
Fallental
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 43
Joined: Mon Mar 02, 2009 6:13 pm

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

Post by Fallental »

hmmm thank you that was wrong too, but when i correct it even more errors pop up lawl
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

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

Post 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.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

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

Post 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;
}
Post Reply