5 Replies - 310 Views - Last Post: 29 July 2014 - 06:58 PM Rate Topic: -----

#1 a01093  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-July 14

help for fstream and getline

Posted 29 July 2014 - 01:18 PM

I don't know what is the problems and how to debug them.
Can anyone help me??
Thanks!!

file.txt:(just show few lines)
Product ID Name Type Price
UK24960 Cookies Food 131.45
JP28911 Roasted Duck Food 231.64
JP15519 Garden Biscuit Food 45.96
FR22679 Clark Leather Shoes Shoe 2606.8
AU12004 Bed Furniture 377.19
FR13873 DOUBLE BED Furniture 644.82
FR30360 2-seat Sofa Furniture 988.43

void loadFile(string filename){  << error C2562: 'loadFile' : 'void' function returning a value
	char id[20];
	char name[40];
	char type[20];
	double price[20];

	ifstream file(filename, ios::in);  << error C2065: 'file' : undeclared identifier
	cout << "Loading the product list file..." << endl;
	if(file.is_open()){		<< error C2228: left of '.is_open' must have class/struct/union
							<< 1>          type is ''unknown-type''
		cout << "File is opened." << endl;
		while(file.eof){	<< error C2228: left of '.eof' must have class/struct/union
							<< 1>          type is ''unknown-type''
							<< fatal error C1903: unable to recover from previous error(s); stopping compilation
				file.getline(id, 50, '\t');
				file.getline(name, 50, '\t');
				file.getline(type, 50, '\t');
				file.getline(price, 50, '\t');
				cout << endl;
			}
		main_menu();
	}
	else{
		cout << "Failed to open file." << endl;
		return 1;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: help for fstream and getline

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9179
  • View blog
  • Posts: 34,468
  • Joined: 12-June 08

Re: help for fstream and getline

Posted 29 July 2014 - 01:28 PM

Care to elaborate on any error messages you may be receiving? Lines it is happening on? What is the code doing? what should it be doing?
Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1342
  • View blog
  • Posts: 4,600
  • Joined: 19-February 09

Re: help for fstream and getline

Posted 29 July 2014 - 02:04 PM

Hi, welcome to DIC.

Your first error is because a void function does not return a value, you are trying to return a value near the end of the function with :

25      return 1;



file being an undeclared identifier might be caused by not using namespace std.

Do you have using namespace std; statement at the head of your program?


This line does not look right :

21     main_menu();



I take it that main_menu is the calling function, for best practice, you should allow the loadFile function to finish and return to the calling function naturally. The reason for this is a part of the program memory called the stack filling up (you don't need to worry about this just now).

There are also some problems with your data variables/arrays and also the getlines, which can be looked at later.
Was This Post Helpful? 0
  • +
  • -

#4 a01093  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-July 14

Re: help for fstream and getline

Posted 29 July 2014 - 06:38 PM

Thanks.
I am using "uisng namespace std;"

void loadFile(string filename){  
	char id[20];
	char name[40];
	char type[20];
	double price[20];

	ifstream file(filename); 
	cout << "Loading the product list file..." << endl;
	if(file.is_open()){	
		cout << "The file is opened." endl;
		while(file.eof){	
				file.getline(id, 50, '\t');
				file.getline(name, 50, '\t');
				file.getline(type, 50, '\t');
				file.getline(price, 50, '\t');
				cout << endl;
			}
		return main_menu();
	}
	else{
		cout << "Failed to open file." << endl;
		exit(1);
	}
}


error:
1>------ Build started: Project: Assi_2, Configuration: Debug Win32 ------
1> eShop.cpp
1>g:\assi_2\assi_2\eshop.cpp(180): error C2065: 'file' : undeclared identifier
1>g:\assi_2\assi_2\eshop.cpp(180): error C2228: left of '.is_open' must have class/struct/union
1> type is ''unknown-type''
1>g:\assi_2\assi_2\eshop.cpp(203): error C2039: 'is_good' : is not a member of 'std::basic_ifstream<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1>g:\assi_2\assi_2\eshop.cpp(203): fatal error C1903: unable to recover from previous error(s); stopping compilation
Was This Post Helpful? 0
  • +
  • -

#5 a01093  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-July 14

Re: help for fstream and getline

Posted 29 July 2014 - 06:46 PM

but how to use getline(), to search a field(ID/type], then display the whole line or related productName?
I am now using class and if...else to do it
I don't know is it really correct

string search_id(string product_id){
		if (product_id == "AU07439")
			return "4-seat Sofa";
		else if (product_id == "AU11644")
			return "Salomon X2 Pro";
		else if (product_id == "AU12004")
			return "Single Bed";
		else return ""; 
}

Was This Post Helpful? 0
  • +
  • -

#6 a01093  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-July 14

Re: help for fstream and getline

Posted 29 July 2014 - 06:58 PM

Thank you for your help
when I finish the whole program
I would unload the whole file to here
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1