Inserting user-input string to an array

I'm trying to code the program that will store item data.
And I'm having problems to receive user-input string to array.
Please help. I've been searching to find a vaild solution...
Thanks in advance!


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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <iomanip>

using namespace std;

typedef struct _Item{
    char item_num[20];
    string item_des[1000];
    int item_quant;
    double item_price;
}Item;

Item pArr[100];
int num_stored = 0;

void addName(string pArr[], int& num_stored);
void printMenu();
void addItem();
void searchItem();
void listItem();
void totalValue();


int main()
{
    int n = 1;
    int choice;




    while(n){

        printMenu();

        cin >> choice;

        switch(choice){
            case 1:
                addItem();
                break;
            case 2:
                //searchItem();
                break;
            case 3:
                listItem();
                break;
            case 4:
                //totalValue();
                break;
            case 0:
                cout << "You've exited out of the program" << endl;
                n = 0;
        }
    }
}



void printMenu(){
    cout << "=============================" << endl;
    cout << "1) Add an item to memory" << endl;
    cout << "2) Search memory for an item" << endl;
    cout << "3) List what's in memory" << endl;
    cout << "4) Total value on hand" << endl;
    cout << "=============================" << endl;

    cout << "\n0) Exit Program" << endl;
    cout << "       " << num_stored << " items stored" << endl;
}

void addItem(){
    char item_num[20];
    string item_des[1000];
    int item_quant;
    double item_price;

    system("cls");

    cout << "=============================" << endl;
    cout << "     Register an Item" << endl;
    cout << "=============================" << endl;
    cout << "Item Description: ";
    getline(cin, pArr[num_stored].item_des); // this is the problem here.
    cout << "Item Number: ";
    cin >> item_num;
    cout << "Item Quantity: ";
    cin >> item_quant;
    cout << "Item Price: ";
    cin >> item_price;


    strcpy(pArr[num_stored].item_num, item_num);
    pArr[num_stored].item_quant = item_quant;
    pArr[num_stored].item_price = item_price;
    num_stored++;

    cout << "\nItem Registered" << endl;

    system("PAUSE");
    system("cls");

}


void listItem(){
    cout << "===========================================================================" << endl;
    cout << setw(5) << "Item Number" << setw(20) << "Description" << setw(20) << "Quantity" << setw(10) << "Price" << endl;
    cout << "===========================================================================" << endl;
    for (int i=0; i<num_stored; i++){
        cout << setw(5) << pArr[i].item_num << setw(20) << pArr[i].item_des << setw(20) << pArr[i].item_quant << setw(10) << pArr[i].item_price << endl;
    }
}
Last edited on
I haven't ran it, but from what I can tell, item_des, is a array, which has to be accessed via index. change that line to:

1
2
3
getline(cin, pArr[num_stored].item_des[num_stored]);
cin.ignore();
//getline leaves a newline charachter in buffer, which will skip over the next input. This should clear the buffer. 


Also you are not performing any form of validation check on the variables you are initially reading into, and are not doing anything with them later on. It would be best to just read into the final destinations right away.
Last edited on
now that I think about it. The description doesn't have to be a string array.
@InfinityCounter

Thanks for your help

I forgot to add the factor that the description can have spaces. That's why I used string array.

It works fine! Although I had to use cin.ignore() before getline()
But, it works great! Thank you so much!

Actually, another problem arouse in my code.

getline() will get the string into array.
But my listitem() function will show an address output.
if I put a pointer in front of pArr[i].item_des, it will only read the first input.

here's my code:

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
void addItem(){
    char item_num[20];
    string item_des[1000];
    int item_quant;
    double item_price;



    system("cls");

    cout << "=============================" << endl;
    cout << "     Register an Item" << endl;
    cout << "=============================" << endl;
    cout << "Item Description: ";
    cin.ignore();
    getline(cin, pArr[num_stored].item_des[num_stored]); // ??
    cout << "Item Number: ";
    cin >> item_num;
    cout << "Item Quantity: ";
    cin >> item_quant;
    cout << "Item Price: ";
    cin >> item_price;


    strcpy(pArr[num_stored].item_num, item_num);
    pArr[num_stored].item_quant = item_quant;
    pArr[num_stored].item_price = item_price;
    num_stored++;

    cout << "\nItem Registered" << endl;

    system("PAUSE");
    system("cls");

}


void listItem(){
    system("cls");

    cout << "===========================================================================" << endl;
    cout << setw(5) << "Item Number" << setw(20) << "Description" << setw(20) << "Quantity" << setw(10) << "Price" << endl;
    cout << "===========================================================================" << endl;
    for (int i=0; i<num_stored; i++){
        cout << setw(5) << pArr[i].item_num << setw(20) << *pArr[i].item_des << setw(20) << pArr[i].item_quant << setw(10) << pArr[i].item_price << endl; // here is the problem
    }

    system("PAUSE");
    system("cls");
}

I have figured that
getline(cin, pArr[num_stored].item_des[num_stored];
isn't storing the user-input string after pArr[0] .

I'm not sure why.

Any helps?
Last edited on
Ha ha

I figured it out. InfinityCounter was right. Somehow I thought I had to use string array to get the code going but I was entirely wrong.

I felt so stupid after correcting the code.

The problem is solved by just deleting the string array format.

Thanks!
Topic archived. No new replies allowed.