Hello bethmayweather,
Last time.
PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.
Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
|
If you can not learn from this, how can I expect you to learn anything?
This is what I have now and my program doesn't run, I'm not sure where the issue is though |
Since you did not bother to explain what it is doing I can only guess at your problem.
Looking at the program you start in "main" defining the variable
string food
, but you never use it. Is there a point or reason for this variable?
After that you are usable down to the while loop, but you have see better code for this part.
The while condition is based on "fin.is_open()". You should read
http://www.cplusplus.com/reference/fstream/ifstream/is_open/ for an understanding of how this works. Even when "fin" sets the "eof" bit on the stream the condition still evaluates to true.
I do not know where you got the idea of reading the file using "getline" for every line and then using "stoi" to convert the numeric variables. I am wondering if you could find a more difficult way of doing this? Although it does work until you reach the line for "is drink". You read this as a string with the "getline" and it works because it is a string, but when yo try to convert to an int, which is wrong in the first place because "isDring" is defined as a bool, the "stoi" function fails because it can not convert a string of letters to a number. This should result in a run time error of "Invalid stoi arguement".
Maybe I should have stated this in a better way:
1 2 3 4 5 6 7 8 9 10 11 12
|
while (fin.is_open()) // <--- This does not work the way you are thinking.
{
Food f; // <--- Should use something better than just 'f'. I called it "foodItem".
getline(fin, temp);
f.name = temp;
fin >> foodItem.calories >> foodItem.sugar >> foodItem.carbs >> std::boolalpha >> foodItem.isDrink;
// <--- Clears the "\n" from the input buffer before the next "getline". A must have.
fin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
|
Line 9 will read and store four fields at one time.
As I said in the input file "True" and "False" must be in all
lower case letters for the "std::boolalpha" to work. Should you need the "std::boolalpha" for output it will take the number stored in the "bool" variable and change it to a word.
The rest of reading the file does work as is. I do personally feel the storing the three descriptions in a vector is a bit much, but it does work if that is what you want. And it is good practice.
Now that you have stored all the information from the file what are you going to do with it. The next time through the loop you will overwrite what you have just read and will have no way to use The first read. In the end the struct will only contain information from the last read.
Your instructions start with:
Struct is a means of building a more complicated type from simpler parts. Struct was
introduced in C. In C++, structs were promoted to being nearly identical to the powerful
C++ feature class. In fact, everything you learn in this project about struct can be
applied (with next to no changes) to reimplimentation using class.
Defining a struct:
struct Person
{
string fname;
string lname;
int age;
};
Then, the struct can be used like any type:
// Make one person.
Person p; // <--- But give it a better name.
// Make a vector of persons. This should be done in "main".
vector<Person> people;
Accessing members of a struct.
There is more than one syntax for accessing members of a struct. The basic form is
(assuming p as defined above):
cout << p.fname << " " << p.lname << " " << p.age << endl;
|
This is good information and good examples for constructing the struct and how to use it. But remember that they are just examples and does not mean that you have to follow them exactly. Calling the actual object that is constructed "p", or "f" as you did, is only a suggestion. It also strikes me as being lazy and unimaginative in choosing a variable name. You should strive to come up with a better name than a single letter for the variable name. It mostly benefits you in the future and the end.
You do seem to grasp accessing the member variables of the struct properly.
Once you get the read loop fixed the next while loop is another endless loop the just prints the menu and waits for you to type something in, does not matter what, and then loops back to the top. I used a do/while loop here, but a while loop can work if done correctly.
For now I would comment these lines and worry about the loop later.
1 2 3 4 5 6 7
|
//while (true)
//{
PrintMenu();
string answer;
cin >> answer;
//}
|
Hope that helps,
Andy