Character problem

This is a discussion on Character problem within the C++ Programming forums, part of the General Programming Boards category; Iím trying to code a program that displays the alphabet in alphabetical order. The file has the letters in random ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    16

    Character problem

    Iím trying to code a program that displays the alphabet in alphabetical order. The file has the letters in random order and then I coded to where its in alphabetical order. The problem is when I compile, the letters donít show up .I know I have to change the int to char and I did but I still donít get the character letters to show up.


    Code:
     void load_array (char input_filename[], int things[], int array_size);
    void display_array(int things[], int array_size);
    void bubble_sort_array(int things[], int array_size);
    void pause(void);
    
    // Variables
    char     data_name[]  = "C:\\Codes_Files\\Alphabet.txt";
    
    //******************************************************
    // main
    //******************************************************
    
    int main(void)
      {
      int record_count = count_file_values(data_filename);
      int letters[record_count];
      load_array_from_file(data_filename, letters, record_count);
      cout << "randomized letters of the alphabet: \n";
      display_array(letters, record_count);
      bubble_sort_array(letters, record_count);
      cout << "\n\nThe sorted alphabet: \n";
      display_array(letters, record_count);
      pause();
    
      return 0;
      }
    
    // load_array
    
    void load_array_from_file(char input_filename[], int things[], int array_size)
      {
      fstream  inData;           
    
      inData.open(input_filename, ios::in);        
      if (!inData)
        {
        cout << "\n\nError opening file: " << input_filename << "\n\n";
        pause();
        exit(EXIT_FAILURE);
        }
      else
        {
        for (int i = 0; i < array_size; i++)   
          {
          inData >> things[i];
          }
        inData.close();
        }
      return;
      }
    
    
    // display_array
    
    void display_array(int things[], int array_size)
      {
      for (char i = 0; i < array_size; i++)
        {
        cout << things [i] << " " ;
        }
      
    
      return;
      }
    
    
    // bubble_sort_array
    
    void bubble_sort_array(int things[], int array_size)
      {
      bool moresortneeded;   
      int  temp;             
      do
        {
        moresortneeded = false;
        for(int i = 0; i < array_size - 1; i++)
          {
          if(things[i] > things[i+1])
            {
            temp = things[i];
            things[i] = things[i+1];
            things[i+1] = temp;
            moresortneeded = true;
            }
          }
        }
      while(moresortneeded);
    
      return;
      }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
      int record_count = count_file_values(data_filename);
      int letters[record_count];
    For one thing I don't see the definition of count_file_values (it may be returning 0 for all we know or some other bad value).

    Secondly, that code is not valid C++. The size of arrays needs to be known at compile time. GCC only allows this as an extension (since it is valid in C90) - use the -pedantic flag to turn off compiler extensions.

    Instead why not use a std::string to hold the contents of the file? Then you won't need to count the number of characters first and have something that actually stores characters, not numbers.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    Actually I have it defined, I just didnt paste it xD

    int count_file_values(char input_filename[]);
    void load_array_from_file(char input_filename[], int things[], int array_size);
    void display_array(int things[], int array_size);
    void bubble_sort_array(int things[], int array_size);
    void pause(void);

    I'm using two files. One deals with numbers, and one with the characters. Basically instead of trying to use two seperate files (one for numbers, one for characters) I just thought it was easier just to change parts of one to use for the other. The code I pasted now is for the number file I have. I'm trying to change parts of the interger data type to fit the character type by I'm not sure where.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. about wide character and multiple byte character
    By George2 in forum C Programming
    Replies: 3
    Last Post: 05-22-2006, 09:11 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. printing strings (was similar problem)
    By weirdbeardmt in forum C Programming
    Replies: 5
    Last Post: 06-01-2004, 02:12 PM
  4. problem comparing character
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 03-19-2004, 12:15 PM
  5. Problem with character arrays in classes
    By spoketoosoon in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2004, 03:57 AM

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