#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    32
    Rep Power
    11

    Can't format a number for output


    I'm working on a program and i'm almost done. There's one thing that's bugging me and I can't seem to figure it out. I have a program that takes input from a user and stores up to 5 names (First, Last and Age) in an array. Then it asks them how they want to sort the output.

    First and last work fine. Age works...but with one problem. If all ages are 2 digit (ie: 20, 40, 60) then it lists them in order.
    • 20
      40
      60


    If any digit is a single digit along with the double digits, like if I added "3" and "5" it prints like this...

    • 20
      3
      40
      5
      60


    When it should print

    • 3
      5
      20
      40
      60


    any idea why this would happen? Or how to fix it?

    Here's how I call it in my main .cpp file.
    PHP Code:
    if (menu == "3")
        {
            
    cout<<endl<<"Here is the information you entered, sorted by age:"<<endl<<endl;
            
    myEmpinfos.sortAge();
            
    myEmpinfos.getEmpinfos();
        } 
    And this is how I sort it in the .h file I created
    PHP Code:
        void Empinfo::sortAge()
    {
        
    string ageStore;                        
        
    bool sortMore true;
        
    int i;

        do
        {
            
    sortMore false;                        for(0lastcnt-1i++)                                            
                                            
            {
                if ( 
    age[i] > age[1] ) 
                {
                    
    sortMore true
                    

                    
    ageStore age[i+1];              
                    
    age[i+1] = age[i];                          
                    
    age[i] = ageStore;  

                    
                    
    ageStore first[i+1];              
                    
    first[i+1] = first[i];                          
                    
    first[i] = ageStore;  

                    
    ageStore last[i+1];
                    
    last[i+1] = last[i];
                    
    last[i] = ageStore;

                }
            }
        }while(
    sortMore); 
    And here's the class i've used
    PHP Code:
    class Empinfo
    {
        public:
            
    Empinfo();
            
    int setEmpinfo(stringstringstringint);
            
    void getEmpinfos();
            
    void sortFirstname();
            
    void sortLastname();
            
    void sortAge();
        private:
            
    string first[EmpinfoCnt];
            
    string last[EmpinfoCnt];
            
    string age[EmpinfoCnt];
            
    int lastcnt;
    }; 
    Anyone able to help me here?

    Thanks in advance!
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    Change the declaration of the age member of EmpInfo from string to int. i.e. change this:
    string age[EmpinfoCnt];
    to:
    int age[EmpinfoCnt];
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    32
    Rep Power
    11
    I did that and then had to change it in a few other places.. but this doesn't seem to be working. Doing that is going to cause me to change my code more than I really want to I think.

    I'd have to change it in the sorting process and that was complicated enough. hehe.

    Any other ideas?
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    In that case, you'll need to change your sorting process to left-pad elements with '0', if they're not long enough. The problem is that when you do an ASCII-wise sort, instead of a numeric sort, the string "2004" is less than the string "3", but "2004" is greater than "0003". So you'll need to change the sort routine to left-pad the numbers you are comparing with zeros, if one is shorter than the other.

    Another alternative would be to use atoi() to convert the two strings to int and then comparing those two values instead, sort of like this:
    Code:
    void Empinfo::sortAge()
    {
        string ageStore;                        
        bool sortMore = true;
        int i;
        int tmp1, tmp2;
    
        do
        {
            sortMore = false;                        for(i = 0; i < lastcnt-1; i++)                                            
                                            
            {
                tmp1 = atoi(age[i].c_str());
                tmp2 = atoi(age[i+1].c_str());
                if ( tmp1 > tmp2 ) 
                {
                      // rest of your code comes here
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    32
    Rep Power
    11
    Thanks for the fast replies.

    So just plug it in like this?
    PHP Code:
    do
        {
            
    sortMore false;                // Prepare for no swap to occur, set Boolean to false
            
                
    tmp1 atoi(age[i].c_str());
                
    tmp2 atoi(age[i+1].c_str());
                if ( 
    tmp1 tmp2 

            
            for(
    0lastcnt-1i++)    // Loop through all elements in the array
                                            // Subtract 1 from "lastcnt" to ensure we don't
                                            // go past the end of array.
            

    I make Windows really unhappy when I do that. heh. How does it fit into my existing code?
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    Nope, the code for tmp1 and tmp2 should be inside the for loop, otherwise age[i] and age[i+1] have no meaning.
    Code:
    do
        {
            sortMore = false;
            for(i = 0; i < lastcnt-1; i++)                                        
            {
                tmp1 = atoi(age[i].c_str());
                tmp2 = atoi(age[i+1].c_str());
                if ( tmp1 > tmp2 ) 
                {
                      // rest of your code comes here
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    32
    Rep Power
    11
    Thanks again for your replies. Really fast.

    Hrm...same problem. I pasted it in exactly how you put it..declared the variables.. and it won't run. Maybe I should try the other way you talked about. left-padding the numbers. How would I do that?

IMN logo majestic logo threadwatch logo seochat tools logo