Hi. I have an array of structs problem. I am having issues with my getname getprice functions. I need to be able to enter a code and return the name and price for that item. The values I return are for all items. It looks like it should be simple but none of the code I have tried has worked. Any help is appreciated thankyou. P.S I did read "read this before posting" it' my first time I apologize if it's somethings wrong.
#include<fstream>
#include<string>
#include<iostream>
#include<iomanip>
using namespace std;
struct Item {
string code;
string name;
double price;
};
int loadData (Item items[], int &count);
const int MAX_ITEMS = 100;
Item items[MAX_ITEMS];
string getName(string code);
double getPrice (string code);
void printItems (Item items);
int count = 0;
int main()
{
string code;
ifstream inputFile;
string infile;
string input;
string key;
cout << "Welcome to Angela's grocery store" <<endl;
cout << loadData(items, count );
cout << count << "Items loaded successfully." <<endl;
//while (inputFile){
cout << "Please enter code: ";
cin >> key;
getline(cin, key);
//getName (code);
getPrice (code);
//cout << "My Grocery List:" <<endl;
//for (int n = 0; n < count; n++){
// printItems (items[n]);
return 0;
}
int loadData (Item items[], int &count)
{
string input;
ifstream inputFile;
//cout << "Please enter the name of the backup file." <<endl;
//cin >> input;
//inputFile.open(input.c_str());
inputFile.open("C:\\Users\\angela\\Desktop\\cs 161\\prices.txt");
//inputFile.open(input.c_str());
if (!inputFile.is_open()) {
return -1 ;
}
while (getline(inputFile, input)) {
items[count].code = (input.substr(0,8).c_str());
items[count].name = (input.substr(10,26).c_str());
items[count].price = atof(input.substr(36,8).c_str());
count++;
}
return 0 ;
}
void printItems (Item items) //to check that my my text file loaded properly into my array
{
cout << items.code << " "
<< items.name << " "
<< items.price <<endl;
}
string getName(string code)
{
string name;
string key ;
for (int i = 0; i < count; i++) {
if (items[i].code.find(key) == 0 ) {
cout << "************" <<endl;
cout << setw(3) << right << items[i].name << endl;
}
}
return 0;
}
double getPrice (string code){
string name;
string input;
string key;
for (int i = 0; i < count; i++) {
if (items[i].code.find(key)== 0 ) {
cout << items[i].price <<endl;
}
}
return 0;
}