Ascii conversion problems with dynamic memory

This is a discussion on Ascii conversion problems with dynamic memory within the C++ Programming forums, part of the General Programming Boards category; Hello. I'm attempting some programming with pointers/dynamic memory, and I'm having some problems. I want the user to be able ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    1

    Ascii conversion problems with dynamic memory

    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;
    }

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Don't know a ton of C++, but since total's a pointer, shouldn't this:

    cout << "The sum of the ASCII values is :" << total << "." << endl;

    be this:

    cout << "The sum of the ASCII values is :" << *total << "." << endl;

    Actually, now that I look closer, shouldn't everywhere you use total be dereferenced?

  3. #3
    Unregistered
    Guest
    I know it's not pertinent to the question you asked, but since you are just starting with dynamic memory you should be aware that there are two syntactical versions of delete. One for striaight pointers, and one for arrays. The pointer version is:

    delete pointername;

    and the array version is:

    delete [] arrayname;

    In your code you only use the array version, despite the fact that total and input are just pointers, and not arrays.

    Also, if you are going to check that there was adequate memory for the dynamic variables, and it is a good idea, you should do so before you try to use them like you did with array, not after, like you did with input (and for some reason you didn't even check for total).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with dynamic memory in C++
    By IndioDoido in forum C++ Programming
    Replies: 8
    Last Post: 10-31-2007, 04:58 PM
  2. Dynamic Linking & Memory usage
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 06-02-2007, 10:57 PM
  3. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 03:15 PM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2003, 12:51 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 02:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21