help with records

Write your question here.we been ask by our professor to create a program using records .to perform an array 10x10 col&rows.



#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <algorithm>

using namespace std;

struct politician_struct
{
char sur_name[15];
char last_name[15];
char first_name[15];
char party_list;
char current_position[20];
char department[15];
char educ_attainment;
float annual_salary;
int saln;
char known_as;
char religion;
char implemented_law;
};

int main()
{
politician_struct politician[15];

const int size=30;

int i = 0;
ifstream inFile;
inFile.open("sampledata.txt");


for (i = 0; i < 50; i++)
{


inFile >> politician[i].sur_name >> politician[i].last_name >> politician[i].first_name >> politician[i].party_list >> politician[i].current_position >> politician[i].department>> politician[i].educ_attainment>> politician[i].annual_salary
;politician[i].saln ; politician[i].known_as ; politician[i].religion ; politician[i].implemented_law;


inFile.close();


}





return 0;
}
Welcome to the forum. When posting code, please use code tags so the server displays line numbers that we can refer to. Just edit the post, highlight the code and click the <> button to the right of the edit window.
1
2
politician[i].annual_salary
;politician[i].saln ; politician[i].known_as ; politician[i].religion ; politician[i].implemented_law;

That looks wrong to me. I assume you intend to keep reading the record, which means these lines should be:
politician[i].annual_salary >> politician[i].saln >> politician[i].known_as >> politician[i].religion >> politician[i].implemented_law;
Last edited on
Line 27: You only allocate 15 elements for your politician array.

Line 36: You're attempting to read up to 50 records into your politician array. This will cause an out of bounds reference if there are more than 15 entries in your file.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Topic archived. No new replies allowed.