I have a problem that ı could not understand.

#include <cstdlib>
#include <iostream>
#include <stdio.h>
using namespace std;


struct record
{
char name [20];
double ave;
};

int main(void)
{
int x,y;


struct record array1[3];
struct record array2[3];
FILE *a;
a=fopen("in.txt","r");

for (int i=0;i<3;i++)
{
fscanf(a," %d",&y);

array1[i].ave=(x+y)/2.0;
cout << y <<endl;
}





system("PAUSE");
return 0;
}

Recommended Answers

First of all what are you trying to do ?Is this written by or someone else ?Just state your issue

#include <cstdlib>
#include <iostream>
#include <stdio.h>
using namespace std;


struct record //definition of a type 'record'
{
char name [20]; //declaring an array of char(a string) twenty characters long …
Jump to Post

All 2 Replies

my in.txt file
in.txt

john 20 30
mark 30 40
house 49 40

First of all what are you trying to do ?Is this written by or someone else ?Just state your issue

#include <cstdlib>
#include <iostream>
#include <stdio.h>
using namespace std;


struct record //definition of a type 'record'
{
char name [20]; //declaring an array of char(a string) twenty characters long
double ave; //and a double precision variable
};

int main(void)
{
int x,y;  //two variables of integral type


struct record array1[3];  
struct record array2[3]; //two arrays containing three objects of type 'record' (defined upper') 
FILE *a; //'a' is a file pointer 
a=fopen("in.txt","r"); //here it gets associated with "in.txt" with read acces "r"

for (int i=0;i<3;i++) //sets a loop that will execute the following statements for three times 
{
fscanf(a," %d",&y); //read a value of decimal type ('%d') from the file associated to 'a' , into the variable 'y',
		     // where 'y' is passed by reference ('&') meaning it's value will change when the function exists

array1[i].ave=(x+y)/2.0; //assigns to each 'ave' contained by each object of type 'record' from the array1 array ,
			  // the value of (x+y)/2 ('note that x is not defined which it should be')
cout << y <<endl; //print the value of 'y'
}





system("PAUSE"); //'pause' is an ms-dos command from the windows virtual dos machine 
		   just open a command prompt and type "pause /?|more" for more information
return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.