Dynamic Array

This is a discussion on Dynamic Array within the C++ Programming forums, part of the General Programming Boards category; Hello All, So I have a program I am working on that utilizes a dynamic array and then takes a ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    8

    Dynamic Array

    Hello All,

    So I have a program I am working on that utilizes a dynamic array and then takes a series of lowercase letters and changes them to uppercase. I have this program so far but when I entered in the toupper to switch from lowercase to uppercase I keep getting errors. I do not know what I am doing wrong so any pointers would be appreciated. I am pretty positive that my code is pretty much done but I am just making a mistake somewhere. Thanks in advance for any help.

    Code:
    #include <cstdlib>
    #include <iostream>
    
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        
        char* letter;
        int numberOfLetter;
        
        cout << "Please enter the number of letters " 
             << "you would like to capitalize:" << endl;
             
        cin >> numberOfLetter;
        
        letter = new char[numberOfLetter];
    
           for (int i = 0; i < numberOfLetter; i++){
               cout << "Please enter a letter " << i + 1 << endl;
               cin >> letter[i];
    }                        
               cout << "The letters you entered were:" << endl;
          
               char *letter2 = new;     
           
           for (int i = 0; i < numberOfLetter; i++){
               cout << letter2[i] << endl;
           
               letter[i] = letter2[i];     
                
                if(isalpha(letter2[i]))
          {
              letter2[i] = toupper(letter2[i]); 
          } 
    }    
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And by "errors" you mean what, exactly?

    (Note that toupper isn't even defined unless you #define <cctype>.)

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    Thanks I totally forgot about the <cctype>. I am getting a build error and it is telling me that I need a identifier before the ';' token but I am not understanding what it is wanting. I have been tinkering but I am just trying to get it to compile.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    An identifier before which ; token? (Although usually it's the other way around, but still.)

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    The error message is specific too char *letter2 = new;

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    new is a keyword: new what?

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    It is supposed to be a pointer to "char* letter". After I had taken a look to that line of code I realized I do not need anything other than "char *letter2;". The program compiled but I ran it, entered in the letters to convert and it gave the line "The letters you entered were:" and then windows force shut it down.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    This is the code that force closes my program. It is weird that it compiles but then doesnt allow the program to finish properly.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <cctype>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        
        char* letter;
        int numberOfLetter;
        
        cout << "Please enter the number of letters " 
             << "you would like to capitalize:" << endl;
             
        cin >> numberOfLetter;
        
        letter = new char[numberOfLetter];
       
           for (int i = 0; i < numberOfLetter; i++){
               cout << "Please enter a letter " << i + 1 << endl;
               cin >> letter[i];
    }                        
               cout << "The letters you entered were:" << endl;
    //      letter = new char[letter2];
           char *letter2;     
           
           for (int i = 0; i < numberOfLetter; i++){
               cout << letter2[i] << endl;
           
               letter[i] = letter2[i];     
                
                if(isalpha(letter2[i]))
          {
              letter2[i] = toupper(letter2[i]); 
          } 
    }    
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need some letters there -- if you just do char *letter2; then letter2 points to New Jersey and you don't want to go there. You can make it point to letter if you want, or you can do another new char[array].

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    So I got it to finally run the whole program but now I am getting symbols instead of letters.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <cctype>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        
        char* letter;
        char* letter2;
        int numberOfLetter;
        
        cout << "Please enter the number of letters " 
             << "you would like to capitalize:" << endl;
             
        cin >> numberOfLetter;
        
        letter = new char[numberOfLetter];
       
           for (int i = 0; i < numberOfLetter; i++){
               cout << "Please enter a letter " << i + 1 << endl;
               cin >> letter[i];
    }                        
               cout << "The letters you entered were:" << endl;
    //         letter = new char[letter2];
    //         char *letter2;     
               letter2 = new char;
           
           for (int i = 0; i < numberOfLetter; i++){
               cout << letter2[i] << endl;
           
               letter[i] = letter2[i];     
                
                if(isalpha(letter2[i]))
          {
              letter2[i] = toupper(letter2[i]); 
          } 
    }    
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    "new char" is incredibly wrong. You need an array of chars. You also never assign anything to letter2[i] before calling toupper, meaning you are calling toupper on complete nonsense, rather than the string that was typed in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Array Resizing
    By dld333 in forum C++ Programming
    Replies: 13
    Last Post: 11-03-2005, 11:13 PM
  2. need help with dynamic array syntax
    By soldyne in forum C Programming
    Replies: 3
    Last Post: 10-11-2005, 01:59 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 09:02 PM
  5. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 11:48 AM

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