0

I'm making a delivery system like the McDelivery system using structures. I have this code to store the customer's order data and his/her info.

struct customer
{
    int contactno;
    char firstname[20], lastname[20];

    struct order
    {
        struct date
        {
            int day, month, year;
        }orderdate;

    }o;
}c;

Then I used array to store the item's info and i have this code

int spaghetti[2], fries[2], icecream[2], MonsterMcBurger[2], SuicideBurger[2], VeggieWhooper[2], GrandSlam[2],  BiscottiFrappuccino[2], CaptainCrunch[2], PizzaSub[2];

I have no problem inserting the price of each item but I'm having trouble on how you put the item's name in array. Is it possible to assign a text in array? if not what code should I use to store the name and the price of the item at the same time?

EricSchaefer
  • 25,272
  • 21
  • 67
  • 103

1 Answers1

1
#include <string.h>

int main() {
    customer cust;
    strcpy(cust.firstname, "John");
    strcpy(cust.lastname, "Smith");
}

this is a very C way of doing things though; C++ can handle this in a better way, for instance, by using std::string. so instead of char firstname[20], lastname[20];, you'd have std::string firstname, lastname;, and your assignment would look like this:

int main() {
    customer cust;
    cust.firstname = "John";
    cust.lastname = "Smith";
}

also, to answer your question, it's not possible to assign values to an array of chars in the way you want to, see here for more.

Community
  • 1
  • 1
stellarossa
  • 1,730
  • 1
  • 18
  • 29