How to store string in array?

Fairly new to classes, so sorry in advance. I need to store fruit names e.g. apple & banana in an array, but I'm not sure where/how to set these strings in an array. fruitName should be the array and its initialize in constructor is "". I need to set fruit names in an array but how do I do that?

//store.h
#ifndef STORE_H
#define STORE_H
#include <iostream>
using namespace std;

class fruit
{
private:
string fruitName; //Name of Fruit
double unit; //Unit Price of Fruit
int quantity; //Quantity of Fruit
public:
fruit(); // Default Constructor
fruit(string, int, double);

void setName(string);
void setUnit(double);
void setQuantity(int);
string getName() const {return fruitName;}
double getUnit() const {return unit;}
int getQuantity() const {return quantity;}
double Total() {return unit*quantity;}
void print();
friend ostream & operator<<(ostream &, const fruit &);

~fruit();
};
***********************
//store.cpp
#include "store.h"
#include <iostream>
using namespace std;

fruit::fruit() // Initialize Values
{
fruitName = "";
unit = 0;
quantity = 0;
}

fruit::fruit(string xfruit, int xunit, double xquantity )
{
fruitName = xfruit;
unit = xunit;
quantity = xquantity;
}

void fruit::setName(string xfruit)
{
fruitName = xfruit;
}

string getName()
{
return fruitName;
}
You need to make sure you understand the difference between a fruit object i.e. an instance of the fruit class, and a string:

An array of 20 fruit objects:
fruit fruit_stall[20];

An array of 20 strings:
string fruitName[20];

Last edited on
I edited my program and got it in an array, however, I don't know how I can access it from there. The array is private and I don't know how to access each element. For each fruit, I need to set a Unit Price.

// In store.cpp file
//Store fruit type in array
const string fruit::fruitName[100] =
{"Apple", "Banana", "Grape", "Orange", "Pear", "Exit"};

//Set unit price to fruit type
void fruit::setUnit()
{
fruitName[0] = 0.99; // Trying to set Apple to 0.99
}

Last edited on
You should be doing something like this. Note that I made a few changes to avoid separate header files etc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
using namespace std;

class fruit
{
private:
    string fruitName; //Name of Fruit
    double unit; //Unit Price of Fruit
    int quantity; //Quantity of Fruit
public:
    ~fruit(){}; // <-- FIX THIS
    fruit() // Initialize Values
    {
        fruitName = "";
        unit = 0;
        quantity = 0;
    }
    
    fruit(string xfruit, int xunit, double xquantity )
    {
        fruitName = xfruit;
        unit = xunit;
        quantity = xquantity;
    }
    
    void setName(string xfruit)
    {
        fruitName = xfruit;
    }
    
    string getName()
    {
        return fruitName;
    }
};

int main()
{
    const int SIZE = 3;
    
    fruit f1("Apple", 0.99, 1);// <-- name, unit, quantity
    fruit f2("Pear", 3.56, 5);
    fruit f3("Grape", 0.07, 3);
    
    fruit fruit_stall[SIZE];
    
    fruit_stall[0] = f1;
    fruit_stall[1] = f2;
    fruit_stall[2] = f3;
    
    for(int i = 0; i < SIZE; i++)
    {
        cout << fruit_stall[i].getName() << endl;
    }
    
    return 0;
}


Note also you need methods for getUnit() and getQuantity() which return a double and int respectively
Last edited on
Topic archived. No new replies allowed.