为什么我的数组没有输出正确的数据,这些数据是从外部文件输入的?
Why isn't my array outputting the correct data, which was input from an external file?
当输入来自文本文件时,我的数组输出似乎遇到了问题......虽然输出应该是单个字符(A,B,C,D),但当出现提示时,数组会输出乱码,如@,?等符号。任何形式的帮助将不胜感激。程序的成绩部分也没有计算,但我自己可以弄清楚。 该程序编译并运行,错误为:
29 46 C:UsersRobertDesktopbob projectProj1.cpp [Warning] deprecated
conversion from string constant to 'char*' [-Wwrite-strings]
第 32 行也有同样的错误
#include <iostream>
#include<cstdlib>
#include <iomanip>
#include <fstream>
using namespace std;
// Global constants
const int SIZE = 20; // The number of questions
const int numQuestions = 20;
// Function prototypes
void readFile(char filename[],char answers[], int SIZE);
void compareAnswers(char student[], char correct[], int SIZE, int & numMissed);
void displayTestResults(int numMissed, int SIZE);
int main()
{
// Number of questions missed.
int numMissed;
// Array to hold the correct answers
char correct[SIZE];
// Array to hold the student's answers
char student[SIZE];
//Read the correct answers.
readFile("CorrectAnswers.txt", correct, SIZE);
// Read the student's answers.
readFile("StudentAnswers.txt", student, SIZE);
// Compare the student's answers with the correct
// answers.
compareAnswers(student, correct, SIZE, numMissed);
// Display the test results.
displayTestResults(numMissed, SIZE);
system("PAUSE");
return 0;
}
// ********************************************************
// The readFile function reads the contents of an answer *
// file into an array. *
// ********************************************************
void readFile(char filename[], char values[], int SIZE)
{
fstream inFile;
// Open the file.
inFile.open(filename);
for (char i = 0; i < SIZE; i++)
{
inFile>>values[i];
inFile.close();
}
return;
}
// ********************************************************
// The compareAnswers function compares the elements of *
// the student array with the elements of the correct *
// array. For each student answer that is incorrect the *
// funcction increments a counter that keeps track of the *
// number of incorrect answers, making this available via *
// a call by reference argument. The function displays *
// the question number answered incorrectly along with *
// answer given by the student and the correct answer. *
// ********************************************************
void compareAnswers(char student[], char correct[],
int SIZE, int &numMissed)
{
// Initialize numMissed.
numMissed = 0;
cout<< "Checking the answers...n";
// Step through the answer arrays comparing
// each answer.
for (char i = 0; i < SIZE; i++){
if(student[i] == correct[i])
{i++;}
else
{
numMissed++;
cout<<"I'm sorry, you had the wrong answer for "<<i + 1<<"."<<endl;
cout<<"Your answer was "<<student[i]<<".";
cout<<endl;
cout<<"The correct answer was "<<correct[i]<<".";
i++;
cin.ignore();
}
}
return;
}
// ********************************************************
// The displayTestResults function displays the test *
// statistics. *
// ********************************************************
void displayTestResults(int numMissed, int SIZE)
{
int temp;
// Calculate the number of correctly answered
// questions.
int correctlyAnswered = SIZE - numMissed;
// Calculate the numeric score correctly rounded to the nearest tenth of a percent.
temp == static_cast<int>(static_cast<double>(correctlyAnswered/SIZE*10+0.5));
// Display the number of correctly answered
// questions.
cout<<" The number of questions you got correct is:" <<correctlyAnswered;
// Display the percentage of correctly
// answered questions.
cout<<" The percentage of questions you got correct is:"<<temp;
//Display the letter grade on the exam.
cout<<endl<<endl;
if (temp >=93)
{cout<<"Your letter grade is an: A";}
else if(temp>=89)
{cout<<"Your letter grade is an: A-";}
else if(temp>=87)
{cout<<"Your letter grade is a: B+";}
else if(temp>=83)
{cout<<"Your letter grade is a: B";}
else if(temp>=79)
{cout<<"Your letter grade is a: B-";}
else if(temp>=77)
{cout<<"Your letter grade is a: C+";}
else if(temp>=73)
{cout<<"Your letter grade is a: C";}
else if(temp>=69)
{cout<<"Your letter grade is a: C-";}
else if(temp>=67)
{cout<<"Your letter grade is a: D+";}
else if(temp>=63)
{cout<<"Your letter grade is a: D";}
else if(temp>=57)
{cout<<"Your letter grade is a: D-";}
else
{cout<<"Your letter grade is a: F"<<endl;}
return;
`enter code here`}
若要使警告静音,应将该函数声明为:
void readFile(const char *filename, char *answers, int SIZE)
它之所以抱怨,是因为您将一个常量的字符串文本传递给一个不将参数声明为 const 的函数。
要解决乱码问题,请将 for 循环的主体更改为:
if(student[i] != correct[i]) {
numMissed++;
cout<<"I'm sorry, you had the wrong answer for "<<i + 1<<"."<<endl;
cout<<"Your answer was "<<student[i]<<".";
cout<<endl;
cout<<"The correct answer was "<<correct[i]<<".";
cin.ignore();
}
您不需要在循环内执行i++
,它已经在循环标头中完成。
相关文章:
- 使用提升将数据从 PyObject 复制到浮点数 *
- 如何使用 Google Test 向测试添加元数据 / 如何将数据从 Google Test 发送到 TestEven
- C++编程从外部文本文件定义数组大小
- 将相机数据从服务器实时流式传输到客户端
- 如何在 C/C++ 中从外部库调用函数
- 如何使用位字段将数据从二进制文件复制到结构中?
- 使用蓝牙组件将数据从Android手机传输到串行设备时出现问题
- 在CRC-16 CCITT中将数据从二进制解码为文本,我应该输入一个码字,使用CRC生成器进行编码
- 如何将数据从一个结构链接到另一个结构
- 尝试将数据从C++发送到 Python 并使用套接字C++反转,C++ sendto() 和 python recvfr
- 在 c++ 中将数据从文件读取到队列中
- 将数据从 std::Vector 存储到 Eigen::Vector 时出错
- 如何在C++中将用户数据从一个 Lua 块传递到另一个块
- OpenGL - 从外部文件绘制点
- 从外部 VM 复制特定数据的最佳方法是什么?
- C++从外部设备读取数据 - 连接:光电USB
- 减少从外部数据文件中检索字符数组的重复
- 为什么我的数组没有输出正确的数据,这些数据是从外部文件输入的?
- 如何显示QWidget以上数据流从外部库处理的设备
- 将外部中断产生的数据从c++程序传输到Linux中的Java