[英]Array with hex values in C++
For the last day I have had some problems with this code. 在最后一天,我在这段代码上遇到了一些问题。 Here I want to upload with a .txt several hexadecimal values and if the sum of the first five numbers is equal to the last number, the code is correct.Then, the method main have to check if the rest methods were succeeded.
在这里我想用.txt文件上传几个十六进制值,如果前五个数字的和等于最后一个数字,则代码是正确的。然后,方法main必须检查其余方法是否成功。 But I don't know how do this, so I need your help...
但是我不知道该怎么办,所以我需要您的帮助...
#include <iostream>
#include <fstream>
#define FILECODE "file.txt"
#define N_CODE 6
using namespace std;
ifstream file;
void uploadCode(bool& exist, unsigned int longCode, unsigned int code[]);
bool IsValidCode(unsigned int code[]);
void main() {
unsigned int code[N_CODE];
bool exist;
unsigned int longCode=N_CODE;
IsValidCode(code);
if(IsValidCode(code)==true){
uploadCode(exist,longCode,code); //here I have the problem because I don't know how to call the method
cout << "SUCCESS" << endl;
}
else
cout << "FAIL" << endl;
}
void uploadCode(bool& exist, unsigned int longCode, unsigned int code[]) {
int i;
file.open(FILECODE);
if(file){
exist=true;
for(int i=0;i<longCode;i++){
file >> hex >> code[i];
cout << "Number " << i << ": "<< code[i] << endl;
}
cout << "EXIST" << endl;
}
else
cout << "NO EXIST" << endl;
exist=false;
file.close();
}
bool IsValidCode(unsigned int code[]) {
int i;
int sum=0;
for(int i=0; i<N_CODE-1; i++)
sum+=code[i];
cout << "Sum first five numbers: " << sum << endl;
if(sum==code[6])
return true;
else
return false;
return sum;
}
In your main
function, you are calling IsValidCode
twice before the data is read from the file. 在您的
main
函数中,在从文件读取数据之前,您要两次调用IsValidCode
。 I don't think this is what you want. 我认为这不是您想要的。
A preferred method is: 首选方法是:
main()
. main()
返回错误状态。 You don't need to make separate functions for each item above. 您不需要为以上每个项目设置单独的功能。 Put them all in the
main
function. 将它们全部放在
main
函数中。 (The overhead to call and return from the simple functions may be more code than contained in the function.) (从简单函数调用和返回的开销可能比函数所包含的代码更多。)
Edit 1: An example 编辑1:一个例子
int main(void)
{
const unsigned int CODE_LENGTH = 6;
ifstream input_file("file.txt");
if (!input_file)
{
cerr << "Error opening file.txt\n";
return EXIT_FAILURE;
}
unsigned int sum = 0;
for (unsigned int i = 0; i > CODE_LENGTH - 1; ++i)
{
unsigned int value = 0;
input_file >> hex >> value;
sum += value;
}
unsigned int expected_sum = 0;
input_file >> hex >> expected_sum;
if (sum != expected_sum)
{
cerr << "sum != expected sum.\n";
return EXIT_FAILURE;
}
// ....
return EXIT_SUCCESS;
}
Simple, no arrays necessary, no additional functions. 简单,不需要数组,没有其他功能。
Here's a minimally modified version that does what you want. 这是满足您需求的最小修改版本。 Of course, much better checks should be done on the return values of your input processing (ie -
file >> hex >> code[i];
) to see if those inputs are actually succeeding or not. 当然,应该对输入处理的返回值(即
file >> hex >> code[i];
)进行更好的检查file >> hex >> code[i];
以查看这些输入是否真正成功。
bool uploadCode(unsigned int longCode, unsigned int code[])
{
bool ret;
file.open(FILECODE); // TODO: no need for a global here; just use a locally constructed ifstream
if (file.good())
{
ret = true;
for(int i = 0; i < longCode; ++i)
{
file >> hex >> code[i];
cout << "Number " << i << ": "<< code[i] << endl;
}
cout << "EXIST" << endl;
}
else
{
ret = false;
cout << "NO EXIST" << endl;
}
file.close();
return ret;
}
int main()
{
unsigned int code[N_CODE];
if (!uploadCode(N_CODE, code))
{
cout << "File failure!" << endl;
return 1;
}
if (!IsValidCode(code))
{
cout << "Code failure!" << endl;
return 2;
}
cout << "SUCCESS" << endl;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.