need help.

it compiles but when i run it i get a segmentation fault.
im using unix.

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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  #include <iostream>
#include <string>
using namespace std;

class bag
{

public:
bag(){n=0; array[30];}
bool add( string item);
bool remove(string item);
int getCurrentSize(){ cout<<"size of bag"<<n<<endl;}
bool isEmpty(){if(n != 0) return false; else return true;}
void clear(){ n=0;}
int getFrequencyOf(string item);
bool contains (string item);
void display();


private:
int n;
string array[];
};

bool bag::add(string item)
{
int m=n;
while(n>=0)
{array[n]=item;
n++;
}
if(m<n)
return true;
else
return false;
}

bool bag::remove(string item)
{
int m = n;
string temp;
for (int i = 0;i<n;i++)
{
   if(item == array[i])
        { temp= array[n-1];
        array[n-1]=array[i];
        n--;
}
}
        if(m>n)
        return true;
        else
         return false;
}
int bag::getFrequencyOf(string item){
int i = 0;
int count=0;
 while ((i < n) && (item != array[i])) i++;
{       if(array[i]==item)
count++;
} return count;
}
bool bag::contains (string item){
for(int i=0;i<n;i++)
{if(item==array[i])
return true;
}}
void bag::display(){
for(int i=0; i<n; i++)
cout<<array[i]<<endl;
}



main program



#include <iostream>
#include <string>
#include "bag.h"
using namespace std;

int main() {
  // Declarations
  bag groceries;                 // default ctor
  string item;

  // test add items
  cout << "*** TEST 1 add() \n";
  groceries.add("milk");
  groceries.add("meat");
  groceries.add("carrots");
  groceries.add("fish");

  // test display()
  cout << "*** TEST 2 display()\n";
  groceries.display();

  // test remove()
  cout << "*** TEST 3 remove()\n";
  groceries.remove("carrots");

  groceries.display();

  // test clear()
  cout << "*** TEST 4 clear()\n";
  groceries.clear();

  groceries.display();

  // test isEmpty()
  cout << "*** TEST 5 isEmpty()\n";
  if (groceries.isEmpty())
    cout << "Bag is empty\n";
  else
    cout << "Bag is not empty\n";

  // TEST add() boolean return
  cout << "*** TEST 6 add() boolean return\n";
  cout << "Enter an item or 'quit': ";
  cin >> item;
  while (item != "quit") {
     if (!groceries.add(item))
        cout << "ERROR - bag is full!\n";
     cout << "Enter an item or 'quit': ";
     cin >> item;
  }

  groceries.display();

  // TEST contains()
  cout << "*** TEST 7 contains()\n";
  cout << "Enter an item to search for: ";
  cin >> item;
  if (groceries.contains(item))
    cout << item << " found\n";
  else
    cout << item << "not found\n";

  cout << "\nAdding two more of " << item << " to the list\n";
  groceries.add(item);
  groceries.add(item);

  groceries.display();

  // test of getfrequency Of()
  cout << "*** TEST 8 getFrequencyOf()\n";
  cout << item << " is in the list " << groceries.getFrequencyOf(item) << " times\n";

  cout << "\n\n****** TEST COMPLETE ******\n";

}


My compiler wrote:
1
2
3
// Line 9
// warning: statement has no effect
array[30];

This accesses the 31th element in the array. For this to work the array needs to be at least of size 31. This statement is a bit pointless because you're not using the value for anything.


My compiler wrote:
1
2
3
// Line 12
// warning: no return statement in function returning non-void
int getCurrentSize(){ cout<<"size of bag"<<n<<endl;}

You probably don't want want to use cout in this function and instead use a return statement to return the size.


My compiler wrote:
1
2
3
// Line 22
// warning: ISO C++ forbids zero-size array ‘array’
string array[];

You need to specify the size of the array between []. If you don't want a fixed size you should consider using std::vector.


My compiler wrote:
1
2
3
4
5
6
7
// Line 63
// warning: control reaches end of non-void function
bool bag::contains (string item){
for(int i=0;i<n;i++)
{if(item==array[i])
return true;
}}

You need to return a value also in the case when the item is not found.
Last edited on
Topic archived. No new replies allowed.