homework...

i am baffled i dont know how this can happen. it wont let me use the << operator.


#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
struct menuItemType{//struct
string menuItem;
int price;
};
int index;
menuItemType menuList[8];

ifstream inFile;
inFile.open("menu_data");

for(index = 0; index <8; index++)
inFile >> menuList[index].menuItem
>> menuList[index].price;

cout << menuList[0];

system("PAUSE");
return EXIT_SUCCESS;
}

everything works except for the cout statment.

any help will be appreciated.
You can't just output a struct through cout. You need to do something like menuList[0].menuItem. But you knew that.

EDIT: It's funny how you got it right the first few times, but not when you were outputting it...

-Albatross
Last edited on
heh i can see how that would be funny. so all i have to do is menuList[0].menuItem and it will print to the screen? or am i missing something.

how can i output to the screen?
Last edited on
One way is to model it exactly after your inFile >> line.

Or, a better way is to write operator<< for your struct and then leave the line as is.

1
2
3
4
5
struct menuItemType {
    friend std::ostream& operator<<( std::ostream& os, const menuItemType& m ) {
        return os << m.menuItem << ' ' << m.price;
    }
};

geeze; i really dont understand. i feel pretty stupid. my teacher hasnt told us about those statements or keywords. looks like jibberish.

what do you mean by modeling?

ugh sorry for this. i am just really hung up on this.
jsmith's solution is better by a long shot, but we want you to understand before you try the better solutions. You have to learn how to use a bubble sort before you can use a smoothsort.

You can't output the whole struct; you can only output one element of it using the extraction operator (<<) as it is. I don't recommend modifying it just yet, which is jsmith's solution. Instead, get that one element you want to output using the . operator to put on the right side of the << operator.

-Albatross
like this?

for(index = 0; index <8; index++)
inFile >> menuList[index].menuItem
>> menuList[index].price;
<< menuList[0].menuItem;

sorry if i am a bother. ive just been drilling into this book and to no avail.

thanks for you kind helping hand.

That was actually correct what you had earlier, regarding those lines. The error was in the line with your cout statement.

-Albatross
okay well i got the program to load.
using cout << menuList[0].menuItem;

only thing is that it doesnt print anything.

is this because the file data is not being read in?
Perhaps menu_data doesn't exist. You should make a statement that checks this.

http://cplusplus.com/reference/iostream/ifstream/

-Albatross
i tried this

for(index = 0; index <8; index++)
inFile >> menuList[index].menuItem
>> menuList[index].price;
cout << menuList[index].menuItem;

i thought it would work.
it does print to the screen, but its in some archaic characters. and says that the drivers are not working.


--okay im not getting those archaic looking characters anymore
instead i am getting a bunch of nonsense numbers istead of the menu items..
Last edited on
Topic archived. No new replies allowed.