File reading

Hi everyone. I created a program in which there is this code:
1
2
3
4
5
ifstream name;
name.open(nameFileName);
getline(name, name);
MyRestaurant.setName(name);
name.close();

(MyRestaurant is a class name)
but i am getting many errors, bringing me back to these lines. Is there a problem with them?
getline(name, name);

What type is name? And the other name? What type is that? Are those meant to be two different things? How is the compiler going to know which one you mean?
Last edited on
It looks like your getline is causing the problem. The first parameter should be the name of the file, and the second parameter should be the name of the string that you want to insert the line into.

1
2
3
    string Text;
    ifstream File(namefileName);
    getline(File,Text);


You are also sending the file into your setName function when you should probably be sending the string instead, make sure you don't call them the same thing.
Right, that makes sense. Thanks guys! I'll try it out and see if it works.
It resolved part of the problem, but now i am getting an error message that says that the getline is supposed to have three arguments. Can someone tell me what the third is?
I guess you have an old compiler or something, getline() is overloaded to be used just as you have done.

The third argument is a delimiting character, so for all intents and purposes, this aught to work for you:
getline(inFile, nextLine, '\n');
Thanks, but now that I put three arguments, it says it only expected 2. So I deleted the last one again, and it said it expected 3. If this is any help for finding my error here is a bigger chunk of my 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
34
35
36
37
38
39
40
41
42
43
Restaurant MyRestaurant;
string nameFileName = which + "name.txt";
string moneyFileName = which + "money.txt";
string numWaiterFileName = which + "numWaiter.txt";
string qualWaiterFileName = which + "qualWaiter.txt";
string numTablesFileName = which + "numTables.txt";
string foodQualFileName = which + "foodQual.txt";
string name_;
int money_;
int numWaiters_;
int qualWaiters_;
int numTables_;
int foodQual_;
ifstream name;
name.open(nameFileName);
getline(name, name_, '\n');
MyRestaurant.setName(name_);
name.close();
ifstream money;
money.open(moneyFileName);
getline(money, money_);
MyRestaurant.setMoney(money_);
money.close();
ifstream numWaiters;
numWaiters.open(numWaiterFileName);
getline(numWaiters, numWaiters_);
MyRestaurant.setNumWaiters(numWaiters_);
numWaiters.close();
ifstream qualWaiters;
qualWaiters.open(qualWaiterFileName);
getline(qualWaiters, qualWaiters_);
MyRestaurant.setQualWaiters(qualWaiters_);
qualWaiters.close();
ifstream numTables;
numTables.open(numTablesFileName);
getline(numTables, numTables_);
MyRestaurant.setNumTables(numTables_);
numTables.close();
ifstream foodQual;
foodQual.open(foodQualFileName);
getline(foodQual, foodQual_);
MyRestaurant.setFoodQual(foodQual_);
foodQual.close();
1
2
int money_;
getline(money, money_);


getline() only works with strings.
Finally! Thanks a lot LowestOne! It works!
Topic archived. No new replies allowed.