This a very beginner question. I'm trying to create a simple c switch case based console calculator that creates a txt file and writes the output to it. I have very little experience with OOPS or C so I have no clue on how to make this work.
This snippet creates a text file and writes to it:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream MyFile("filename.txt");
MyFile << "Lorem ipsum dolor amet";
MyFile.close();
}
When I try to add this to my calculator code, it gives me a bunch of errors:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char op;
float num1, num2;
cout << "Enter Operator: , -, *, / : ";
cin >> op;
cout << "Enter two Operands: ";
cin >> num1 >> num2;
switch(op)
{
case ' ':
cout << num1 << " " << num2 << " = " << num1 num2;
ofstream MyFile("filename.txt");
MyFile << num1 << " " << num2 << " = " << num1 num2;
MyFile.close();
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
ofstream MyFile("filename.txt");
MyFile << num1 << " - " << num2 << " = " << num1 - num2;
MyFile.close();
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
ofstream MyFile("filename.txt");
MyFile << num1 << " * " << num2 << " = " << num1 * num2;
MyFile.close();
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
ofstream MyFile("filename.txt");
MyFile << num1 << " / " << num2 << " = " << num1 / num2;
MyFile.close();
break;
default:
cout << "Error! Operator is not correct";
break;
}
return 0;
}
The error messages in the console:
calctext.cpp: In function 'int main()':
calctext.cpp:25:14: error: jump to case label
25 | case '-':
| ^~~
calctext.cpp:20:18: note: crosses initialization of 'std::ofstream MyFile'
20 | ofstream MyFile("filename.txt");
| ^~~~~~
calctext.cpp:27:18: error: redeclaration of 'std::ofstream MyFile'
27 | ofstream MyFile("filename.txt");
| ^~~~~~
calctext.cpp:20:18: note: 'std::ofstream MyFile' previously declared here
20 | ofstream MyFile("filename.txt");
| ^~~~~~
calctext.cpp:32:14: error: jump to case label
32 | case '*':
| ^~~
calctext.cpp:20:18: note: crosses initialization of 'std::ofstream MyFile'
20 | ofstream MyFile("filename.txt");
| ^~~~~~
calctext.cpp:34:18: error: redeclaration of 'std::ofstream MyFile'
34 | ofstream MyFile("filename.txt");
| ^~~~~~
calctext.cpp:20:18: note: 'std::ofstream MyFile' previously declared here
20 | ofstream MyFile("filename.txt");
| ^~~~~~
calctext.cpp:39:14: error: jump to case label
39 | case '/':
| ^~~
calctext.cpp:20:18: note: crosses initialization of 'std::ofstream MyFile'
20 | ofstream MyFile("filename.txt");
| ^~~~~~
calctext.cpp:41:18: error: redeclaration of 'std::ofstream MyFile'
41 | ofstream MyFile("filename.txt");
| ^~~~~~
calctext.cpp:20:18: note: 'std::ofstream MyFile' previously declared here
20 | ofstream MyFile("filename.txt");
| ^~~~~~
calctext.cpp:46:9: error: jump to case label
46 | default:
| ^~~~~~~
calctext.cpp:20:18: note: crosses initialization of 'std::ofstream MyFile'
20 | ofstream MyFile("filename.txt");
CodePudding user response:
The scope of a variable declared inside a switch
is the entire switch
-block - a case
is not a separate scope.
This leads to two problems:
- Multiple declarations of the same variable name, and
- Jumping across a variable initialization, which you're not allowed to do.
The trivial fix is to wrap each case in a pair of curly braces, adding a new scope:
case ' ':
{
ofstream MyFile("filename.txt");
cout << num1 << " " << num2 << " = " << num1 num2;
MyFile << num1 << " " << num2 << " = " << num1 num2;
// You don't need to explicitly close a file, its destruction takes care of it.
break;
}
but I would remove the code duplication instead:
bool valid_op = true;
float result = 0;
switch(op)
{
case ' ':
result = num1 num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
cout << "Error! Operator is not correct";
valid_op = false;
break;
}
if (valid_op)
{
ofstream MyFile("filename.txt");
cout << num1 << ' ' << op << ' ' << num2 << " = " << result;
MyFile << num1 << ' ' << op << ' ' << num2 << " = " << result;
}
CodePudding user response:
The variables declared in a switch
block have their scope as that switch
block. So the MyFile
object in
case is visible in -
case. So compiler thinks you have declared a variable with same name again.
You can possibly change the variable name for each case or, do something like this.
int main()
{
char op;
float num1, num2;
cout << "Enter Operator: , -, *, / : ";
cin >> op;
cout << "Enter two Operands: ";
cin >> num1 >> num2;
ofstream MyFile;
switch(op)
{
case ' ':
MyFile = ofstream("filename.txt");
cout << num1 << " " << num2 << " = " << num1 num2;
MyFile << num1 << " " << num2 << " = " << num1 num2;
MyFile.close();
break;
case '-':
MyFile = ofstream("filename.txt");
cout << num1 << " - " << num2 << " = " << num1 - num2;
MyFile << num1 << " - " << num2 << " = " << num1 - num2;
MyFile.close();
break;
case '*':
MyFile = ofstream("filename.txt");
cout << num1 << " * " << num2 << " = " << num1 * num2;
MyFile << num1 << " * " << num2 << " = " << num1 * num2;
MyFile.close();
break;
case '/':
MyFile = ofstream("filename.txt");
cout << num1 << " / " << num2 << " = " << num1 / num2;
MyFile << num1 << " / " << num2 << " = " << num1 / num2;
MyFile.close();
break;
default:
cout << "Error! Operator is not correct";
break;
}
return 0;
}