How to read a txt file into an array of structures?

I can't get my array to read my file properly. When I run the program, it doesn't read my file properly....what am I doing wrong?


#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <math.h>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

struct machine {
string name;
double cost;
int num;
};

int main() {

double userMoney;
int i; //count
int leftover;
double change;
const int size = 5;
ifstream file;
file.open("sodas.txt");

machine vending[size] = {};

file >> vending[size].name >> vending[size].cost >> vending[size].num;
//AM I DOING THIS PART RIGHT? IM READING THE FILE INTO THE ARRAY OF STRUCTURES....I THINK


// TEST IF MY FILE WAS READ INTO ARRAY
cout << vending[size].name << " " << vending[size].cost << " " << vending[size].num << endl;
//TEST IF MY FILE WAS READ INTO THE ARRAY


cout << "Please choose a drink :-)" << endl;
cout << "1: Cola" << endl;
cout << "2: Root Beer" << endl;
cout << "3: Lemon-Lime" << endl;
cout << "4: Grape Soda" << endl;
cout << "5: Cream Soda" << endl;
cout << "6: Quit :(" << endl;

int choice;
cin >> choice;

switch (choice) {
case 1: cout << "You chose Cola! Enter the amount of money inserted in dollars." << endl;
cin >> userMoney;
if (userMoney <= 1 && userMoney > 0) {
change = userMoney - vending[size].cost;
cout << "Your change is " << change << " dollars" << endl;
leftover = vending[size].num - 1;
}
else {}
break;

case 2: cout << "You chose Root Beer! Enter the amount of money inserted in dollars." << endl;
cin >> userMoney;
if (userMoney <= 1 && userMoney > 0) {
change = userMoney - vending[size].cost;
cout << "Your change is " << change << " dollars" << endl;
if (vending[size].num < 1) {
cout << "SOLD OUT :(" << endl;
}
else {
leftover = vending[size].num - 1;
}
}
else {}
break;
case 3: cout << "You chose Lemon Lime Soda! Enter the amount of money inserted in dollars." << endl;
cin >> userMoney;
if (userMoney <= 1 && userMoney > 0) {
change = userMoney - vending[size].cost;
cout << "Your change is " << change << " dollars" << endl;
if (vending[size].num < 1) {
cout << "SOLD OUT :(" << endl;
}
else {
leftover = vending[size].num - 1;
}
}
else {}
break;
case 4: cout << "You chose Grape Soda! Enter the amount of money inserted in dollars." << endl;
cin >> userMoney;
if (userMoney <= 1 && userMoney > 0) {
change = userMoney - vending[size].cost;
cout << "Your change is " << change << " dollars" << endl;
if (vending[size].num < 1) {
cout << "SOLD OUT :(" << endl;
}
else {
leftover = vending[size].num - 1;
}
}
else {}
break;
case 5: cout << "You chose Cream Soda! Enter the amount of money inserted in dollars." << endl;
cin >> userMoney;
if (userMoney <= 1 && userMoney > 0) {
change = userMoney - vending[size].cost;
cout << "Your change is " << change << " dollars" << endl; \
if (vending[size].num < 1) {
cout << "SOLD OUT :(" << endl;
}
else {
leftover = vending[size].num - 1;
}
}
else {}
break;
case 6: cout << "You have quit the vending machine :((" << endl;
system("pause");
return 0;
}

system("pause");
return 0;
}

Last edited on
it doesn't read my file properly

What is the output?
How does your input file look like?
You need to check first if the file opened correctly before you can use it.
You declared an array vending[] with dimension size. This means that the elements of this array are
vending[0], vending[1], ... vending[size-1].

Thus, when you refer to vending[size] throughout your program you are going beyond the end of the array. You are probably intending to loop through the elements of the array.

I think that you need to read up on arrays (and loops) first.

Please put your code within code tags. It is impossible to see its structure without.
Please tell me what is the output?
Last edited on
Topic archived. No new replies allowed.