Skip to content

Instantly share code, notes, and snippets.

@NickersF
Created November 20, 2015 20:25
Show Gist options
  • Save NickersF/41cd101787a25795150e to your computer and use it in GitHub Desktop.
Save NickersF/41cd101787a25795150e to your computer and use it in GitHub Desktop.
CS-161 Assignment 7 - Back up 11/20/15
// Assignment #7 - Cash register
// Author: Nicholas Fazzolari
// Date 19/11/2015
// Sources:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
struct Item {
string code;
string name;
double price;
};
const int MAX_ITEMS = 100;
Item items[MAX_ITEMS];
int loadData();
string getName(string code);
double getPrice(string code);
int main() {
int retVal;
// test driver for load data - currently working
retVal = loadData();
for (int i = 0; i < retVal; i++) {
cout << items[i].code << " " << items[i].name << " " << items[i].price << " " << endl;
}
// i've tried multiple statements here which all broke my application
cout << retVal << endl;
return 0;
}
// the following function prompts the user for a file,
// if a valid file has been found the function stores the data
// in each respective element of the structs members which
// are initialized as arrays with 100 elements
int loadData() {
ifstream infile;
string input;
int itemCount = 0;
cout << "Please enter a file path/name: ";
getline(cin, input);
infile.open(input.c_str());
if (!infile.is_open()) {
cout << "Error opening file.";
return -1;
}
// read the data into struct arrays for each element
// there are three elements: code, name, price
while (getline(infile, input)) {
items[itemCount].code = input.substr(0, 5).c_str();
items[itemCount].name = input.substr(10, 21).c_str();
items[itemCount].price = atof(input.substr(36, 5).c_str());
itemCount++;
}
return itemCount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment