Functions and structs

okay so i have this struct, a struct i have used for an array.
thanks to those that have helped me it is working.

although i run into a problem with i attempt to turn that into
a function. i run into scope limitations.


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

using namespace std;

int index;

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

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




for(index = 0; index <8; index++){
getline(inFile,menuList[index].menuItem);
cout << menuList[index].menuItem << endl;

}


system("PAUSE");
return EXIT_SUCCESS;
}

i am having trouble with the bold section of my code.
i want to make a function that calls that segment. getData()
i have tried to do this myself, and ask my teacher. but she is hard to get a hold of..


what would i do without you http://www.cplusplus.com/forum/

If you want to write a function, you need to ask yourself these questions:
What will it do?
This one's simple because you already have the code
What does it need to work?
In this case you need the file object and menuItemType array. We'll pass those as parameters.
What will it return?
Here you don't really need to return anything, so the function can be void. You could, however make it return the menuItemArray, but I'll do it the other way..

Now put it all together:
1
2
3
void getData(ifstream inFile, menuItemType menuList[]){
   //Your code goes here
}
wow, youre awesome. thanks a bunch. one question though. what would the prototype look like? since if i put it where it belongs menuItemType would not have been declared yet.

would it be void getData()?


it is also saying menuItem is undeclared this is because its outside the function.
can you help me use the scope resolution operator as i am unfamiliar with it.
Last edited on
The prototype is basically the same as the definition, just without the actual code. You need the return type, name, and parameters. That's all.

int func(int, bool);
Topic archived. No new replies allowed.