error reading datas from text files

a have a text file which include a few lines and one of the line is int.. while i tried to get the data from that text file i'm receiving errors.I'm a beginner to c++.

#include <iostream>
#include <fstream>
using namespace std;

const int MAX = 80;
const int MAXNO = 10;

struct Date
{
int day;
char month [MAX];
int year;
};

struct Student
{
char name [MAX];
char nationality [MAX];
// Date birthDate;
int no;
char wishMessage [MAXNO] [MAX];
};

void myInfo (fstream&, char[], Student&);
int main()
{
srand(time (NULL));
fstream afile;
char filename [MAX];

cout << "Enter the file name: ";
cin >> filename;
Student a;
myInfo(afile,filename,a);

}
void myInfo(fstream& afile, char filename [], Student& a)
{
afile.open(filename, ios::in);
if (!afile)
{
cout << "file opened for reading failed" << endl;
exit (-1);
}
afile.getline(a.name, MAX);
afile.getline(a.nationality, MAX);
afile.clear ();
afile.ignore(100, '\n');
afile >> a.no;
afile.clear ();
afile.ignore(100, '\n');
for(int i=1; i<MAXNO; i++){
afile.getline(a.wishMessage, MAXNO, MAX);
afile.getline(a.wishMessage, MAXNO, MAX);
afile.getline(a.wishMessage, MAXNO, MAX);
afile.getline(a.wishMessage, MAXNO, MAX);
afile.getline(a.wishMessage, MAXNO, MAX);
afile.getline(a.wishMessage, MAXNO, MAX);
afile.getline(a.wishMessage, MAXNO, MAX);
}
afile.close ();
}
Use [ code ] [ / code ] tags, and post the errors you're receiving.
1
2
3
4
Checking dependecies
"C:\Program Files(x86)\quincy\mingw\bin\gcc.exe"-fno-rtti -fno-exception -std=c++98-pedantic-errors -Wno-long-long -Wno-write-strings -gstabs -l "C:\Program Files(x86)\quincy\mingw\include\\etc....
lab_1.cpp in function 'void myinfo(std:fstream&,char*,Student&)':
lab_1.cpp: 19: error: no matching function for call to 'std:basic_fstream<char, std:char_traits<char> >> ::getline(char[10][80], const int&, const int&)';  


still got alot more
This looks overly complicated for a program that only needs to read in data from a text file. You need to start off simple, read in a single line from the file and print it to the screen to ensure you're getting input from the text file properly.

After that, build your way up to reading everything in from the text file. It's generally a bad idea to program in a bunch of code, and then fix all 27 errors at once. It's far easier to fix 1 error at a time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream fin;
    fin.open ("Some File.txt");
    // can also add an if statement here: if the file isnt open, return 1;
    string szFileInput;

    getline (fin, szFileInput, '\n');
    cout << szFileInput << endl;

    cin.get();
    return 0;
}


You also need to use [ code ] [ / code ] tags so your code is readable.
Last edited on
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <fstream>
using namespace std;

const int MAX = 80;
const int MAXNO = 10;

struct Date
{
	int day;
	char month [MAX];
	int year;
};

struct Student
{
	char name [MAX];
	char nationality [MAX];
	Date birthDate;	   
	int no;
	char wishMessage [MAXNO] [MAX];
};

void myInfo (fstream&, char[], Student&, Date&);
void printmyInfo(fstream&, char[], Student&, Date&);

int main()
{
		srand(time (NULL));
		fstream afile;
		char filename [MAX];
		cout << "Enter your info file name: ";
		cin >> filename;
		Student a;
		Date b;
		myInfo(afile,filename,a,b);
		printmyInfo(afile,filename,a,b);
		
}



void myInfo(fstream& afile, char filename [], Student& a, Date& b)
{
	afile.open(filename, ios::in);
	if (!afile)
	{
		cout << "file opened for reading failed" << endl;
		exit (-1);
	}
	afile.getline(a.name, MAX);
	afile.getline(a.nationality, MAX);   
	afile >> b.day;
	afile >> b.month;
	afile >> b.year;
	switch(b.month[])
	{
	case 'May': 
	strcpy(b.month,"05");
	break;
	default :
	strcpy(b.month,"10");
	break;
	}
	afile >> a.no;
	afile.clear ();
	afile.ignore (100, '\n');
	int MAXNO=1;
	while(MAXNO<8)
	{  
	afile.getline(a.wishMessage[MAXNO], MAX);
	MAXNO++;
	}
	afile.close ();
}

