Hello everyone, I need help figuring out how to make a switch construct that reads the first character from my input file and from there calculates the result.Either add or multiply. I have already completed the entire program it just does not run correctly. It also keeps coming up with error C2681.
Here is the program:
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include "Calcs.h"
#include <iomanip>
#include <fstream>
using std::ifstream;
using std::ofstream;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Calcs myCalcs;
myCalcs.x=7;
{
cerr << "Error: Input file could not be opened" << endl;
exit(1);
}
if(!outFile)
{ cerr << "Error: Output file could not be opened" << endl;
exit(1);
}
while ( !inFile.eof() )
{
inFile >> myCalcs.add(), myCalcs.multiply();
result = myCalcs.add(),myCalcs.multiply();
outFile <<endl;
}
cout << "End-of-file reached.." << endl;
inFile.close();
outFile.close();
int main(void);
{
cout<<"Welcome, this is a calculator."<<std::endl;
cout<<"Please choose an option by entering the number.\n"<<std::endl;
cout<<"1 - Addition of two numbers\n";
cout<<"2 - Multiplication of two numbers\n";
cout<<"3 - Quit\n";
char response= ' ';
cin>>response;
What does that mean? It doesn't help in trying to figure out what problem you're having.
Anyways, I've look through your code and I've noticed a few things.
1 2
inFile >> myCalcs.add(), myCalcs.multiply();
result = myCalcs.add(),myCalcs.multiply();
you seem to be using those wrong, you are reading something from the inFile, and you can only store the value inside a variable, instead you're trying to read it into methods. Secondly, you can't just separate them with a comma and let the compiler do both. The syntax doesn't work like that.
int main(void);
you seem to have another main function, but it has a semi-colon which is an error.
1 2 3 4 5 6
return 0;
}
system("pause");
return 0;
}
I get the feeling like you're trying to create 2 main functions which are nested. C++ doesn't let you do that. I don't think any programming language does actually.
int main()
{
int selection = 0;
cout << "Welcome" << endl
<< " 1. add" << endl
<< " 2. subtract" << endl
<< " 3. quit" << endl
<< "Choose from menu: " << endl;
cin >> selection;
switch(selection){
case 1: //call function or do operation (you could do what ever you want here)
// you could call function (function1();)
// or do operation ( answer = num1 + num2;)
break; // after this point break out of switch operation
case 2: //call function or do operation
break; // after this point break out of switch operation
case 3: // end or quit you program
break; // after this point break out of switch operation
default: // you end up here if selection is anything other then 1,2,or 3
// so you could either just break out or loop back in by asking another
// selection.
break; // to break out of switch if needed or just let if end its-self without break.
} // end of switch operation
} // end main
so you could think of it like this : "case 1" meaning "case" is acting like an "if statement" ... and "1" is the selection or input. "case" could also been "case 'A':" .
switch operation starts from the top and moves done till it reaches its end '}' and doesn't act like a loop.