Help with the code.

Hi,

Im writing a code to read data from a file and store them into individual objects of a class.

//Bikes.Txt
R15$80000$Yamaha$25$18$150
CBR250$200000$Honda$44$27$250

Thats the example of the Bikes.txt File , it will be like that.
I am getting and error in getline(); so can some help me out with this

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
#include<iostream.h>
#include<fstream.h>
int size=2;
class Listing
{
public: char name;
	int price;
	char brand;
	int displacement;
	int power;
	int category;
	listing()
		 {
			price=0;
			brand=0;
			displacement=0;
			power=0;
			category=0;
		 }
};
int main()
{
	int i;
	Listing bikes[10];
	char input[10];
	fstream datafile;
	datafile.open("Bikes.txt",ios::in);
	if(datafile.is_open())
	{
	for(i=0;i<size;i++)
	{
		if(datafile.good())
		{
			getline(datafile,input[i],"$");
			bikes[i].name=input[i];
			getline<datafile,input[i],"$">;
			bikes[i].price=input[i];
			getline<datafile,input[i],"$">;
			bikes[i].brand = input[i];
			getline<datafile,input[i],"$">;
			bikes[i].displacement = input[i];
			getline<datafile,input[i],"$">;
			bikes[i].power = input[i];
			getline<datafile,input[i],"$">;
			bikes[i].category = input[i];

	cout<<bikes[i].name<<endl<<bikes[i].price<<endl<<bikes[i].brand<<endl<<bikes[i].displacement<<endl<<bikes[i].power<<endl<<bikes[i].category<<endl;
	}
}
}


1
2
3
4
class Listing
{
public: char name;	// should be a string, not a single character
	int price;
Ok , Thanks! I will Change that one , So even Data member " Brand " will also be a string then , Right? But i still dont know whats wrong with the getline , Someone help me out with that.
What kind of problems are you having with getline? Compile problems? Not executing as you expect? Or?

The code you posted won't even compile. Lines 36, 38, 40,42,44 you need parens not angle brackets.

Line 30: Why are you using a for loop with a limit of 2 here? You really want to read until eof or you reach the maximum occurrances of bikes.

lines 35-45: getline expects a string as the second argument, not a char.
Why are you indexing into input?

Im having compile problems with Getline() , I redeclared name and brand and Input to String
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
#include<iostream.h>
#include<fstream.h>
#include<string.h>
int size=2;
class Listing
{
public: 
        string name;
	int price;
	string brand;
	int displacement;
	int power;
	int category;
	Listing()
		 {
			price=0;
			brand=0;
			displacement=0;
			power=0;
			category=0;
		 }
};
int main()
{
	int i;
	Listing bikes[10];
	string input;
	fstream datafile;
	datafile.open("Bikes.txt",ios::in);
	if(datafile.is_open())
	{
	for(i=0;i<size;i++)
	{
		if(datafile.good())
		{
			getline(datafile,input,"$");
			bikes[i].name=input;
			getline<datafile,input,"$">;
			bikes[i].price=input;
			getline<datafile,input,"$">;
			bikes[i].brand = input;
			getline<datafile,input,"$">;
			bikes[i].displacement = input;
			getline<datafile,input,"$">;
			bikes[i].power = input;
			getline<datafile,input,"$">;
			bikes[i].category = input;

	cout<<bikes[i].name<<endl<<bikes[i].price<<endl<<bikes[i].brand<<endl<<bikes[i].displacement<<endl<<bikes[i].power<<endl<<bikes[i].category<<endl;
	}
}
}


Even then im getting the error that , getline function must have function prototype....

Line 30: I am just using 2 as a example because bikes.txt which i wrote above only had 2 bikes in it so only.

Lines 35 - 45 : I reliazed that and i corrected it , Yet i have the same error.

Abstractionanon , Did i declare my string properly because im using a DOSbox C++ complier which is showing an error that , Typename expected error. Is there something wrong with the declaration?

And Even Is_open() has an error that , it doesnt belong to fstream.
Is this a compiler problem? Should i change to any Linux Distro with C++ essentials?
Last edited on
1
2
3
4
5
6
7
8
9
10
getline<datafile,input,"$">;
			bikes[i].price=input;
			getline<datafile,input,"$">;
			bikes[i].brand = input;
			getline<datafile,input,"$">;
			bikes[i].displacement = input;
			getline<datafile,input,"$">;
			bikes[i].power = input;
			getline<datafile,input,"$">;
			bikes[i].category = input;


I think problem is here you are using <> instead of brackets().

anmol2701 , I have changed it to Brackets , and im getting an error in the getline function saying that

"no matching function for call to ‘getline(std::ifstream&, std::string&, const char [2])"

Im not understanding what im doing wrong , because im giving the correct arguements yet im getting the error.
Try using ifstream datafile; instead of fstream datafile;
I did change it ,The Same Error.

no matchin function call to getline.....
The third argument to getline() should be a single char, not a string.
Topic archived. No new replies allowed.