In an attempt to improve my coding skills, I decided to program a very simple DOS calculator. However, when i try and compile the program, I am presented with with following:

------ Build started: Project: Basic Calculator, Configuration: Debug Win32 ------
Compiling...
Basic Calculator.cpp
c:\documents and settings\my documents\visual studio 2008\projects\basic calculator\basic calculator\basic calculator.cpp(23) : error C2061: syntax error : identifier 'chChar'
c:\documents and settings\my documents\visual studio 2008\projects\basic calculator\basic calculator\basic calculator.cpp(25) : error C2181: illegal else without matching if
c:\documents and settings\my documents\visual studio 2008\projects\basic calculator\basic calculator\basic calculator.cpp(26) : error C2061: syntax error : identifier 'chChar'
c:\documents and settings\my documents\visual studio 2008\projects\basic calculator\basic calculator\basic calculator.cpp(28) : error C2181: illegal else without matching if
c:\documents and settings\my documents\visual studio 2008\projects\basic calculator\basic calculator\basic calculator.cpp(29) : error C2061: syntax error : identifier 'chChar'
c:\documents and settings\my documents\visual studio 2008\projects\basic calculator\basic calculator\basic calculator.cpp(31) : error C2181: illegal else without matching if
c:\documents and settings\my documents\visual studio 2008\projects\basic calculator\basic calculator\basic calculator.cpp(32) : error C2061: syntax error : identifier 'chChar'
c:\documents and settings\my documents\visual studio 2008\projects\basic calculator\basic calculator\basic calculator.cpp(34) : error C2181: illegal else without matching if
Build log was saved at "file://c:\Documents and Settings\My Documents\Visual Studio 2008\Projects\Basic Calculator\Basic Calculator\Debug\BuildLog.htm"
Basic Calculator - 8 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

This is the code I wrote:

#include "stdafx.h"
#include <iostream>
#include "functions.h"

int add(int x, int y);			// Declares the add function for use.
int subtract(int x, int y);		// Declares the subtract function for use.
int multiply(int x, int y);		// Declares the multiply function for use.
int divide(int x, int y);		// Declares the divide function for use.

int main()
{
	using namespace std;									// Allows access to cout and cin.
	cout << "This is a basic calculator" << endl;			// Tells the user what the program does.
	cout << "Please enter a whole number" << endl;			// Instructs the user.
	int x;													// Defines x as a interger varible.
	cin >> x;												// Stores the users input in x.
	cout << "Please enter another whole number" << endl;	// Instruction.
	int y;													// Defines y as an integer variable.
	cin >> y;												// Stores user input as y.
	cout << "Please enter a, s, d, or m to add, subract, divide of multiply" << endl;
	char chChar;											// Defines chChar as a char.
	cin >> chChar;
	if chChar == a;												// Checks to see if chChar is 'a.'
		cout << "The answer is: " << add(x, y) << endl;			// If it is a, the add function is used.
	else 
		if chChar = s;											// Checks to see if chChar is 's.'
		cout << "The answer is: " << subtract(x, y) << endl;	// If it is s, the subtracy function is used.
	else 
		if chChar = m;											// Checks to see if chChar is 'm.'
		cout << "The awnser is: " << multiply(x, y) << endl;	// If it is m, the multiply function is used.
	else 
		if chChar = d;											// Checks to see if chChar is 'd.'
		cout << "The answer is: " << divide(x, y) << endl;		// If it is d, the divide function is used.
	else 
		cout << "You entered an incorrect function." << endl;	// Tells the user they have entered an incorrect value.
	std::cin.get();
	return 0;
}

There are no problems with the functions header, so I have not posted it. If it is needed, just ask, and I'll post it. Thanks for reading.

Recommended Answers

All 4 Replies

http://cboard.cprogramming.com/showthread.php?t=88495
You've basically copy/pasted the same error 4 times without first checking that the original copy was in any way good.

Compiling is essentially free, so it's fine to do it all the way through your program development, not just when you're ready to run.

If you only type in 5 lines, and get an error, then you know pretty much where to look. But type in lots of code and get lots of errors, then you're back here posting your code.

I agree that copying code that I knew contained errors was a mistake; I had compiled previously. It is less of an issue of WHERE the errors are, but rather what the Error Messages mean. I cannot find the fault in the incorrect code.

Here's the syntax of an if-statement:

if (condition)
{
   // execute this code if condition is true
}

You have this:

if chChar == a;

You need parentheses areound the condition and you need to get rid of the semicolon.

Thanks very much. That's sorted it out. Thanks for your help. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.