void printmyInfo(fstream& afile, char filename [], Student& a, Date& b)
{
	cout << "My name is " << a.name << endl;
	cout << "My nationality is " << a.nationality << endl;
	cout << "MY date of birth is " << b.day << " - " << b.month << " - " << b.year << endl;
	cout << "I have " << a.no << " wishes:" << endl;
	for(int MAXNO=1;MAXNO<8;MAXNO++)
	{
	cout << " " << MAXNO << ": " << a.wishMessage[MAXNO] << endl;
	}
}




ok now i can run everything but when reach to wishmessage part it'll only display first,third,fifth and seventh messages. it'll skip 2nd,fourth and 6th. can some1 enlighten me?
Last edited on
That's because you're telling your program to skip lines, right here, in line 57:

afile.ignore(50, '\n');
Hi Phil123 i managed to solve everything and left 1 last thing. I need to convert that 03 May 2012 (int, char, int) to 03 05 2012(int,str,int) with strcpy and i still cannot figure out how to do that. I've updated the code to latest one.
Last edited on
Do you have to specifically use strcpy? I can show you how to do it without using that...let me know.
Phil123 there is no specifically but i'm trying to use that because that's only way what i've learnt so far...
Yeah, no problem, we've all been there.

Sooo, I haven't done this before, but here's one way:

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
#include <iostream>
#include <string>
using namespace std;

const int ArSize = 12;

int main ()
{
	string szMonthList[ArSize] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

	int Day = 3;
	string szMonth = "May";
	int Year = 2012;
	int Month;

	for (int i = 0; i < ArSize; i++)
		if (szMonth == szMonthList[i])
			Month = i + 1;

	if (Day < 10)
		cout << "0";

	cout << Day << "-";
	
	if (Month < 10)
		cout << "0";

	cout << Month << "-" << Year << endl;
	cin.get();
	return 0;
}


Basically what I'm doing here is testing which month May is, and storing that value (plus one because of the way arrays are read) into int Month.

If int Day is less than 10, we need to print a 0 to the screen. Same thing with int Month.
Last edited on
Phil123.. Thank you so much for your time... i'm still beginner so there are a lot of things i still dun know and i'm still trying to learn..
PHil123 thank you but in my day structure
struct Date
{
int day;
char month [MAX];
int year;
};

i've declared month as char... so will it be ok ?
is it possible for me to replace that string array with char array? i'm not sure if it's possible .


 
char szMonthList[ArSize] = { 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' };
Last edited on
Hold on, let me see...

char MonthList[12] [20] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

1
2
//type, typename, 12 instances of 20 characters (max)
char MonthList [12] [20] = { ... };


There we go, still waking up here...hah.
Last edited on
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
void myInfo(fstream& afile, char filename [], Student& a, Date& b)
{
	afile.open(filename, ios::in);
	if (!afile)
	{
		cout << "file opened for reading failed" << endl;
		exit (-1);
	}
	afile.getline(a.name, MAX);
	afile.getline(a.nationality, MAX);   
	afile >> b.day;
	afile >> b.month;
	afile >> b.year;
	afile >> a.no;
	afile.clear ();
	afile.ignore (100, '\n');
	int MAXNO=1;
	while(MAXNO<8)
	{  
	afile.getline(a.wishMessage[MAXNO], MAX);
	MAXNO++;
	}
	afile.close ();
}

void changemonth(Date& b)
{
	string szMonthList[ArSize] = {"Jan", "Feb", "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec" };
	int day1 = b.day;
	string szMonth = b.month;
	int year1 = b.year;
	int month1;
	for(int i=0; i<ArSize; i++)
	{
	if(szMonth == szMonthList[i])
			month1 = i+1;
	if(day1 < 10)
		cout << "0";
	if(month1 < 10)
			cout << "0";
			cin.get();
			return 0;
	}
}
void printmyInfo(fstream& afile, char filename [], Student& a, Date& b)
{
	cout << "My name is " << a.name << endl;
	cout << "My nationality is " << a.nationality << endl;
	cout << "MY date of birth is " << day1 << " - " << month1 << " - " << year1 << endl;
	cout << "I have " << a.no << " wishes:" << endl;
	for(int MAXNO=1;MAXNO<8;MAXNO++)
	{
	cout << " " << MAXNO << ": " << a.wishMessage[MAXNO] << endl;
	}
}

still not working for me... i tried to change my char month[] to string month[]... now giving me a lot of error... 
Post all of the errors you're receiving (don't cut anything out) and post all of your code too, I don't see your int main () up there.
Last edited on
ha ha ok.. what time in your place? Mine is 11:15pm..... i will try your char one again.... thank you anyway
Topic archived. No new replies allowed.