0

I'm trying to be able to call the vendingmachine function and have it give me a table of the stuff i got from the file but it gives me crazy numbers like -858993460. I have to be able to change the individual amounts and prices before calling the function so it can give me different numbers.

Cola 0.75 20 Ruby Red Blast 1.00 10 Lemon Fizz 0.75 8 Grape Soda 0.90 5 Citrus Flip 0.85 0 Habanero Surprise 0.80 11 ^^This is the text file im working with

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

using namespace std;



struct soda{
    string name;
    double price;
    int amount;
};

void vendingmachine(struct soda[6]);

int main(){
ifstream inFile;


soda soda[6];


inFile.open ("machine.txt");
if ( inFile.fail() )
{
    cout << "Error: Data file could not be opened" << endl;
    exit (EXIT_FAILURE);
}

for(int i=0; i < 6; i++){
    getline(inFile, soda[i].name);
        inFile >> soda[i].price;
        inFile >> soda[i].amount;
        inFile.ignore(100,'\n');
}


    cout << "Welcome to the vending machine!" << endl;
    cout << endl;



     vendingmachine(soda);

return 0;
}

void vendingmachine(struct soda soda[6]){



    cout << "***************************************************************" << endl;
    cout << "        " << "Drink Name" << "      " << "Price Per Can" << "         " << "Number in Machine" << endl;

    for( int i=0; i < 6; i++){

        cout << setw(17) << soda[i].name << setw(16) << soda[i].price << setw(20) << soda[i].amount << endl;
    }
     cout << "***************************************************************" << endl;

}

Thanks everyone, i changed it to how it should be.

2 Answers 2

3

You are declaring a local variable soda and operating on that in the function

void vendingmachine(struct soda[6]){
    soda soda[6];

You need to operate on the caller's array instead

void vendingmachine(struct soda soda[6]){
    //soda soda[6];
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the responses guys I'm still confused on what to do though.
@user3052728 Change your function in the style shown in the second code snippet of my answer. i.e. give the argument to vendingmachine a name and remove the local array declared in the first line of the function.
0

Change your function to this:

void vendingmachine(struct soda soda[6]){

cout << "***************************************************************" << endl;
cout << "        " << "Drink Name" << "      " << "Price Per Can" << "         " << "Number in Machine" << endl;

for( int i=0; i < 6; i++){

    cout << setw(17) << soda[i].name << setw(16) << soda[i].price << setw(20) << soda[i].amount << endl;
}
 cout << "***************************************************************" << endl;

}

THEN!

Go to your loop where you are receiving the data from the text file and type this:

cout << soda[i].name << endl;

(now run the program)

You will notice that the whole first line is attributed to the Cola then there are 5 blank lines that are printed.

Now all you need to do is make sure the variables from the text file are correctly being put into your struct and you will soon be finished.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.