struct Kingdom {
std::string m_name;
int m_population;
};
Using a std::string would be safer in most instances. Also if you used a std::vector instead of the array you could pass by reference and not need the count parameter.
By the way those error messages are not the complete compiler error messages. The compiler error messages will give much more information, such as the line number where the error was detected.
Also your function call on line 36 is also incorrect. If you want to print a single Kingdom you need to pass a single Kingdom not the array.
The reason I changed it was because when I had it this way, as you just replied, it was throwing garbage out at me. Now having it as you put it, the Call Stack says
[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll] Annotated Frame
It seems to be passing an array that points(?) to garbage..
Sorry for the bother, but Im just learning C++ and Ive been at this particular issue for the past 3 hours
The exception is probably happening because you're trying to delete your memory twice.
Also the "garbage" display is not being caused by the display functions, but is probably being caused by "bad data". I really don't know what you're trying to do in the following snippet, but I suspect that whatever you're doing is not correct.
#ifndef KINGDOM_H
#define KINGDOM_H
#include <cstdlib>
namespace sict
{
struct Kingdom {
char m_name[32];
int m_population;
};
void display(const Kingdom& pKingdom);
void display(const Kingdom kingdoms[], size_t count);
}
#endif
#include <iostream>
//#include "Kingdom.h"
usingnamespace std;
namespace sict
{
void display(const Kingdom& pKingdom) {
cout << pKingdom.m_name << ", " << "population " << pKingdom.m_population << endl;
}
void display(const Kingdom kingdoms[], size_t count) {
cout << "Kingdoms are" << endl;
for (size_t i = 0; i < count; i++) {
display(kingdoms[i]);
//cout << i + 1 << ". " << kingdoms[i].m_name << ", population " << kingdoms[i].m_population << endl;
}
}
}
#include <iostream>
#include <cstring>
//#include "Kingdom.h"
usingnamespace std;
usingnamespace sict;
void read(Kingdom&);
int main() {
int count = 0; // the number of kingdoms in the array
Kingdom *pKingdom = nullptr;
cout << "==========\n"
<< "Input data\n"
<< "==========\n"
<< "Enter the number of Kingdoms: ";
cin >> count;
cin.ignore();
if (count < 1) return 1;
pKingdom = new Kingdom[count];
for (int i = 0; i < count; ++i) {
cout << "Kingdom #" << i + 1 << ": " << endl;
// TODO: add code to accept user input for Kingdom i
read(pKingdom[i]);
}
cout << "==========" << endl << endl;
// testing that "display(...)" works
cout << "------------------------------" << endl
<< "The 1st kingdom entered is" << endl
<< "------------------------------" << endl;
display(pKingdom[0]);
cout << "------------------------------" << endl << endl;
// expand the array of Kingdoms by 1 element
count = count + 1;
Kingdom *cpy_pKingdom = nullptr;
cpy_pKingdom = new Kingdom[count + 1];
cpy_pKingdom = pKingdom;
delete[] pKingdom;
pKingdom = cpy_pKingdom;
// add the new Kingdom
cout << "==========\n"
<< "Input data\n"
<< "==========\n";
cout << "Kingdom #" << count << ": " << endl;
read(pKingdom[count + 1]);
cout << "==========\n" << endl;
display(pKingdom, count);
cout << endl;
// delete[] pKingdom;
getchar();
return 0;
}
void read(Kingdom& pkingdom) {
cout << "Enter the name of the Kingdom: ";
cin.get(pkingdom.m_name, 32, '\n');
cin.ignore(2000, '\n');
cout << "Enter the number of people living in " << pkingdom.m_name << ": ";
cin >> pkingdom.m_population;
cin.ignore(2000, '\n');
}
I get this output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
==========
Input data
==========
Enter the number of Kingdoms: 1
Kingdom #1:
Enter the name of the Kingdom: test
Enter the number of people living in test: 4
==========
The 1st kingdom entered is
------------------------------
test, population 4
------------------------------
==========
Input data
==========
Kingdom #2:
Enter the name of the Kingdom: George
Enter the number of people living in George: 34
==========
Kingdoms are
, population 4
, population 0
I am deallocating memory to avoid memory leaks, hence the delete[]
How can I go about fixing this? I've heard std::vector is a way to go, but we havent learned that yet, and we're supposed to work with what I already have coded in.
This is so confusing, I do not understand what is going on; it is compiling and the syntax seems to be ok
I am deallocating memory to avoid memory leaks, hence the delete[]
Yes but you need to insure you only delete[] the same memory once.
How can I go about fixing this? I've heard std::vector is a way to go,
This is a shame since std::vector is the best way to fix this issue.
This is so confusing, I do not understand what is going on; it is compiling and the syntax seems to be ok
Yes dealing with dynamic memory can get very confusing, which is why std::vector is so highly recommended. However since you must deal with arrays you need to copy each element of the array to the new position, don't try to use assignment.
One thing you may want to do is to keep the capacity of the array separate from your current counter. Just use capacity to size/resize the arrays let your counter keep track of which element you are currently working with.
Here is the resizing of the array, at this point capacity is the value you entered for the number of Kingdoms, count is the number of initialized elements elements currently in the array. The count should always be less than or equal to the capacity. The capacity must be be greater than zero and less than or equal to size_t::max.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Increase the size of the array by 1.
Kingdom *cpy_kingdom;
capacity++;
cpy_kingdom = new Kingdom[capacity];
// Copy the original array to the new array.
for(size_t i = 0; i < count; ++i)
cpy_kingdom[i] = pKingdom[i];
// Delete the memory for the original array.
delete[] pKingdom;
// Now copy the pointer from the new array to the original array.
pKingdom = cpy_kingdom; // Both pointers now pointing to the same place.
Here is the code that follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// add the new Kingdom
cout << "==========\n"
<< "Input data\n"
<< "==========\n";
cout << "Kingdom #" << count << ": " << endl;
read(pKingdom[count++]); // Note the increment of count is here, but after the read().
cout << "==========\n" << endl;
display(pKingdom, count);
cout << endl;
delete[] pKingdom;