arrays - Number input from txt file. Weird glitch C++ -
this homework , i've developed of on own already. keep getting weird glitch. in input file below supposed take name , numbers , put them 6 different variables. name string numbers arrays , last 2 numbers ints.
when run program , want grab tenth number of strings presents me next 1 on eg. want 94 15 in alice's grades.
here's file , here code please help.
#include <iostream> #include<string> #include <fstream> #include <sstream> using namespace std; //all of students info stored here struct student { string name; int asgn[9]; int quiz[9]; int lab[9]; int midterm; int final; }; student s[22]; int main(){ string str; ifstream in; in.open("c:/cs161grades.txt"); (int = 0; < 22; i++){ getline(in, str); stringstream ss(str); ss >> s[i].name >> s[i].asgn[0] >> s[i].asgn[1] >> s[i].asgn[2] >> s[i].asgn[3] >> s[i].asgn[4] >> s[i].asgn[5] >> s[i].asgn[6] >> s[i].asgn[7] >> s[i].asgn[8] >> s[i].asgn[9] >> s[i].quiz[0] >> s[i].quiz[1] >> s[i].quiz[2] >> s[i].quiz[3] >> s[i].quiz[4] >> s[i].quiz[5] >> s[i].quiz[6] >> s[i].quiz[7] >> s[i].quiz[8] >> s[i].quiz[9] >> s[i].lab[0] >> s[i].lab[1] >> s[i].lab[2] >> s[i].lab[3] >> s[i].lab[4] >> s[i].lab[5] >> s[i].lab[6] >> s[i].lab[7] >> s[i].lab[8] >> s[i].lab[9] >> s[i].midterm >> s[i].final; cout << s[2].name << s[2].lab[9] << endl; } //pause , stop getchar(); getchar(); return 0; } the file formatted such twenty students
name assignments quizzes labs midt final alice 71 97 64 30 99 54 8 88 92 94 15 55 91 37 71 1 5 52 37 46 6 19 17 68 7 97 36 31 33 10 72 46
here:
int asgn[9]; that's room enough nine numbers (asgn[0] through asgn[8]), you're trying store ten in it. assignment asgn[9] writes past end of array, undefined behavior.
Comments
Post a Comment