Thread: Funny looking characters

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    27

    Funny looking characters

    This program runs fine but I am getting funny looking characters.
    It's just really one, it's at the botton of the screen. You should see it once u run for yourself.
    I don't know how it gets there or why but It would be GREATLY APPRECIATED IF SOMEONE COULD HELP ME OUT WITH THIS ONE.
    CAN"T THANK-YOU ENOUGH.

    This is infact an assignment due in a couple of days.

    /* By: Huynh Pham
    Date: 07/03/02
    File: ass57q2.cpp

    Uses the selection sort algorithm to sort an unsorted list of 10 names and telephone numbers into ascending alphabetic order
    */

    #include <iostream.h>
    #include <string.h>
    #include <fstream.h>
    #define size 10

    class List_class
    {
    private:
    char names[size][15], numbers[size][15];

    public:
    List_class();
    void to_screen();
    void selection_sort();
    void to_file();
    };

    //-----------------------------------------------------------------------

    void main(void)
    {
    List_class names_list;

    cout << "This is the list before it was sorted" << endl;
    names_list.to_screen();
    names_list.selection_sort();
    cout << endl << endl << "This is the list after it was sorted" << endl;
    names_list.to_screen();
    names_list.to_file();
    } // main

    //-----------------------------------------------------------------------

    List_class::List_class()
    // reads the list in from a file
    {
    fstream infile ("a57q1.txt", ios::in);
    for (int a = 1; a <= size; a++)
    {
    infile >> names[a] >> numbers[a];
    } // for
    infile.close();
    } // constructor

    //-----------------------------------------------------------------------

    void List_class::to_screen()
    // displays the list to the screen
    {
    for (int a = 1; a <= size; a++)
    {
    cout << names[a] << " " << numbers[a] << endl;
    } // for
    } // show_list

    //-----------------------------------------------------------------------

    void List_class::to_file()
    // writes sorted list to text file: a57q3.txt
    {
    fstream outfile ("a57q3a.txt", ios:ut);
    for (int a = 1; a <= size; a++)
    {
    outfile << names[a] << " " << numbers[a] << endl;
    } // for
    outfile.close();
    } // constructor

    //-----------------------------------------------------------------------

    void List_class::selection_sort()
    //sorts an array into alphabetic order
    {
    int loc_of_min;
    char min_names[size], min_num[size], temp_names[size], temp_num[size];

    //The first element in the array is set to be the minimum.
    for (int y = 1; y <= size; y++)
    {
    strcpy(min_names, names[y]);
    strcpy(min_num, numbers[y]);
    loc_of_min = y;

    //The rest of the element is compared to the
    minimum.
    for (int z = y + 1; z <= size; z++)
    {

    //If there is an element smaller
    then the minimum element then set
    //this to the minimum.
    if (strcmp(names[z], min_names) < 0)
    {
    strcpy(min_names, names[z]);
    strcpy(min_num, numbers[z]);
    loc_of_min = z;
    } //if
    } //for

    //Swap the minimum element with the new
    minimum. Numbers corresponding
    //to the name is also swap to stop numbers
    pairing up with other names
    //other than their original.
    if (strcmp(min_names, names[y]) < 0)
    {
    strcpy(temp_names, names[y]);
    strcpy(temp_num, numbers[y]);
    strcpy(names[y], min_names);
    strcpy(numbers[y], min_num);
    strcpy(names[loc_of_min],
    temp_names);
    strcpy(numbers[loc_of_min],
    temp_num);
    } //if
    } //for
    } // selection sort

    *************TEXT FILE LOOKS LIKE THIS*****************
    Ren 999666
    Nidhish 111222
    Nadeem 888222
    Mark 789001
    Sharif 456789
    Jalna 987654
    Miwako 000111
    Misha 007007
    Jayantha 123456
    Diana 111555

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    why did u define size s 10??? wouldnt it be ezr to type 10? lolzz

  3. #3
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Tsk tsk... You are missing the whole point of #define's, Denethor. You define the keyword size to ten and then use the keyword wherever you want. Then if you decided to change the size #, then you only have to change it in one place. Plus it makes things more readable.

    I compiled the code and found nothing wrong with it. No funny characters at all. I had to correct like one or two things (you forgot to comment some things and you opened a file wrong, but I think that was just a typing mistake in the post. Could be your compiler?
    Last edited by biosx; 03-20-2002 at 12:19 AM.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    thats sweating small stuff. this is what i would use it for, at the LEAST:

    #define WHITE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
    #define PURPLE FOREGROUND_BLUE | FOREGROUND_BLUE
    #define RED FOREGROUND_RED
    #define ORANGE FOREGROUND_RED | FOREGROUND_YELLOW
    #define BROWN FOREGROUND_GREEN | FOREGROUND_RED
    #define GREEN FOREGROUND_GREEN
    #define BLUE FOREGROUND_BLUE
    #define GRAY FOREGROUND_INTENSITY
    #define DARKGREEN BLUE | GRAY
    #define DARKRED RED | GRAY
    #define DARKPURPLE PURPLE | GRAY
    #define DARKBROWN BROWN | GRAY
    #define BLACK GRAY | GRAY | GRAY | GRAY

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    27
    What Biosx said is very true. It is defined so that I only have to change it once if I decided to change the size. However if I didn't do it the way it did it than when it comes to changing the size I would have to do it like 5 or 6 times, rather than once!


    Anyways back to my problem

    PLEASE HELP............

  6. #6
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Compare these two pieces of code that do the same things:
    Code:
    /*   NUMBER ONE   */ 
    #include <iostream.h>
    
    int main()
    {
       int i, j, k, l;
    
       for(i = 0; i < 20; i++)
          for(j = 1; j <= 20; j++)
             for(k = 2; k <= 20; k++)
                for(l = 0; l != 20; l++)
                   cout << 20 << endl;
    
       return 0;
    }
    .. and ..

    Code:
    /*   NUMBER TWO  */
    #include <iostream.h>
    
    #define NUMBER 20
    
    int main()
    {
       int i, j, k, l;
    
       for(i = 0; i < NUMBER; i++)
          for(j = 1; j <= NUMBER; j++)
             for(k = 2; k <= NUMBER; k++)
                for(l = 0; l != NUMBER; l++)
                   cout << NUMBER << endl;
    
       return 0;
    }
    Say I wanted to change the number of times these looped. For the first snippet I would have to make 5 corrections. In the second snippet I would only have to make 1 correction. Now imagine that this program was alot bigger. It would be really tough to go through. Plus words tell alot more than numbers. It makes code more clear.

  7. #7
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Originally posted by Wiz_Nil
    What Biosx said is very true. It is defined so that I only have to change it once if I decided to change the size. However if I didn't do it the way it did it than when it comes to changing the size I would have to do it like 5 or 6 times, rather than once!


    Anyways back to my problem

    PLEASE HELP............
    Like I said, there is nothing wrong with it (at least on my end).

  8. #8
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    USE CODE TAGS!!!!!!!!!!!!!!
    It's the # button where you are writing your message. Code is unreadable for me without spacing.

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    okay okay i getseses it :P

  10. #10
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Originally posted by tim545666
    USE CODE TAGS!!!!!!!!!!!!!!
    It's the # button where you are writing your message. Code is unreadable for me without spacing.
    I agree, alot of people don't use the code tags anymore/at-all. We need to re-enforce it.

    Code:
    code code code tags!!
    
    spacing
       stays
          where
             it
                should...

  11. #11
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    wiz-nil, i preface this by saying i don't know much about much. but when you loop to grab the aray, should a start at 0 rather than 1. i don't know. but funny looking characters means the prog is not looking in the right memory. try it see what happens

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    WHATS THE TAG 4 CODE?

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Follow blight2c's advice. Every one of your for-loops should start at 0 and end at size-1.

    List_class::List_class()
    // reads the list in from a file
    {
    fstream infile ("a57q1.txt", ios::in);
    for (int a = 0; a < size; a++)
    .
    .
    .
    void List_class::to_screen()
    // displays the list to the screen
    {
    for (int a = 0; a < size; a++)
    .
    .
    .
    void List_class::to_file()
    // writes sorted list to text file: a57q3.txt
    {
    fstream outfile ("a57q3a.txt", ios:ut);
    for (int a = 0; a < size; a++)
    .
    .
    .
    void List_class::selection_sort()
    //sorts an array into alphabetic order
    {
    int loc_of_min;
    char min_names[size], min_num[size], temp_names[size], temp_num[size];

    //The first element in the array is set to be the minimum.
    for (int y = 0; y < size; y++)
    .
    .
    .
    for (int z = y + 1; z < size; z++)
    {

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. Funny characters being printed
    By learninC in forum C Programming
    Replies: 8
    Last Post: 03-17-2005, 09:02 AM
  4. Characters. Funny looking ones
    By Wiz_Nil in forum C++ Programming
    Replies: 8
    Last Post: 02-25-2002, 12:33 PM
  5. For some reason, it prints funny characters ???
    By Nutshell in forum C Programming
    Replies: 8
    Last Post: 01-14-2002, 04:27 PM