需要帮助更正家庭作业代码,C++ input/output
Need help correcting Homework code, C++ input/output
我正在学习 Comp Sci 的第一门课程。我的任务是输入一个 .txt
文件,然后输出一个修改后的 .txt
文件。我遇到的第一个问题只是修复目录。我做到了,.txt
文件一切正常,除了教授说我在 switch
语句中有错误。
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
int s = -2, a = 0, b = 0, NumIn;
ifstream inFile("NumbersIn.txt");
if (inFile.fail()) {
cout << "Unable to open file" << endl;
return -1;
}
ofstream outFile("RevisedNumbers.txt");
while (!inFile.eof()) {
inFile >> NumIn;
cout << NumIn << endl;
if (NumIn < 0)
NumIn = -1;
else if (NumIn > 0)
NumIn = 1;
outFile << NumIn << endl;
switch (NumIn) {
case -1:
a += s;
break;
case 0:
s += 1;
case 1:
b += s;
}
}
outFile.close();
inFile.close();
cout << "A = " << a << endl;
cout << "B = " << b << endl;
cout << "S = " << s << endl;
return 0;
}
输出:
0
-2
9
14
-22
12
6
-9
12
13
-33
21
9
0
1
2
3
4
1
A = -4
B = -9
S = 0
Program ended with exit code: 0
我在这里包括作业问题,所以我使用 s
、a
和 b
作为整数的原因:
You program will require the following files
Input file: NumbersIn.txt (attached to this assignment)
Output file: RevisedNumbers.txt
You program will use the following variables:
int s=-2, a = 0, b = 0. NumIn;
As long as there is data in the input file, your program will read a number into the variable NumIn, print it on the screen, and then determine the following:
If NumIn is negative change it to -1
If NumIn is positive change it to +1
If NumIn is zero, do nothing
Write the new value of NumIn to the output file RevisedNumbers
After updating NumIN, use a switch statement to based on the current value of NumIn
When NumIn is -1 increase the value of A by S
When NumIn is 0 increment S by 1
When NumIn is 1 increase the value of B by S
When you no longer have data to read in print out the current value of A, B and S.
Submit a copy of your program.
The screen output
The input file.
The output file.
您的版本:
switch (NumIn) {
case -1:
a += s;
break;
case 0:
s += 1;
case 1:
b += s;
}
要修复它,请添加两个 break 语句:
switch (NumIn) {
case -1:
a += s;
break;
case 0:
s += 1;
break;
case 1:
b += s;
break;
}
如果没有它们,案例 0 会跳转到案例 1。这样做实际上是合法的,但被认为是一个常见错误,因此如果您的失败是故意的,您应该始终发表评论。我想这次应该不是。
我正在学习 Comp Sci 的第一门课程。我的任务是输入一个 .txt
文件,然后输出一个修改后的 .txt
文件。我遇到的第一个问题只是修复目录。我做到了,.txt
文件一切正常,除了教授说我在 switch
语句中有错误。
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
int s = -2, a = 0, b = 0, NumIn;
ifstream inFile("NumbersIn.txt");
if (inFile.fail()) {
cout << "Unable to open file" << endl;
return -1;
}
ofstream outFile("RevisedNumbers.txt");
while (!inFile.eof()) {
inFile >> NumIn;
cout << NumIn << endl;
if (NumIn < 0)
NumIn = -1;
else if (NumIn > 0)
NumIn = 1;
outFile << NumIn << endl;
switch (NumIn) {
case -1:
a += s;
break;
case 0:
s += 1;
case 1:
b += s;
}
}
outFile.close();
inFile.close();
cout << "A = " << a << endl;
cout << "B = " << b << endl;
cout << "S = " << s << endl;
return 0;
}
输出:
0
-2
9
14
-22
12
6
-9
12
13
-33
21
9
0
1
2
3
4
1
A = -4
B = -9
S = 0
Program ended with exit code: 0
我在这里包括作业问题,所以我使用 s
、a
和 b
作为整数的原因:
You program will require the following files
Input file: NumbersIn.txt (attached to this assignment)
Output file: RevisedNumbers.txt
You program will use the following variables:
int s=-2, a = 0, b = 0. NumIn;
As long as there is data in the input file, your program will read a number into the variable NumIn, print it on the screen, and then determine the following:
If NumIn is negative change it to -1
If NumIn is positive change it to +1
If NumIn is zero, do nothing
Write the new value of NumIn to the output file RevisedNumbers
After updating NumIN, use a switch statement to based on the current value of NumIn
When NumIn is -1 increase the value of A by S
When NumIn is 0 increment S by 1
When NumIn is 1 increase the value of B by S
When you no longer have data to read in print out the current value of A, B and S.Submit a copy of your program.
The screen output
The input file.
The output file.
您的版本:
switch (NumIn) {
case -1:
a += s;
break;
case 0:
s += 1;
case 1:
b += s;
}
要修复它,请添加两个 break 语句:
switch (NumIn) {
case -1:
a += s;
break;
case 0:
s += 1;
break;
case 1:
b += s;
break;
}
如果没有它们,案例 0 会跳转到案例 1。这样做实际上是合法的,但被认为是一个常见错误,因此如果您的失败是故意的,您应该始终发表评论。我想这次应该不是。