I have almost everything done except me entering 3 digits and getting the correct sum through pointers. Did I do the formula right? Also, for some reason when I put in the numbers it doesn't even display them back anymore. I did nothing to alter the display itself.
#include <iostream>
usingnamespace std;
constint MAXNAME = 10;
int main()
{
int pos;
char* name;
int* one;
int* two;
int* three;
int result;
one = newint;
two = newint;
three = newint;
name = newchar;
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+pos); // read a character into the name array
cout << "Hi ";
for (pos = 0; pos < MAXNAME; pos++)
cout << *(name+pos); // print a character from the name array
cout << endl << "Enter three integer numbers separated by blanks" << endl;
cin >> *one >> *two >> *three;
cout << "The three numbers are " << endl;
cout << *one << "|" << *two << "|" << *three << endl;// output those numbers
result = *one + *two + *three / 3;// calculate the sum of the three numbers
cout << "The sum of the three values is " << result << endl;
delete one;//code to deallocate one, two, three and name
delete two;
delete three;
delete name;
return 0;
}
Remember that newmust be paired with delete; new[]must be paired with delete[].
Get in the habit of freeing resources in the opposite order of their acquisition.
1 2 3 4 5 6 7
int* one = newint;
int* two = newint;
int* three = newint;
// ... delete in opposite order of allocation
delete three;
delete two;
delete one;
Ignoring this guideline is a source of errors when the resources are interdependent.
new and delete are a huge source of problems. There's more in addition to what I've mentioned already. For example, even though delete is used, the program still leaks memory in certain cases because it does not provide any exception safety guarantee.
The bottom line is that new and delete are for careful use by experts. Strongly prefer library components that eliminate these problems, such as std::string, std::vector, and std::unique_ptr.
Ok thanks for the input, but I still can't tell why in the world it won't display the numbers I put in and have the formula do its work for the final answer. It was working find prior to posting, but decided not to work.
I still can't tell why in the world it won't display the numbers I put in
Writing outside of the bounds of your char (pretending there's space for 10 when there's only room for one) causes undefined behavior.
Programs with undefined behavior have meaningless results. The C++ standard doesn't say how such programs should behave, so their behavior is incidental at best. You got (un)lucky, and then you didn't.