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.