Array with hex values in C++ -
for last day have had problems code. here want upload .txt several hexadecimal values , if sum of first 5 numbers equal last number, code correct.then, method main have check if rest methods succeeded. don't know how this, need 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 have problem because don't know how call 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 " << << ": "<< 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 5 numbers: " << sum << endl; if(sum==code[6]) return true; else return false; return sum; }
here's minimally modified version want. of course, better checks should done on return values of input processing (i.e. - file >> hex >> code[i];
) see if inputs succeeding or not.
bool uploadcode(unsigned int longcode, unsigned int code[]) { bool ret; file.open(filecode); // todo: no need global here; use locally constructed ifstream if (file.good()) { ret = true; for(int = 0; < longcode; ++i) { file >> hex >> code[i]; cout << "number " << << ": "<< 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; }
Comments
Post a Comment