Char arrays

hey! So when i run my program, i get a segmentation fault, this is the part of my code that gives me the segmentation fault and i was wondering why

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

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
const int SIZE=1000;
void addphone(char **, int *, double *, int &);
int nextIndex(char **, int);
int find(char **, int , string);
void update(char **, int *, double *, int);
void display(char **, int *, double *, int);
void print(char **, int *, double *, int);
int main(){
        
        string productName[SIZE];
        char **id[SIZE][5];
        double price[SIZE]={0};
        int quantity[SIZE]={0};
        int total=0;
        int choice;
        bool exit=true;
                                                
        while(exit){
                cout<< "1. Add Item" << endl << "2. Update Item" << endl << "3. Display Item"
                    << endl << "4. Print Database" << endl << "5. Exit Program" << endl;
                
                cout << "Select an option from the menu:" << endl;
                
                cin >> choice;
                
                
                switch(choice) {
                case 1: 
                        addphone(id[SIZE][5],quantity,price,total);
                        break;
                case 2: 
                        update(id[SIZE][5], quantity,price,total);
                        break;
                case 3: 
                        display(id[SIZE][5],quantity,price,total);
                        break;
                case 4: 
                               case 5:
                        exit=false;
                        break;
                default:
                        cout << "Invalid Choice" << endl;
                }
        }

}

void addphone(char **id, int *quantity, double *price, int &total)
{

        total=total+1;
        char tempID[5] ;
        double amount;
        int qnty , i, x;

        cout << "Enter the Product ID: ";
        cin>> tempID;
        x=find(id,total,tempID);                                                
                if(x==-1){
                        i=nextIndex(id,total);                          
                        id[i]=tempID;
                                    
                        cout<< "Enter the price of the phone: ";        
                        cin>> amount;                                                           
                        *(price+i)=amount;


                        cout<< "Enter the quantity on hand: ";
                        cin>> qnty;
   
                        *(quantity+i)=qnty;
                                                                        
                        cout<< endl << endl;
                }
                else
                       cout << "This ID is already in the Database" << endl ;
}
 
A lot of things regarding array usage don't seem quite right.

It might be simpler to use std::string rather than char arrays - that avoids issues with buffer overflow etc. as well as simplifying the code.

Note the necessity for strcpy() rather than just =.

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
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>

using namespace std;

const int SIZE = 1000;

void addphone(char [][5], string *, int *, double *, int &);
//int  nextIndex(char **, int);
//int  find(char **, int , string);
//void update(char **, int *, double *, int);
//void display(char **, int *, double *, int);

void print(char [][5], string *, int *, double *, int);

int main()
{
    string productName[SIZE];
    char id[SIZE][5];
    double price[SIZE] = {0};
    int quantity[SIZE] = {0};
    int total = 0;
    int choice;
    bool exit = true;

    while(exit)
    {
        cout << "1. Add Item"       << endl
             << "2. Update Item"    << endl
             << "3. Display Item"   << endl
             << "4. Print Database" << endl
             << "5. Exit Program"   << endl;

        cout << "Select an option from the menu:" << endl;

        cin >> choice;

        switch(choice)
        {
            case 1:
                addphone(id, productName, quantity, price, total);
                break;

            case 2:
            case 3:
            case 4:
                print(id, productName, quantity, price, total);
                break;

            case 5:
                exit = false;
                break;

            default:
                cout << "Invalid Choice" << endl;
        }
    }

}



void addphone(char id[][5], string *productName,  int *quantity, double *price, int &total)
{
    char tempID[5];
    cout << "Enter the Product ID: ";
    cin  >> tempID;

//    int x = find(id,total,tempID);
    int x = -1;

    if (x == -1)
    {
        int i = total;
        total++;

        strcpy(id[i], tempID);

        cout << "Enter the name of the product: ";
        cin  >> productName[i];

        cout << "Enter the price of the phone: ";
        cin  >> price[i];


        cout<< "Enter the quantity on hand: ";
        cin >> quantity[i];


        cout << endl << endl;
    }
    else
       cout << "This ID is already in the Database" << endl ;
}

void print(char id[][5], string *productName, int *quantity, double *price, int total)
{
    cout << fixed << setprecision(2);

    for (int i=0; i<total; ++i)
    {
        cout << setw(6)  << id[i]
             << setw(20) << productName[i]
             << setw(6)  << quantity[i]
             << setw(10) << price[i]
             << endl;
    }
}


I suppose you could have declared this:
 
    char id[SIZE][5];
as
 
    char *id[SIZE];
but then you have an array full of pointers which are uninitialised and need to be allocated with the new[] operator - but it's a lot of trouble and simpler to just use std::string in the first place.
As Chervil says, almost certainly because you're using a bad pointer.

First, you need to learn how to use a debugger; it will tell you the exact line that causes the segFault and the exact value of the bad pointer at that moment. Spending twenty minutes learning to use a debugger is such a useful skill that you should do it now, before you type another line of code.

As Chervil also says, you should also use strings instead of char pointers. I'll go further and say you shouldn't be using pointers at all, and you shouldn't be using arrays at all. There's nothing in this code that requires the use of pointers or arrays; these are more advanced topics, useful in particular circumstances, that often cause trouble for beginners. You're making it much harder for yourself. Do yourself a favour; write simple code that's easy to follow and less likely to be buggy.
My homework assignment requires us to use c-string for one of our arrays and pointers as well. I will try the strcpy function though
Topic archived. No new replies allowed.