Assistance needed please

ojdc2004
ojdc2004 used Ask the Experts™
on
Hello,
Can someone please help me with my program, I just started my class this past week and the first requirement was to do a program with the following requirement: Create a struct that will have 2 things (Name of Snack and Price of snack), then create a one dimensional array of the data type mentioned. Input at least 8 snacks into the array. The program should just print out the name of the snack and the price, with no user input.
I know it is a poorly constructed program, but I have not had much time to study and it is due in 24hours, I would really appreciate your help.

// This program uses a struct to declare variables and an array to be displayed

#include <iostream>              // Basic input and output library
#include <string>
#include <sstream>
using namespace std;
   
// This section will provide global sturct type
struct Snacks
{
        float Name_Snack[25]; //Nema of the Snack
        float Snack_Price;   //Price of the Snack
}VendMachine;

//Void writeSnacks (char Array[]);

int main(int nArg, char* pszArgs[])
{
   
    Snacks Array[10];
   
        Array[0].Name_Snack="Snickers";
        Array[0].Snack_Price = 1.00;
        Array[1].Name_Snack = "Tootsie Roll";
        Array[1].Snack_Price = .95;
        Array[2].Name_Snack = "Milky Way";
        Array[2].Snack_Price = 1.10;
        Array[3].Name_Snack = "Skittles"
        Array[3].Snack_Price = 1.05;
        Array[4].Name_Snack = "M&M's";
        Array[4].Snack_Price = .80;
        Array[5].Name_Snack = "Lay's Potatoe Chips";
        Array[5].Snack_Price = 1.25;
        Array[6].Name_Snack = "Ruffles";
        Array[6].Snack_Price = 1.25;
        Array[7].Name_Snack = "Almond";
        Array[8].Snack_Price = 1.00;
       
        system("PAUSE");
        return 0;
}

void writeSnacks(char Array[])
{
    for(int i=0; Array[i] !='\0'; i++)
    {
        cout<< Array[i];
    }
}
       
        cout << "My snack machine contains:";
        for (it=Array.begin(); it!=Array.end(); ++it) cout <<" "<< *it;
        cout <<endl;
       
    delete [0] Array;
    return 0;
    }
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
This is a C++ program. You will get more help if you Request Attention and add the C++ programming language zone.

What OS compiler are you using?

What exactly is your question. What kind of help specifically are you requesting? Did you try to compile this?

Here's one thing that is pretty clear:
Array[0].Name_Snack is an array of floats, and yet you are trying to set it to a literal string. That does not make sense.
Array[0].Name_Snack="Snickers";

Open in new window

Commented:
Hello.
I made a few modifications to your code. It is not in any way perfect, but I was able to compile and execute it.

  • Renamed your array to Inventory (it was a bit confusing).
  • Changed the snack_name part of the structs type to String
  • Missing semicolon after "Skittles"
  • Rewrote your writeSnacks function.
#include <iostream>              // Basic input and output library
#include <string>
#include <sstream>
using namespace std;
   
// This section will provide global sturct type
struct Snacks
{
        string Name_Snack;		//Nema of the Snack
        double Snack_Price;		//Price of the Snack
}VendMachine;

// Function that prints inventory
// Takes array if Snacks and the length of array as parameter.
void writeSnacks(Snacks arg[], int length);

int main(int nArg, char* pszArgs[]) {
   
	// Inventory array
    Snacks inventory[8];
   
	// Fill inventory
    inventory[0].Name_Snack="Snickers";
    inventory[0].Snack_Price = 1.00;

    inventory[1].Name_Snack = "Tootsie Roll";
    inventory[1].Snack_Price = .95;

    inventory[2].Name_Snack = "Milky Way";
    inventory[2].Snack_Price = 1.10;

    inventory[3].Name_Snack = "Skittles";
    inventory[3].Snack_Price = 1.05;

    inventory[4].Name_Snack = "M&M's";
    inventory[4].Snack_Price = .80;

    inventory[5].Name_Snack = "Lay's Potatoe Chips";
    inventory[5].Snack_Price = 1.25;

    inventory[6].Name_Snack = "Ruffles";
    inventory[6].Snack_Price = 1.25;

    inventory[7].Name_Snack = "Almond";
    inventory[7].Snack_Price = 1.00;
   
	// Print inventory.
	writeSnacks(inventory, 8);

    system("PAUSE");
    return 0;
}

void writeSnacks(Snacks arg[], int length) {
    for(int i=0; i<length; i++) {
        cout << arg[i].Name_Snack << "\n" << arg[i].Snack_Price << "\n\n";
    }
}

Open in new window

Remember that EE policy does not permit us to write extensive code for you for assignments. As such, it is important that you submit your code and ask questions that we can assist you.

Author

Commented:
Thank you for your assistance, it gave me a better understanding on how to write arrays.  

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial