but master Vernon, i have 11 examcode for each student...if i chage 11 to 12? can or not?
nurulshidanoni -1 Posting Whiz in Training
dot.muffin 0 Newbie Poster
Here is the way it works -
"int examcode[11]" opens up a new array with 11 "slots" for integers. To access the first, use examcode[0]. To access the eleventh, use examcode[10]. Though the counting starts at 0, there are still 11 values stored in the array.
Hope this clears things up.
nurulshidanoni -1 Posting Whiz in Training
oo...i understand..i try again.
nurulshidanoni -1 Posting Whiz in Training
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
struct student
{
string studentid;
int examcode[10];
};
ifstream stream1("Standrews83.txt");
char a[611];
if(!stream1)
{
cout << "While opening a file an error is encountered" << endl;
}
else
{
cout << "File is successfully opened" << endl;
}
{
[B]vector <student> students;
student aStudent;[/B] while (stream1 >> aStudent.studentid)
{
for (int i = 0; i < 10; i++)
{
stream1 >> aStudent.examcode[i];
stream1 >> aStudent.studentid;
}
stream1 >> aStudent.examcode[11];
[B] students.push_back (aStudent);[/B] }
for (int i = 0; i < students.size (); i++)
{
[B] cout << students.at (i).studentid << endl;[/B] for (int j = 0; j < 10; j++)
[B] cout << students.at (i).examcode[j] << endl;[/B] }
return(0);
}
}
nurulshidanoni -1 Posting Whiz in Training
still error..
VernonDozier 2,218 Posting Expert Featured Poster
Master Vernon. Ha ha! That's great.
Let's look at this loop carefully:
while (stream1 >> aStudent.studentid) // part 1
{
for (int i = 0; i < 10; i++) // part 2
{
stream1 >> aStudent.examcode[i];
stream1 >> aStudent.studentid;
}
stream1 >> aStudent.examcode[10]; // part 3
students.push_back (aStudent);
}
Recall that your input file looked something like this (for each student):
s1 0003
s1 0013
s1 0034
s1 0054
s1 0071
s1 0081
s1 0097
s1 0105
s1 0116
s1 0135
s1 0138
You have 22 things to read in. You are reading in "s1" 11 times, but you still need to read it in.
So the while loop is organized like this:
1) Read in the first piece of data, which is "s1" on the first line.
2) Read in the next 20 pieces of data.
3) Read in the last piece of data (0138).
That's 22 pieces of data to be read in for each student. Parts 1 and 3 read in one piece of data each. That means that the for-loop (part 2) needs to read in 20 pieces of data. Each pass through the for-loop reads in 2 pieces of data:
for (int i = 0; i < 10; i++) // part 2
{
stream1 >> aStudent.examcode[i]; // exam code. First piece of data
stream1 >> aStudent.studentid; // student id. 2nd piece of data
}
So 20 pieces of data divided by 2 pieces of data for each pass through the loop = 10 passes through the loop, not 11.
Finally, after you are done with the for-loop, you still have not read in examcode[10] since the loop's control specifies that i must be LESS THAN 10. So the number of passes through the loop is one fewer than the number of data pairs that you need to read in due to parts 1 and 3. If you designed your loop with "eof" as the test for the while loop, you would likely design it so that you indeed go through the for-loop 11 times. However you design it, each pass through the while loop must consume 22 pieces of data. In this case, the 22 pieces of data are consumed in this way:
Part 1 : 1 piece of data
Part 2 : 20 pieces of data
Part 3 : 1 piece of data
---------------------------
Total : 22 pieces of data (1 + 20 + 1 = 22)
Again, there is more than one way to do it.
VernonDozier 2,218 Posting Expert Featured Poster
Your code has some bizarre spacing. Makes it kind of hard to follow. However, I see one error at least (we're posting pretty fast so you may have already corrected it). As I mentioned in a previous post, this will be a segmentation fault:
stream1 >> aStudent.examcode[11];
Needs to be this:
stream1 >> aStudent.examcode[10];
VernonDozier 2,218 Posting Expert Featured Poster
Change this:
struct student
{
string studentid;
int examcode[10];
};
back to this, like you had before:
struct student
{
string studentid;
int examcode[11];
};
nurulshidanoni -1 Posting Whiz in Training
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
struct student
{
string studentid;
int examcode[10];
};
ifstream stream1("Standrews83.txt");
char a[611];
if(!stream1)
{
cout << "While opening a file an error is encountered" << endl;
}
else
{
cout << "File is successfully opened" << endl;
}
{
[B]vector <student> students;[/B] student aStudent;
while (stream1 >> aStudent.studentid) //part 1
{
for (int i = 0; i < 10; i++)
{
stream1 >> aStudent.studentid; //student id. 1st piece of data
stream1 >> aStudent.examcode[i]; //exam code. 2nd piece of data
stream1 >> aStudent.studentid; //student id. 3nd piece of data
}
stream1 >> aStudent.examcode[10]; //part 3
students.push_back (aStudent);
}
for (int i = 0; i < students.size (); i++)
{
cout << students.at (i).studentid << endl;
for (int j = 0; j < 10; j++)
cout << students.at (i).examcode[j] << endl;
}
return(0);
}
}
I have change to read exam code..but the bold still problem
nurulshidanoni -1 Posting Whiz in Training
int examcode[11];
};struct student
{
string studentid;
int examcode[11];
};
why must change Master Vernon? u said 0 to 11?means [10]
VernonDozier 2,218 Posting Expert Featured Poster
Change this:
int examcode[11];
};struct student
{
string studentid;
int examcode[11];
};
back to this:
struct student
{
string studentid;
int examcode[11];
};
It all goes INSIDE the struct and it only needs to be done once.
This:
int examcode[11];
tells the compiler to SET ASIDE SPACE for 11 integers. These 11 integers have "indexes" (might be spelled wrong - plural of "index"). These 11 integers are referred to like this:
examcode[0] - refers to 1st integer.
examcode[1] - refers to 2nd integer.
examcode[2] - refers to 3rd integer.
examcode[3] - refers to 4th integer.
examcode[4] - refers to 5thinteger.
examcode[5] - refers to 6th integer.
examcode[6] - refers to 7th integer.
examcode[7] - refers to 8thinteger.
examcode[8] - refers to 9th integer.
examcode[9] - refers to 10th integer.
examcode[10] - refers to 11th integer.
Just remember this. If you need space for 11 integers, do THIS ONCE (in your case this is done at the top inside of the student struct):
int examcode[11];
When you want to refer to those integers LATER, what goes in the brackets is always in the range of 0 through 10 (which is 11 minus 1). The reason for this is that it makes things more efficient for the computer when it runs the code. There is less arithmetic to do when compiling and running to figure out where these integers are stored. I wouldn't worry about the "why" for now actually. In BASIC, everything started at 1 and it was converted for you behind the scenes at run-time. In C++, the indexes always start at 0, not 1. Just remember that if you do this at top inside your struct:
int examcode[11];
you can't do this later:
stream1 >> aStudent.examcode[11];
You CAN do this though:
stream1 >> aStudent.examcode[10];
and you CAN do this:
stream1 >> aStudent.examcode[0];
VernonDozier 2,218 Posting Expert Featured Poster
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
struct student
{
string studentid;
[B] int examcode[10];[/B] // change to 11
};
ifstream stream1("Standrews83.txt");
char a[611];
if(!stream1)
{
cout << "While opening a file an error is encountered" << endl;
}
else
{
cout << "File is successfully opened" << endl;
}
[B] { [/B] // delete this bracket
vector <student> students; student aStudent;
while (stream1 >> aStudent.studentid) //part 1
{
for (int i = 0; i < 10; i++)
{
stream1 >> aStudent.studentid; //student id. 1st piece of data
stream1 >> aStudent.examcode[i]; //exam code. 2nd piece of data
stream1 >> aStudent.studentid; //student id. 3nd piece of data
}
stream1 >> aStudent.examcode[10]; //part 3
students.push_back (aStudent);
}
for (int i = 0; i < students.size (); i++)
{
cout << students.at (i).studentid << endl;
[B]for (int j = 0; j < 10; j++)[/B] // change 10 to 11
cout << students.at (i).examcode[j] << endl;
}
[B] return(0); [/B] // change to: return 0;
}
[B]}[/B] // delete this bracket
You have a bracket that doesn't need to be there before the word "vector". You also will probably have to take out the second bracket at the very last line. See my comments in the above code. Also, this:
return (0);
should probably be this:
return 0;
Again, this:
int examcode[10];
should be this:
int examcode[11];
This (towards the end when you are displaying):
[B]for (int j = 0; j < 10; j++)[/B]
should be this (you want to display all 11 exam codes so go through the loop 11 times):
[B]for (int j = 0; j < 11; j++)[/B]
Your code is hard to read. The missing brackets, etc., will be far easier to spot/change if you use different spacing (i.e. indent the inside of loops). The compiler does not care but it's easier on the people debugging your code (and yourself).
nurulshidanoni -1 Posting Whiz in Training
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
int examcode[11];
{
struct student
{
string studentid;
int examcode[11];
};
ifstream stream1("Standrews83.txt");
char a[611];
if(!stream1)
{
cout << "While opening a file an error is encountered" << endl;
}
else
{
cout << "File is successfully opened" << endl;
}
vector <student> students;
student aStudent;
while (stream1 >> aStudent.studentid) //part 1
{
for (int i = 0; i < 10; i++)
{
stream1 >> aStudent.studentid; //student id. 1st piece of data
stream1 >> aStudent.examcode[i]; //exam code. 2nd piece of data
stream1 >> aStudent.studentid; //student id. 3nd piece of data
}
stream1 >> aStudent.examcode[10]; //part 3
students.push_back (aStudent);
}
for (int i = 0; i < students.size (); i++)
{
cout << students.at (i).studentid << endl;
for (int j = 0; j < 11; j++)
cout << students.at (i).examcode[j] << endl;
}
return 0;
}
I have change...what u told so..maybe i'm so stupid..
VernonDozier 2,218 Posting Expert Featured Poster
Get rid of the line that you put directly after
int main ()
int main ()
int examcode[11]; // delete this line.
You don't need it. You have it where you need it somewhere else. Don't do anything else besides deleting that one line. Then try to compile. Let me know if it works.
nurulshidanoni commented: very helpful person..may Allah bless you.. +1
nurulshidanoni -1 Posting Whiz in Training
i have delete but 1 error? dont what missing function header
C:\Documents and Settings\Administrator\Desktop\c++ master\16\2.cpp(10) : error C2447: missing function header (old-style formal list?)
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main();
{
struct student
{
string studentid;
int examcode[11];
};
ifstream stream1("Standrews83.txt");
char a[611];
if(!stream1)
{
cout << "While opening a file an error is encountered" << endl;
}
else
{
cout << "File is successfully opened" << endl;
}
vector <student> students;
student aStudent;
while (stream1 >> aStudent.studentid) //part 1
{
for (int i = 0; i < 10; i++)
{
stream1 >> aStudent.studentid; //student id. 1st piece of data
stream1 >> aStudent.examcode[i]; //exam code. 2nd piece of data
stream1 >> aStudent.studentid; //student id. 3nd piece of data
}
stream1 >> aStudent.examcode[10]; //part 3
students.push_back (aStudent);
}
for (int i = 0; i < students.size (); i++)
{
cout << students.at (i).studentid << endl;
for (int j = 0; j < 11; j++)
cout << students.at (i).examcode[j] << endl;
}
return 0;
}
VernonDozier 2,218 Posting Expert Featured Poster
Two issues. First issue, you have a semicolon in this line that you do not want:
int main ();
Remove that semicolon so it looks like this:
int main ()
Second issue is the location of the struct. It is normally outside of the "main" function. I didn't bring this up earlier because, while normally it is outside of main, I didn't know it HAD to be outside of main. Apparently it does. So remove these lines from where they are now:
struct student
{
string studentid;
int examcode[11];
};
and put them between the lines :
using namespace std;
and:
int main ()
The result will look like this:
using namespace std;
struct student
{
string studentid;
int examcode[11];
};
int main ()
This puts the "student" struct before the main function instead of inside it. Try to compile again after these changes.
nurulshidanoni -1 Posting Whiz in Training
can open..but the output like below...
i store the c++ file and data file in 1 folder.
But, master vernon, thank you for help.I'm very appreciated it.
File is successfully opened
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
Press any key to continue
VernonDozier 2,218 Posting Expert Featured Poster
Hmm. If you post the exact program and the exact input file, maybe I can run it on my computer and see if I get the same results. I haven't tried it yet myself but it seems like it should work. It's up to you. Sounds very similar to the problems you were having earlier in the thread.
VernonDozier 2,218 Posting Expert Featured Poster
Just noticed a problem. You changed this loop when you are reading in the data.
for (int i = 0; i < 10; i++)
{
stream1 >> aStudent.studentid; //student id. 1st piece of data
stream1 >> aStudent.examcode[i]; //exam code. 2nd piece of data
stream1 >> aStudent.studentid; //student id. 3nd piece of data
}
Delete that first line. It does not belong inside the loop twice. It should look like this:
for (int i = 0; i < 10; i++)
{
stream1 >> aStudent.examcode[i];
stream1 >> aStudent.studentid;
}
nurulshidanoni -1 Posting Whiz in Training
Successfulll read....but freak...
132
7
29
69
75
96
104
111
132
7
29
s580
70
75
95
103
111
132
6
29
69
74
104
s581
110
132
134
6
29
70
74
95
103
110
132
s583
7
29
70
75
104
111
132
134
6
29
69
s584
74
100
104
110
137
7
49
69
75
95
104
s586
111
132
6
29
69
74
95
104
110
134
6
s587
29
69
74
104
110
132
134
6
29
69
74
s588
104
110
130
132
2
6
29
69
74
103
110
s590
130
6
29
69
74
95
104
110
132
7
49
s591
70
75
95
103
111
137
7
29
69
75
104
s592
111
134
137
2
6
29
69
74
103
110
132
s594
6
29
69
74
103
110
130
134
2
6
29
s595
69
74
103
110
132
2
6
29
70
74
103
s597
110
132
2
7
29
69
75
104
111
134
2
s598
6
49
69
74
103
110
137
6
29
69
74
s599
104
110
132
134
6
49
70
74
100
103
110
s601
132
6
49
74
104
110
130
134
137
7
29
s602
68
75
104
111
130
134
6
49
69
74
104
s603
110
130
134
1
6
29
68
74
104
110
134
s605
6
49
69
74
104
110
132
134
2
6
29
s606
74
103
110
130
132
2
6
29
69
74
96
s608
103
110
2
6
29
70
75
95
103
110
6
s609
49
69
74
95
103
110
132
6
49
70
74
s610
100
103
110
132
6
29
69
74
95
103
110
132
2
7
29
70
75
100
104
111
103
110
Press any key to continue
nurulshidanoni -1 Posting Whiz in Training
Dear Master Vernon,
i will try continue tomorrow..baceuse i get to go back...
Hope to c u again tomorrow.
VernonDozier 2,218 Posting Expert Featured Poster
All right. It worked for me. Here is my program. Compare it to yours.
// Filename : SchoolScores.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct student
{
string studentid;
int examcode[11];
};
int main()
{
ifstream stream1 ("Standrews83.txt");
char a[611];
if(!stream1)
{
cout << "While opening a file an error is encountered" << endl;
}
else
{
cout << "File is successfully opened" << endl;
}
vector <student> students;
student aStudent;
while (stream1 >> aStudent.studentid) //part 1
{
for (int i = 0; i < 10; i++) // part 2
{
stream1 >> aStudent.examcode[i];
stream1 >> aStudent.studentid;
}
stream1 >> aStudent.examcode[10]; //part 3
students.push_back (aStudent);
}
stream1.close ();
for (int i = 0; i < students.size (); i++)
{
cout << students.at (i).studentid << endl;
for (int j = 0; j < 11; j++)
cout << students.at (i).examcode[j] << endl;
}
return 0;
}
Here is what I used for the Standrews83.txt" input file
s1 0003
s1 0013
s1 0034
s1 0054
s1 0071
s1 0081
s1 0097
s1 0105
s1 0116
s1 0135
s1 0138
s2 0003
s2 0015
s2 0036
s2 0056
s2 0071
s2 0083
s2 0097
s2 0106
s2 0118
s2 0135
s2 0138
s3 0003
s3 0010
s3 0031
s3 0051
s3 0071
s3 0078
s3 0097
s3 0105
s3 0113
s3 0135
s3 0138
Here are my results:
File is successfully opened
s1
3
13
34
54
71
81
97
105
116
135
138
s2
3
15
36
56
71
83
97
106
118
135
138
s3
3
10
31
51
71
78
97
105
113
135
138
nurulshidanoni -1 Posting Whiz in Training
Dear Master Vernon,
I have successfully opend...but i check a student 215 only took a 9 exam, and another take 8..so ...how must arrange it?
nurulshidanoni -1 Posting Whiz in Training
i save my file in 1 folder wth a data, but when i close and i compile again, it open , but only take a some data only, it make me had to copy again into a new file..so how can it happen?
VernonDozier 2,218 Posting Expert Featured Poster
O.K. So I guess that the assumption that each student has exactly 11 exam codes is not accurate? I'm assuming that the input file actually looks something like this:
s1 0003
s1 0013
s1 0054
s1 0071
s1 0081
s1 0097
s1 0105
s1 0116
s1 0135
s1 0138
s2 0003
s2 0015
s2 0071
s2 0083
s2 0097
s2 0106
s2 0118
s2 0135
s2 0138
s3 0003
s3 0010
s3 0031
s3 0051
s3 0071
s3 0078
s3 0097
Student scores are still in order and in the format above? The number of exam codes varies? Each line has the student name next to a student exam code? Student scores are not interspresed like below, correct?
s1 0005
s2 0007
s1 0019
In this example not all of student s1's exam codes are together. s2 has an exam code shoved in the middle. I am assuming that the data is not in this format but is in the previous format. Either way is doable, but if your data is in the format directly above, it is slightly more difficult. I am going to assume that the data is NOT interspersed (i.e. it looks like the top part of this post rather than looking like the format directly above). Take a good look at your data and find out for sure.
You need to change your struct from this:
struct student
{
string studentid;
int examcode[11];
};
to this:
{
string studentid;
vector <int> examcode;
};
Your display function will change from this:
for (int i = 0; i < students.size (); i++)
{
cout << students.at (i).studentid << endl;
for (int j = 0; j < 11; j++)
cout << students.at (i).examcode[j] << endl;
}
to this:
for (int i = 0; i < students.size (); i++)
{
cout << students.at (i).studentid << endl;
for (int j = 0; j < student.at (i).examcode.size (); j++)
cout << students.at (i).examcode.at (j) << endl;
}
By site rules, I'm not allowed to write all the code for you, so you need to change your input file reading block (this is what it was before):
while (stream1 >> aStudent.studentid) //part 1
{
for (int i = 0; i < 10; i++) // part 2
{
stream1 >> aStudent.examcode[i];
stream1 >> aStudent.studentid;
}
stream1 >> aStudent.examcode[10]; //part 3
students.push_back (aStudent);
}
Do not change anything else in the program! We don't want to "fix" anything that wasn't broken before. The main change that you need to make is that, one, you are now dealing with vectors rather than arrays in the examcode part so you need to learn how to deal with vectors if you do not already know how. Two, as you are reading in the data, you need to check whether you have seen this particular student before by comparing the "studentid" just read in to the previous "
studentid". If they are the same, don't do the "push_back" command on "aStudent" yet. If you have not seen this studentid before, it's a new student, so you'll do a "push_back" on aStudent. This file read loop may change dramatically from what you have and is likely to be much more complex, so throw it out and start from scratch if needed. The logic will be sort of the same, but not exactly. You'll almost certainly need to use the "compare" function from the string library. You'll likely have a push_back command of an integer into the examcode vector in addition to the push_back command of aStudent onto the students vector.
Don't change anything else from the rest of the program! Good luck.
VernonDozier 2,218 Posting Expert Featured Poster
i save my file in 1 folder wth a data, but when i close and i compile again, it open , but only take a some data only, it make me had to copy again into a new file..so how can it happen?
The data file won't open when you compile, only when you run the program. All of the files (text file, executable program file, and .cpp program file) should be in the same directory (technically that is not always required, but let's make it simple and make sure they are all in the same directory). Once you get the data file the way you want it, there is no reason to edit it. You can run the program numerous times successfully. It should read the whole file. I would suggest, before moving on, that you copy and paste the two files I posted yesterday, compile and run the program and make sure you get the exact same results that I did. If you do not, compare both the program file and the input/text file and make sure they are identical. You should get the EXACT same results as I did when you run it on your computer.
nurulshidanoni -1 Posting Whiz in Training
my data is like this for s1 has 11 exam
until s215, then for s215 there is 9 exam code and s216 has 8 exam code, but for s300 has 11 exam code..then for s400 has 8 exam code...
nurulshidanoni -1 Posting Whiz in Training
// Filename : SchoolScores.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct student
{
string studentid;
vector <int> examcode;
};
int main()
{
ifstream stream1 ("1.txt");
char a[611];
if(!stream1)
{
cout << "While opening a file an error is encountered" << endl;
}
else
{
cout << "File is successfully opened" << endl;
}
vector <student> students;
student aStudent;
while (stream1 >> aStudent.studentid) //part 1
{
for (int i = 0; i < 10; i++) // part 2
{
stream1 >> aStudent.examcode[i];
stream1 >> aStudent.studentid;
}
stream1 >> aStudent.examcode[10]; //part 3
students.push_back (aStudent);
}
stream1.close ();
for (int i = 0; i < students.size (); i++)
{
cout <<"\n"<< endl;
cout << students.at (i).studentid << endl;
for (int j = 0; j < student.at (i).examcode.size (); j++)
cout <<students.at (i).examcode.at(j)<< endl;
}
return 0;
}
Compiling...
1.cpp
C:\Documents and Settings\Administrator\Desktop\c++ master\20\1.cpp(45) : error C2039: 'at' : is not a member of 'student'
C:\Documents and Settings\Administrator\Desktop\c++ master\20\1.cpp(9) : see declaration of 'student'
C:\Documents and Settings\Administrator\Desktop\c++ master\20\1.cpp(45) : error C2228: left of '.examcode' must have class/struct/union type
C:\Documents and Settings\Administrator\Desktop\c++ master\20\1.cpp(45) : error C2228: left of '.size' must have class/struct/union type
Error executing cl.exe.
1.exe - 3 error(s), 0 warning(s)
nurulshidanoni -1 Posting Whiz in Training
i dont understand the push back ..
VernonDozier 2,218 Posting Expert Featured Poster
Yeah, you are going to get errors because you have changed the struct and the display section of the program, but you have not yet changed the part of the program that reads in the input file. That part of the program still relies on the previous struct format, and it is thus incompatible with the new struct format. So you are guaranteed to get errors now.
Question: Did you ever run the code that I posted last night along with the input file I posted? Did you get the same results? If you have not yet, please do that. Before modifying the code, that needs to compile and run flawlessly and get the same results.
When you change the struct, you must change all the code that relies on it too. In this case, the code that relies on it is the part that I did not do for you yet since the forum rules do not allow me to do so until you have made a few attempts on your own. I tried to give you some pointers on how to do it though.
In order to do it though, you must first be familiar with the vector class in C++. push_back is a function provided by the vector class. A vector is basically a dynamic array. I don't know if you are familiar with arrays, static arrays, dynamic arrays, and the differences between them. If you are not familiar with the vector class, you won't be able to program this code. If that is the case, I suggest that you find a tutorial on it. Here is a decent link to the vector class:
http://www.cplusplus.com/reference/stl/vector/
push_back is described here:
http://www.cplusplus.com/reference/stl/vector/push_back.html
If this is all new to you, then this program might be a little advanced for you at this time. Put a few hours into this, post your attempts, and if you are completely lost, let me know. Good luck.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.