can anybody help me this code give the error!

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

/* ******************** getSize ********************
asks user for size
size saved in calling function via reference parameter
*/
void getSize(int);

/* ******************** getSpace ********************
gets an array whose size is chosen by the user
size saved in calling function via reference parameter
*/
void getSpace(int);

/* ******************** inputData ********************
gets data collected by the user's inputs
size saved in calling function via reference parameter
*/
void inputData(int[], int);

/* ******************** printData ********************
prints the strings, one per line
size saved in calling function via reference parameter
*/
void printData(int[], int);

/* ******************** destroy ********************
returns all space to the heap
size saved in calling function via reference parameter
*/
void destroy();

int main ()
{
int size;
int strings[size];

getSize(size);
getSpace(size);
inputData(strings, size);
printData(strings, size);
destroy();

return 0;
}

void getSize(int size)
{
cout << "Please input the size of your array: ";
cin >> size;
}

void getSpace(int size)
{
int *strings;
strings = new int[size];
}

void inputData(int strings, int size)
{
cout << "Please input your strings:" << endl;

for (int i = 0; int < size; i++)
{
cin >> strings[i];
}
}

void printData(int strings, int size)
{
for (int i = 0; int < size; i++)
{
cout << strings[i] << endl;
}
}

void destroy()
{
delete ptr;

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

/* ******************** getSize ********************
asks user for size
size saved in calling function via reference parameter
*/
void getSize(int);

/* ******************** getSpace ********************
gets an array whose size is chosen by the user
size saved in calling function via reference parameter
*/
void getSpace(int);

/* ******************** inputData ********************
gets data collected by the user's inputs
size saved in calling function via reference parameter
*/
void inputData(int[], int);

/* ******************** printData ********************
prints the strings, one per line
size saved in calling function via reference parameter
*/
void printData(int[], int);

/* ******************** destroy ********************
returns all space to the heap
size saved in calling function via reference parameter
*/
void destroy();

int main ()
{
int size;
int strings[size];

getSize(size);
getSpace(size);
inputData(strings, size);
printData(strings, size);
destroy();

return 0;
}

void getSize(int size)
{
cout << "Please input the size of your array: ";
cin >> size;
}

void getSpace(int size)
{
int *strings;
strings = new int[size];
}

void inputData(int strings, int size)
{
cout << "Please input your strings:" << endl;

for (int i = 0; int < size; i++)
{
cin >> strings[i];
}
}

void printData(int strings, int size)
{
for (int i = 0; int < size; i++)
{
cout << strings[i] << endl;
}
}

void destroy()
{
delete ptr;
}
Last edited on
Please use [code][/code] tags. (It's also the <> button in the "Format:" menu.)

Change this:
for (int i = 0; int < size; i++)
to this:
for (int i = 0; i < size; i++)
everywhere you have it.

Also, I think you made a mistake when you copy/pasted your code.
I change that but give the error in this line cin >> strings[i]; what should i do
Last edited on
The strings that gets sent to the input function is this array that is created in main with an undetermined size. If you're creating an array at compile time - the compiler needs to know the size.

1
2
int size;
int strings[size];



You aren't actually sending the size variable via reference in your functions, so for example, the size the user enters doesn't get saved back to the original size variable in main.

1
2
3
4
5
6
7
8
9
10
/* ******************** getSize ********************
asks user for size
size saved in calling function via reference parameter
*/

void getSize(int size)
{
cout << "Please input the size of your array: ";
cin >> size;
}



There is no ptr defined in the program. The destroy function doesn't know what it's supposed to be destroying.
1
2
3
4
void destroy()
{
delete ptr;
}
Realize that variables that are declared inside a function are completely local to that function -- they don't exist anywhere outside of it.

Also, your comments say that you're using reference parameters, but you're not.
So getSize should be declared as
void getSize(int&);.

Your getSpace function has some problems as well:
1
2
3
4
5
void getSpace(int size)
{
    int *strings;
    strings = new int[size];
}
When this function is called, it allocates some memory and doesn't return the pointer to it. Since the strings variable here disappears after the function ends, you end up getting a memory leak.

I suppose your inputData function should really be
void inputData(int* strings, int size) ?
Similarly for your printData function.

And for your destroy function:
1
2
3
4
void destroy()
{
    delete ptr;
}
Two problems with this:
1) There's no such thing as ptr. You probably meant to pass it as a parameter to the function:
void destroy(int* ptr)
2) Since you're allocating an array (using new[]), it should be delete[], not delete.

After fixing all of that, your main function will need a few adjustments:
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    int size;
    //int strings[size]; // Wrong -- non-constant array size, and 'size' is also uninitialized
    // Instead, it should probably be
    int* strings;

    getSize(size);
    strings = getSpace(size); // Assuming you change 'getSpace' to return the pointer
    // ...
    destroy(strings);
}

On a side note, strings is a weird name for an array of ints. If you wanted actual strings, use std::string instead of int.

Also, if you use std::vector, you won't have to worry about allocating and freeing the memory yourself.
thank you so much but when i run the program does not show the output!
#include <iostream>
#include <string>
using namespace std;

/* ******************** getSize ********************
asks user for size
size saved in calling function via reference parameter
*/
void getSize(int);

/* ******************** getSpace ********************
gets an array whose size is chosen by the user
size saved in calling function via reference parameter
*/
void getSpace(int);

/* ******************** inputData ********************
gets data collected by the user's inputs
size saved in calling function via reference parameter
*/
void inputData(int[], int);

/* ******************** printData ********************
prints the strings, one per line
size saved in calling function via reference parameter
*/
void printData(int[], int);

/* ******************** destroy ********************
returns all space to the heap
size saved in calling function via reference parameter
*/
void destroy(int* ptr);

int main ()
{
int size;
int strings[size];

getSize(size);
getSpace(size);
inputData(strings, size);
printData(strings, size);
destroy(strings);

return 0;
}

void getSize(int size)
{
cout << "Please input the size of your array: ";
cin >> size;
}

void getSpace(int size)
{
int *strings;
strings = new int[size];
}

void inputData(int *strings, int size)
{
cout << "Please input your strings:" << endl;
for (int i = 0; i < size; i++)

{
cin >> strings[i];
}
}

void printData(int* strings, int size)
{
for (int i = 0; i < size; i++)
{
cout << strings[i] << endl;
}
}

void destroy(int* ptr)
{
delete ptr;
}
I see you made some changes but the code still has some of the issues that were described in earlier posts.
Topic archived. No new replies allowed.