Dynamic Array Problems

Hello, i have very limited experience with dynamic memory and was writing a sample program that i am only have 1 issue with. As you can see in the program it asks you to fill in your last name with 10 total letters and to repeat the last letter if < 10 letters. After it's supposed to print "Hi <last name with 10 letters>". For example if i put "Jonessssss" it would only print "Hi ssssssssss". Any help is appreciated.

#include <iostream>

using namespace std;

const int MAXNAME = 10;

int main()

{

int pos;

char * name; int * one; int * two; int * three; int result;

one = new int[10];
two = new int[10];

three = new int[10];
name = new char[10];

cout << "Enter your last name with exactly 10 characters." << endl;

cout << "If your name has < 10 characters, repeat last letter. " << endl

<< "Blanks at the end do not count." << endl;

for (pos = 0; pos < MAXNAME; pos++)

cin >> name[10];

cout << "Hi ";

for (pos = 0; pos < MAXNAME; pos++)


cout << name[10];



cout << endl << "Enter three integer numbers separated by blanks" << endl;

cin >> *one;
cin >> *two;
cin >> *three;


cout << "The three numbers are " << endl;

cout << *one << " " << *two << " " << *three << endl;

result = *one + *two + *three;

cout << "The sum of the three values is " << result << endl;

delete one;
delete two;
delete three;
delete name;

system("pause");
return 0;

}
name[10] is a memory location after the array of ten characters that the 'name' points to. You may not read it and must not write to it.

"C-string" has to have a terminating null character at the end.

https://www.cprogramming.com/tutorial/lesson9.html
http://www.cplusplus.com/reference/ostream/ostream/operator-free/


PS. Do you know that *one and one[0] mean the same thing, when the 'one' is a pointer?
Last edited on
C++ arrays are indexed starting at 0, so your name array with 10 characters is indexed with values 0..9. name[10] is not valid.

So just change name[10] to name[9] in two places.

That will make the program work, but I have to ask why you're creating these arrays at all? Since you only ever use one element in each array, they could be ordinary variables instead:
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
#include <iostream>

using namespace std;

const int MAXNAME = 10;

int
main()
{

    int pos;

    char name;
    int one;
    int two;
    int three;
    int result;

    cout << "Enter your last name with exactly 10 characters." << endl;
    cout << "If your name has < 10 characters, repeat last letter. " << endl
	 << "Blanks at the end do not count." << endl;

    for (pos = 0; pos < MAXNAME; pos++)
	cin >> name;

    cout << "Hi ";

    for (pos = 0; pos < MAXNAME; pos++)
	cout << name;

    cout << endl << "Enter three integer numbers separated by blanks" << endl;

    cin >> one;
    cin >> two;
    cin >> three;

    cout << "The three numbers are " << endl;

    cout << one << " " << two << " " << three << endl;

    result = one + two + three;

    cout << "The sum of the three values is " << result << endl;
    system("pause");
    return 0;
}

I think that's just going to confuse them more...

Demii,
1
2
for (pos = 0; pos < MAXNAME; pos++)
  cin >> name[10]; 

You're accessing name[10] (an invalid index as the others have mentioned) MAXNAME times. Perhaps you meant to do name[pos]?
Ganado that's what it was thank you.
Note: You use both MAXNAME and literal 10 in your code. It is hard to know whether they are unrelated. Furthermore, if they are related and you do change one, you have to update the others too.

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
#include <iostream>

int main()
{
  using std::cin;
  using std::cout;
  const int MAXNAME = 10;  // this const does not have to be global; it is used only in main()

  int pos;

  // initialize to null on declaration
  char * name = new char[MAXNAME+1] {};

  cout << "Enter your last name with exactly " << MAXNAME << " characters.\n";
  cout << "If your name has < " << MAXNAME << " characters, repeat last letter. \n"
       << "Blanks at the end do not count.\n";

  for ( pos = 0; pos < MAXNAME; ++pos )
    cin >> name[pos];

  cout << "Hi ";
  for ( pos = 0; pos < MAXNAME; ++pos )
    cout << name[pos];
  cout << '\n';

  cout << "Hi " << name << '\n';

  cout << "More seriously " << MAXNAME << " non-blanks\n"
  pos = 0;
  char input = 0;
  while ( pos < MAXNAME && cin >> input && input != ' ' ) {
    name[pos] = input;
    ++pos;
  }
  cout << "Hi " << name << '\n';

  delete [] name; // array delete
}
Last edited on
Topic archived. No new replies allowed.