0

Ok so here's my code. I'm getting no errors in the compiler, however when I run this program it lets me input 2 names then crashes with a Window's Error. What in the heck am I doing wrong?!

#include <iostream>

using namespace std;

//declaration of variables

int i; // Loop Counter
int x; // Number of Family Members

int main()
{

string FamilyName[x]; // Array of Names


cout << "Enter Number of Family Members" <<endl;
cin >> x;

for (i = 0 ; i < x ; i++){
        cout << "Enter Family Member's Name: " <<endl;
        cin >> FamilyName[i];
}
for (i = 0 ; i < x ; i++){
    cout << FamilyName[i] <<endl;
}

return 0;
}
5
  • is it so hard to post up the error message? Commented Dec 9, 2014 at 3:06
  • This should ideally not compile as array size(x in the string FamilyName[x]) should be a constant expression. Which compiler are you using? Commented Dec 9, 2014 at 3:08
  • @KickButtowski "filename.exe has stopped working A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available. " Commented Dec 9, 2014 at 3:08
  • @Arun CodeBlocks 13.12 Commented Dec 9, 2014 at 3:08
  • @musical_coder Array declarations allocate space (so long as x > 0) Commented Dec 9, 2014 at 3:32

4 Answers 4

1

You can solve the problem in two ways, allocate the array to a large size(x should be initialized...and it should be a constant value) and then the code becomes:

#include <iostream>
#include <string>
using namespace std;

//declaration of variables

int i; // Loop Counter
const int x = 500; // MAX Number of Family Members

int main()
{

string FamilyName[x]; // Array of Names

int count;
cout << "Enter Number of Family Members" <<endl;
cin >> count;

for (i = 0 ; i < count ; i++){
        cout << "Enter Family Member's Name: " <<endl;
        cin >> FamilyName[i];
}
for (i = 0 ; i < count ; i++){
    cout << FamilyName[i] <<endl;
}

return 0;
}

Or use dynamic memory allocation to read the number of family member first and then do the allocation:

int main()
{

int count;
cout << "Enter Number of Family Members" <<endl;
cin >> count;

auto FamilyName = new string[count];

for (i = 0 ; i < count ; i++){
        cout << "Enter Family Member's Name: " <<endl;
        cin >> FamilyName[i];
}
for (i = 0 ; i < count ; i++){
    cout << FamilyName[i] <<endl;

    delete[] FamilyName;
}

Hope this helps

Sign up to request clarification or add additional context in comments.

1 Comment

The second version should use vector to manage the memory, there is no reason to use raw pointers (especially not hidden behind auto)
0

You are not allocating any memory to your string array, on this line string FamilyName[x]; // Array of Names x is not defined. It also may be a good idea to set a max array size or have some way of pushing on to the end of the array.

Comments

0

The size of your array should be constant.

Maybe use a vector of strings since vectors can grow dynamically.

string name;
vector<string> FamilyName;
for(int i = 0; i < x; i++)
{
    cin >> name;
    FamilyName.push_back(name);
}

Either that or give your array a constant size larger than any family size you might encounter.

string FamilyName[100];

if you wouldn't ever get a family with over 100 members.

Comments

0

You need to either allocate reasonable memory for FamilyName or use STL:

string[] FamilyName;
//after get x
FamilyName = new string[x];

or

vector<string> FamilyName;
cin << temp_string;
FamilyName.push_back(temp_string);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.