please help me

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;

ifstream inFile ("datafile.txt");
ofstream outFile ("resultfile.txt");


int result;


if(!inFile)

{
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;

if (response =='1')
{
myCalcs.add();

}

else if (response=='2')
{
myCalcs.multiply();
}

else
{
cout<<"You entered an invalid option.";
}

return 0;
}

system("pause");
return 0;
}
It also keeps coming up with error C2681.


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.
Last edited on
Here is a switch operation, this is just an example, has nothing to do with you program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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.

I hope this helps.
-TAZO

Last edited on
Topic archived. No new replies allowed.