Hello. I'm attempting some programming with pointers/dynamic memory, and I'm having some problems. I want the user to be able to input some characters, and they will be output onto the screen along with the sum of their ascii values. I just can't seem to get the values to add up, and I always end up displaying a memory address when I try to display the sum. I've attempted a couple of different techniques, including the use of getch(), which is in this version of the code.
#include <iostream.h>
#include <conio.h>
void main (void)
{
int *total; // Store the sum of ascii values
total = new int;
int *input; // Store the # of char to be entered
input = new int;
int asci; // Variable
cout << "How many characters are to be entered?: ";
cin >> *input;
if (input == NULL){
cout << "Memory Error!\n";
return;
}
char *array;
array = new char[*input];
if (array == NULL){
cout << "Memory Error!\n";
return;
}
for (int count=1; count <=*input; count ++){
cout << "Enter character " << count << ":";
cin.ignore();
cin.get(array[count]);
asci = getch();
total +=asci;
if (total == NULL){
cout << "Memory Error!\n";
return;
}
}
cout << "You entered ";
for (count=0; count <=*input; count ++) {
cout <<array[count];
}
cout << endl;
cout << "The sum of the ASCII values is :" << total << "." << endl;
delete [] array;
delete [] input;
delete [] total;
}