May 5, 2010 at 10:36pm UTC
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.
May 5, 2010 at 10:51pm UTC
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 May 5, 2010 at 10:53pm UTC
May 5, 2010 at 10:54pm UTC
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 May 5, 2010 at 11:01pm UTC
May 5, 2010 at 11:32pm UTC
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.
May 5, 2010 at 11:42pm UTC
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
May 5, 2010 at 11:47pm UTC
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.
May 5, 2010 at 11:49pm UTC
That was actually correct what you had earlier, regarding those lines. The error was in the line with your cout statement.
-Albatross
May 5, 2010 at 11:56pm UTC
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?
May 6, 2010 at 12:15am UTC
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 May 6, 2010 at 12:56am UTC