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;
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:
#include <iostream>
usingnamespace std;
constint 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;
}
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.
#include <iostream>
int main()
{
using std::cin;
using std::cout;
constint 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 = newchar[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
}