-6

I am making a program that reads a text file that has a bunch of sale numbers and it works fine except that i keep on getting a an error which states: Access violation reading location 0xCCCCCCCC.

Here is the relevant part of my code: Any help would be greatly appreciated.

void task1()
{

//  const int MAX_CHARS_PER_LINE = 512;
//  const int MAX_TOKENS_PER_LINE = 20;
//const char* const DELIMITER = " ";

string year;
string line;
double unitsold1[12],unitsold2[12],unitprice1[12],unitprice2[12];
string month[12];

string name="D:\\Sandesh Pyakurel\\Model_X_Sale_";
cout<<"\nPlease enter a year in the yyyy format: ";
cin>>year;

string result=name+year+".txt";

ifstream myfile;
myfile.open(result.c_str());

double a,b,c,d;
if(!myfile)
{
    cout<<"Could not open file"<<endl;

}

if (myfile.is_open()) 
{

    int count =0;
    while (myfile>>month[count]
                 >>unitsold1[count]
                 >>unitprice1[count]
                 >>unitsold2[count]
                 >>unitprice2[count]   )
           {
               count++;
           }

    myfile.close();
    for(int i=0;i<=count;i++)
    {
        cout<<month[i]<<" "<<unitsold1[i]<<" "<<unitprice1[i]<<" "    <<  
                    unitsold2[i]<<" "<<unitprice2[i]<<" "<<endl;

    }




        /*  
        char myarray[5];

        for(int i=0;i<=5;i++)
                {   myfile.getline(myarray,100);
                    cout<<myarray[i];
                }
        */
        /*
            char buf[100];
            myfile.getline(buf,100);


            int n=0;
            const char* token[100]={};



            token[0]=strtok(buf,DELIMITER);
            if(token[0])
            {
                for (n=1;n<MAX_TOKENS_PER_LINE;n++)
                {
                    token[n]=strtok(0,DELIMITER);
                    if(!token[n])
                        break;
                }

            }



            for(int i=0;i<n;i++)
            {

                stringstream ss(token[i]);
                ss>>tmp>>a>>b>>c>>d;
                cout<<ss<<endl;
            }*/

}

}
  • Replace `i<=count` with `i < count`. – Crowman May 04 '14 at 20:36
  • 2
    see e.g. http://stackoverflow.com/questions/15302364/c-access-violation-reading-location-0xcccccccc?rq=1 the 0xcccccccc pattern is commonly used to fill memory so that it can be recognized as uninitialized, for debug build. so you're using an uninitialized pointer. – Cheers and hth. - Alf May 04 '14 at 20:38
  • 2
    If you had done even the smallest amount of debugging, you would have discovered that the problem occurs when `i == 12`. – Ben Voigt May 04 '14 at 20:46
  • 0xCCCCCCCC is uninitialized memory [When and why will an OS initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?](http://stackoverflow.com/q/370195/995714) – phuclv Apr 28 '15 at 15:45
  • *"I am making a program that ... works fine except ... Access violation reading location 0xCCCCCCCC."* - I don't think it is working fine... – jww Nov 08 '18 at 03:29

1 Answers1

0

Presuming you have twelve months in your input file, you're reading past the end of your arrays. Replace:

for(int i=0;i<=count;i++)
{

with:

for( int i=0; i < count; ++i )
{
Crowman
  • 25,242
  • 5
  • 48
  • 56