Question
Arithmetic overflow casting 4 byte value into 8 byte value. How to fix? C++ I am getting 4 warnings with the above line as shown:
Arithmetic overflow casting 4 byte value into 8 byte value. How to fix? C++
I am getting 4 warnings with the above line as shown:
Why is this happening and how do I fix it? I appreciate any help, thanks.
#define _CRTDBG_MAP_ALLOC #define _CRT_SECURE_NO_WARNINGS #include #include #include #include
using std::cin; using std::cout; using std::endl; using std::ifstream; using std::ofstream; using std::setw; using std::right; using std::left;
const char DATA_IN[] = { "data.txt" }; const char DATA_OUT[] = { "report.txt" };
void readWords(int& total, char**& words, int*& count); void writeFile(int& total, char** words, int* count); void outputFile(int& total, char** words, int* count); void countWords(int& total, char**& words, int* count, char* buffer); int findWord(char** words, int* count, int total, char* buffer);
int main() { _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); ifstream input(DATA_IN); ofstream output(DATA_OUT);
int total = 0; char** words = nullptr; int* count = nullptr; char** temp_word = nullptr;
if (input.is_open()) { // load the file readWords(total, words, count);
// write to the file writeFile(total, words, count);
// output to the screen outputFile(total, words, count); } else cout
// free allocated memory for (int i = 0; i
input.close(); return 0; }
// read words from text file void readWords(int& total, char**& words, int*& count) { ifstream input(DATA_IN);
total = 0; char buffer[256]; char** temp_word = nullptr;
input >> buffer; // priming read // while input is not at the end of file while (!input.eof()) { // index = findWord function int index = findWord(words, count, total, buffer);
// if a word is found if (index >= 0) { count[index]++; // increment count index } else { char** temp_word = new char* [total + 1]; // allocate space for temp_word size_t len = strlen(buffer) + 1; // set length of buffer temp_word[total] = new char[len]; // allocate length for temp_word strcpy_s(temp_word[total], len, buffer); // copy buffer to words
int* temp_count = new int[total + 1]; temp_count[total] = 1;
for (int i = 0; i
delete[] count; count = temp_count; delete[] words; words = temp_word;
countWords(total, words, count, buffer); // call process function
++total; } input >> buffer;
} }
int findWord(char** words, int* count, int total, char* buffer) { int found_word = -1; // total number of words in input file
for (int i = 0; i
findWord(words, count, total, buffer); // if word is already in array if (found) { // allocate new array of temp_word pointers one larger than the current number of words char** temp_word = new char* [total + 1]; int* temp_count = new int[total + 1];
int counter = 0; // make new array point to any words located before the one to delete while (counter
words = temp_word; count = temp_count; for (int i = 0; i
//write list of words to file void writeFile(int& total, char** words, int* count) { ofstream output(DATA_OUT);
if (output.is_open()) { // for each entry, write to the screen for (int i = 0; i
}
// output word frequency occurence to screen void outputFile(int& total, char** words, int* count) { cout
}
A J! A A C26451 Arithmetic overflow: Using operator '+' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator to avoid overflow (10.2). C26451 Arithmetic overflow: Using operator'+'on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator '+'to avoid overflow (10.2). C26451 Arithmetic overflow: Using operator '+'on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator ''to avoid overflow (10.2), C26451 Arithmetic overflow: Using operator '+' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator ''to avoid overflow (10.2). Lab 12.3 11 Lab 12.3 || Lab 12.3 || Lab 12.3 || CONVERT.CPP CONVERT.CPP CONVERT.CPP CONVERT.CPP A J! A A C26451 Arithmetic overflow: Using operator '+' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator to avoid overflow (10.2). C26451 Arithmetic overflow: Using operator'+'on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator '+'to avoid overflow (10.2). C26451 Arithmetic overflow: Using operator '+'on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator ''to avoid overflow (10.2), C26451 Arithmetic overflow: Using operator '+' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator ''to avoid overflow (10.2). Lab 12.3 11 Lab 12.3 || Lab 12.3 || Lab 12.3 || CONVERT.CPP CONVERT.CPP CONVERT.CPP CONVERT.CPPStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started