why won't this work??

This is a discussion on why won't this work?? within the C++ Programming forums, part of the General Programming Boards category; i'm trying to have this thing reaarange the numbers in a string into descending order and then print out the ...

  1. #1
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69

    why won't this work??

    i'm trying to have this thing reaarange the numbers in a string into descending order and then print out the new number , but something's the matter and i don't know what... help!!
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <string>
    
    void ascendingdigit(int initialdigit [], int n);
    
    int main()
    {
    char initialdigit [5];
    bool done = false;
    
    cout<<"Enter a non-zero 4 digit number..."<<endl;
    cin>>initialdigit;
    do
     {
         void ascendingdigit(int initialdigit [], int n);
    
     }
    
     while (!done);
    
          system("PAUSE");
          return 0;
    }
    
    void ascendingdigit(int initialdigit[], int n)
    {
        bool exchanges;
        int temp;
        int i;
        do {
           exchanges = false;
           for (i=0; i<initialdigit [0] -1; i++)
           {
              if (initialdigit[i] > initialdigit[i+1])
              {
                 temp = initialdigit[i];
                 initialdigit[i] = initialdigit[i+1];
                 initialdigit[i+1] = temp;
                 exchanges = true;
                 cout<<temp<<endl;
              }
    
           }
        } while (exchanges);
     }

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    do {
        // void ascendingdigit(int initialdigit [], int n);
        ascendingdigit( initialdigit, n )   // !!Warning, n is not initialized!!
     }
    
     while (!done);
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    this didn't work, i'm lost... i hate C++, lol... i need someone to look at this code and help me figure out why it won't work...

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    My guess is that you are trying to make a Kaprekar's number program where you have to rearrange the digits of a 4 digit number in least to greatest and greatest to least order, and then subtract them. My suggestion would be to make two functions: one to isolate a given digit of a given number( digit 0 = ones place ), and one to reconstruct an integer value from a vector of digits. Put your number into a vector with four elements, one for each digit, using that function. This way, the sorting becomes very easy.

    Code:
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    //...
    
    vector<int> foo; //vector that holds your digits
    
    //To sort from greatest to least
    sort( foo.begin(), foo.end() );
    
    //Then call your function to construct your greatest-least number
    
    //And from least to greatest, use reverse iterators
    sort( foo.rbegin(), foo.rend() );
    
    //And call the function again to get your least-greatest number from it
    If you're not familiar with the STL or standard algorithms, I suggest you learn to use them because they are obviously very handy.

  5. #5
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    i'm not allowed to use those commands yet for the class that i'm in, it would be nice if i could though. i have to use the code that i posted in the first post. if anyone could help me figure out why the code i posted isnt working would be awesome, plus this assignment is due tomorrow! and i've been trying to figure it out for over a week now... HELP!!!

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Look my first post above. Did you get it? If not, *ask*

    Also could you tell me what this loop is supposed to do?
    Code:
    for ( i = 0; i < initialdigit [ 0 ] - 1; i++ )
    {
       ......
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

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