Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is my issue..I am getting a Debug Assertion Error StringSubscript out of ra

ID: 3618995 • Letter: H

Question

Here is my issue..I am getting a Debug Assertion Error StringSubscript out of range error and i have no clue on how to fix it.Here is my input.txt file and my code.


1,A,C,B,D,B,A,B,D,C,A
2,B,D,C,A,C,B,A,C,A,B
3,C,A,D,B,B,D,C,B,A,C
4,D,B,A,C,B,C,D,A,A,D
5,B,A,C,A,B,B,C,D,A,B
6,C,C,D,B,C,C,B,D,B,D
7,D,B,A,C,B,A,D,C,D,B
8,A,D,B,D,C,D,A,B,C,A
9,C,D,D,B,B,A,C,B,C,D
10,D,B,A,C,B,C,D,A,C,D
11,A,C,B,D,C,B,A,D,B,A
12,B,A,C,A,B,D,B,C,B,C
13,D,A,D,C,B,D,B,A,C,A
14,A,B,A,D,C,A,C,B,C,D
15,B,C,B,A,B,C,A,D,B,D
16,C,D,C,B,B,B,D,A,C,B
17,D,B,A,D,C,B,A,C,D,B
18,B,C,B,A,D,D,a,C,B,C
19,A,D,C,B,B,C,D,B,D,A
20,C,A,D,C,B,A,B,D,C,A

#include <iomanip>
#include <fstream>
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
int main()
{
    char ch;
    string data;
    char input[10][20];
    int j,k,count;
    char *fileName = new char[10];
    string in;

    ifstream infile;
    ofstream outfile;
    outfile.open("data.csv",ios::app);        //out filewhere data is going.


    char dateStr[9];
    _strdate_s(dateStr);
    printf("The current date is %s ",dateStr);
        cout << "Thisprogram turns data into percentages." << endl;
        cout << "Enter 'Y'or 'y' to continue." <<endl;
        cout << endl;
        cin >> ch;
          //prompt user to enter input file name
    cout << " Enter the input file name:";
    cin >> fileName;
    //open input file
    infile.open(fileName);
    if (!infile.is_open())
    {
        cout << "unable toopen the input file.";
        system("pause");
        exit(1);
    }
    for (int i = 0; i < 20; i++)
    {  
        infile>>in;
        j=0;
        while(in[j]!=',')
                 j++;
        for(k=0;k<10;k++)
            {
               while(in[j]<'A'||in[j]>'D')
                   j++;
             input[k][i]=in[j];
             j++;
             }
    }
for(int i='A';i<='D';i++)
{
    for(k=0;k<10;k++)
    {
        count=0;
    for(j=0;j<20;j++)
       {
          if(input[k][j]==i)
           count++;
       }
     outfile<<(char)i<<","<<k+1<<","<<count/20.*100.<<endl;
   }
}            
infile.close();
outfile.close();
   
    system("pause");

}

Explanation / Answer

On line 18, you have a lower case 'a' instead of an upper case'A'. That may be a typo, or you may want to handle that asanother case in your calculations. Making the 'a' upper case removed the run-time error when Iexecuted your code.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote