Chris
Chris

Reputation: 311

C++ Buffer is too small Error

Intermittently, Visual Studio throws an exception when running my code. I say intermittently because I've been able to successfully run my code without an error. The error was thrown after I created the function "print_Days."

The exception thrown is:

Debug Assertion Failed!

File: minkernel\crts\ucrt\corecrt_internal_string_templates.h

Line: 81

Expression: (L"Buffer is too small" && 0)

The function reads from a .txt file that has 7 days of the week listed (Monday thru Sunday) and then alphabetically sorts the days in a 2D c-string array (professor is making us use c-string instead of string unfortunately).

Here is all of my code:

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>

using namespace std;
//Constants for 2D array
const int NUM_OF_ROWS = 7; //Seven days listed in the file
const int NUM_OF_COLS = 10; //Longest word is 9 chars long, plus \0

void get_Days(ifstream& file, char days[][NUM_OF_COLS], int rows);
void sort_Days(char days[][NUM_OF_COLS], int rows);
void print_Days(const char days[][NUM_OF_COLS], const int rows);

void get_Days(ifstream& file, char days[][NUM_OF_COLS], int rows) {
   //Read from text file and return day
   for (int i = 0; i < rows; ++i)
   {
      file >> days[i];
   }
}

void sort_Days(char days[][NUM_OF_COLS], int rows) {
   //Sort the array alphabetically
   char temp[NUM_OF_COLS];
   for (int i = 0; i < rows; i++)
   {
      for (int j = 0; j < rows; j++)
      {
         if (strcmp(days[j - 1], days[j]) > 0)
         {
            strcpy_s(temp, days[j - 1]);
            strcpy_s(days[j - 1], days[j]);
            strcpy_s(days[j], temp);
         }
      }
   }
}

void print_Days(const char days[][NUM_OF_COLS], const int rows) {
   //Print the sorted array to the console
   for (int i = 0; i < NUM_OF_ROWS; ++i)
      for (int i = 0; i < rows; i++)
      {
         for (int j = 0; j < NUM_OF_COLS; j++)
         {
            cout << days[i][j] << endl;
         }
      }
}

int main() {
   //This program reads from a file (days.txt), sorts the days 
   // alphabetically, and then prints the result to the console.
   ifstream infile("days.txt");
   char days[NUM_OF_ROWS][NUM_OF_COLS];
   if (!infile)
   {
      cout << "File (days.txt) does not exist." << endl;
      return 1;
   }
   get_Days(infile, days, NUM_OF_ROWS);
   infile.close();
   sort_Days(days, NUM_OF_ROWS);
   print_Days(days, NUM_OF_ROWS);
   return 0;
}

Upvotes: 2

Views: 4102

Answers (1)

L.Y. Sim
L.Y. Sim

Reputation: 900

A few things are wrong with the code:

sort_Days

The sort_Days algorithm is throwing errors because you are trying to index days[j - 1] when the nested for loop starts with j = 0. So your initial index is out of bounds.

Furthermore, it seems like you are trying to perform bubble sort on the c-style strings, but your bubble sort implementation is incorrect. Please consult this page for how to implement a simple bubble sort. Hint: the for loop conditional, strcmp and strcpy_s indices need some tweaking.


print_Days

Your print_Days function is incorrect. Here is a version that prints out each c-style string instead of each char within the string:

void print_Days(const char days[][NUM_OF_COLS], const int rows) 
{
    for (int j = 0; j < rows; j++)
    {
        cout << days[j] << endl;
    }
}

You should know that std::cout understands that when you pass it a c-style string (i.e. the char[NUM_OF_COLS] within days), it means you want to print out the whole string up to the null-terminator.

Your for loop termination conditional was also wrong, because you had j < NUM_OF_COLS, whereas days actually is an array with NUM_OF_ROWS elements, and each element is an array of NUM_OF_COLS size. The way you had it had indexing out of bounds of the days array.


And while I am nitpicking

Try not to use using namespace::std;, there are plenty of reasons why you shouldn't..

Upvotes: 2

Related Questions