Question

For some reason, the code just crashes and gives me an Access Violation error. It keeps saying the object users is not initialized.

First-chance exception at 0x0F45E89A (msvcr110d.dll) in Random Coding in C++.exe: 0xC0000005: Access violation writing location 0xABABABAB.Unhandled exception at 0x0F45E89A (msvcr110d.dll) in Random Coding in C++.exe: 0xC0000005: Access violation writing location 0xABABABAB.

Thanks. Any ideas?

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

using namespace std;

struct User { 
    string name; //Both first and last name go here 
    int birthYear; 
    string major; 
}; 

int main()
{
    ifstream input("input.txt");

    if(!input || !input.is_open())
        return -1;

    string buffer;
    int count = -1;
    int index = 0;
    int size;
    User* users;

    while(getline(input, buffer, '\n'))
    {
        stringstream ss(buffer);

        if(count == -1)
        {
            ss >> size;
            users = new User[size];
            count = 0;
        }
        else
        {
            if(count == 0)
            {
                users[index].name = buffer;
                count++;
            }
            if(count == 1)
            {
                ss >> users[index].birthYear;
                count++;
            }
            if(count == 2)
            {
                users[index].major = buffer;
                count = 0;
                index++;
            }
        }
    }

    for(int i = 0; i < 2; i++)
    {
        cout<<users[i].name << " " << users[i].birthYear << " " << users[i].major <<endl;
    }
    system ("PAUSE");
    return 0;
}
Was it helpful?

Solution

for(int i = 0; i < 2; i++)
{
    cout<<users[i].name << " " << users[i].birthYear << " " << users[i].major <<endl;
}

Looks incoorect. How are you so sure that users contain atleast two elements. If getline fails because of badfile or first line suggests only 1 record, you will get above exception.

You should change the loop to

// Initialize size with 0 before while(getline) loop
for(int i = 0; i < size; i++)
{
    cout<<users[i].name << " " << users[i].birthYear << " " << users[i].major <<endl;
}

Also below lines of code look problematic

if(count == 0)
{
    users[index].name = buffer;
    count++;
}
if(count == 1)
{
    ss >> users[index].birthYear;
    count++;
}
if(count == 2)
{
    users[index].major = buffer;
    count = 0;
    index++;
}

When count is 0, it would go into first if condition and get incremented. Then condition count == 1 would become true and you will visit next 2 conditions also. You should replace next 2 if conditions with else if or a switch with break statements to see the intended behavior.

Also it would be a good practice to free users after you are done with it.

OTHER TIPS

What I think is that the first getline fails (the while does not enter, so nothing is created) and you automatically reach the for loop. Check in the for that you actually have users.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top