What's wrong with this array?

Dec 13, 2008 at 4:29am
I am trying samples of my code at a time can anyone plz tell what's wrong with the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include<string>
#include <fstream>
using namespace std;
void ReadAnswers(char[], int size); 

int main ()
{
	
	ifstream infile;
	ofstream outfile;
	infile.open("InputOfAnswers.txt");  
	outfile.open("OutputOfResults.txt"); 

	
    const int size=20;
	char Correctresults[size];
	ReadAnswers(Correctresults, size);
	 infile.close();
	outfile.close();
return 0;
}
	
void ReadAnswers(char Correctresults[], int size)
{
	 for (int o=0; o<20; o++)
	 {
	 infile>>Correctresults[o];
	 outfile<<Correctresults[o];

	 }
	
}

There's an error message that says:
1
2
3
4
5
dentifier
C:\Documents and Settings\Lami\Desktop\HW4.cpp(41) : warning C4552: '>>' : operator has no effect; expected operator with side-effect
C:\Documents and Settings\Lami\Desktop\HW4.cpp(42) : error C2065: 'outfile' : undeclared identifier
C:\Documents and Settings\Lami\Desktop\HW4.cpp(42) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
Error executing cl.exe.


What's that supposed to mean? any ideas on how I could fix it?
Dec 13, 2008 at 5:30am
I think it is relating to the fact that infile and outfile are not defined within the scope of the ReadAnswers() function. Fix that and see if that was causing the other errors.
Dec 13, 2008 at 9:26am
I need urgent help!! I am still working on the problem and have found problems in reading the data for example for the following set of data which is student's ID followed by 20 answers (its not a code but rather a text file-otherwise its too long to fit the screen):

1
2
3
4
5
  2061090    B  D  A  C   C       A   B   C    C   A     B    D    D    A   B       C    A     B    D    B
  2061143    B  C  A  C    B      A   B    C   C   A     B     A    D    A   B       C    A     B    D    A
  2071233    B  C  A  C    B      B    A   A   C   A     A     A    D    A   B       C    A     A   D     D
  2081251    A  A  A  C    B      B    A   A   C   A     A     A    D    A   B       C   A     A    D     D
  2071217    B  D  A  A    C      A    B   A   C   D     B     C    D    A   D       C   C     B    D     A


I came up with the following code but i think im doing something wrong:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void ReadStudents(char array[5][], ifstream& infile)  
 { 
	 
	 for (int i =1; i<5;  i++)
	 {   int ID;
		 
		 infile>>ID>>endl;//to read the ID first 

		   for (int j=0; j<20; j++)  // this loop reads the answers of each student from the text file
		   {
			   
			   infile>>AnswersOfStudents[i][j];
	
		   }
			 
	   }
 }

how can i read only the students answers should I initialize the counter to 1 rather than zero and about the inner loop-its counter would change too right-To put it simply, How can I skip the students ID and just store the answers into a two a two dimensional array - im confused!

note: For the above problem, I just figured it out-I have to pass the infile and outfile by reference

thanx in advance
Last edited on Dec 13, 2008 at 12:45pm
Topic archived. No new replies allowed.