How could I initialize variables I want to input data in?

Hello everyone.
I am a new person and I have the code snippet code that has not yet run, can you help me?

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

void arrReplace(){
string *arrname;
cout<<"Input array name\n";
cin>>*arrname;
}
void arrInputSize(){
int *n;
cout<<"Input number of elements\n";
cin>>*n;
}
void arrInput(){
int *n;
int *arr[*n];
for(int x=0;x<*n;x++){
cout<<"Input "<<x++<<" array element";
cin>>*arr[x];
}
}
void endConfig(){
int *n;
int *arr[*n];
char answer;
string *arrname;
cout<<"Result: "<<*arrname<<"[";
for(int x=0;x<*n;x++){
cout<<*arr[x];
}
cout<<"], "<<n<<" elements.\n Wanna change something?\nO.No\nA.Yes";
cin>>answer;
while(answer!='O'){
cout<<"What exactly?\nA.Name\nB.Number of elements\nC.Array values\nO.Nothing";
}
if(answer=='A'){
void arrReplace();
}
if(answer=='B'){
void arrInputSize();
}
if(answer=='C'){
void arrInput();
}
cout<<"Result: "<<*arrname<<"[";
for(int x=0;x<*n;x++){
cout<<*arr[x];
}
cout<<"], "<<*n<<" elements.";
}
int main()
{
arrReplace();
arrInputSize();
arrInput();
endConfig();
return 0;
}
Thanks
1
2
3
4
5
void arrReplace(){
string *arrname;
cout<<"Input array name\n";
cin>>*arrname;
}


That's wrong. This code creates a string-pointer. It does not create a string. The string-pointer simply points to random memory somewhere, and then you try to write into that random memory. This is bad. If you want to use a string, create a string first, then you can use it.

1
2
3
4
5
void arrInputSize(){
int *n;
cout<<"Input number of elements\n";
cin>>*n;
}

Same problem here.

1
2
3
4
5
6
7
8
9
void endConfig(){
int *n;
int *arr[*n];
char answer;
string *arrname;
cout<<"Result: "<<*arrname<<"[";
for(int x=0;x<*n;x++){
cout<<*arr[x];
}

This code creates a string-pointer, pointing to random memory, and then tries to output the string (which doesn't exist) straight away without ever having stored any value in the string (that doesn't even exist). This makes no sense at all. It also tries to create an array of int-pointers of size *n , but n is an int-pointer pointing to random memory so this is just wrong as well.

It looks like you don't understand pointers, and you don't understand that creating a variable with one name in one place, and creating another variable with the same name somewhere else, isn't the same variable; it's two different variables, storing different data. It looks like you believe that creating an array in different functions will make them all the same array with the same data in them. This is not true.

This will sound harsh; the above code is too advanced for you. We could fix it up, but you won't really benefit. You need to start simpler. How about you try just creating a string, storing data in it from cin, and then outputting that to screen? Get that working, and then you can try it with a function.

Last edited on
Hello kullboys,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

As Repeater has pointed out some of your problems I will try to expand on some of what he said.

Your program has many problems. Some will not be seen until the program will run.

For one thing each function defines:

1
2
int *n;
int *arr[*n];

The first thing you need to realize is that when the function ends these variables are lost and no longer available to other functions. You could define these variables in main and pass them to the functions.

The next problem is int *arr[*n];. This is not allowed in C++. The variable "n" needs to be a constant value. Second by the time you use "n" you have not given it a value, so you have no idea what garbage value is at the memory address. Overlooking the fact that you are creating an array of pointers what needs to be done is:

1
2
3
4
5
int arr[20]{};

or

int arr[MAXSIZE]{};

Where "MAXSIZE" is defined as: constexpr int MAXSIZE{20}; or const int MAXSIZE{20};. And the 20 could be any number you need for the size of the array. The empty {}s will initialize the entire array to zero.

in the function "endConfig()" There are several problems.

The for loop just does not look right. Mostly in the dereferencing of the pointer. This may help you to understand pointers: http://www.cplusplus.com/doc/tutorial/pointers/

If you enter anything other than "O" you have an endless while loop. I would also suggest something like this after entering your choice:

1
2
3
cout << "], " << n << " elements.\n Wanna change something?\nO.No\nA.Yes";
cin >> answer;
answer = toupper(answer);

This way it does not matter what case is entered it will end up as upper case in the end. You may need to add the header file "<cctype>" at the beginning of your program.

The if statements will either never be reached or used because the only way to get there is to start with "O", but you never test for "O" and all the other if statements will be skipped.

In the if statements you have very nice prototypes, that are not needed, but no function call as I believe you intended. Remove the void to make it a function call. And you may want to read up on this: http://www.cplusplus.com/doc/tutorial/functions/

The whole logic of the while loop needs to be restructured. The while loop needs to contain more then just a "cout" statement.

You have a good start with your program and I agree with Repeater you should loose the pointers for now and get the program working with simple variables first.

One last point I would like to make. Most of your code is written this way and there is technically nothing wrong with it.

1
2
3
4
5
6
7
8
void arrInput(){
int *n;
int *arr[*n];
for(int x=0;x<*n;x++){
cout<<"Input "<<x++<<" array element";
cin>>*arr[x];
}
}


But this is much easier to read and follow.

1
2
3
4
5
6
7
8
9
10
void arrInput()
{
	int *n;
	int *arr[*n];
	for (int x = 0; x<*n; x++)
	{
		cout << "Input " << x++ << " array element";
		cin >> *arr[x];
	}
}

When the {} line up in the same column they are much easier to match up and the indentation helps keeps things separate.

Hope that helps,

Andy
Topic archived. No new replies allowed.