Thread: assignment of new string

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

    assignment of new string

    i need to figure out a way to make the resulting 4 digit number assigned into a new string... any help would be appreciated...
    also, i can't get this while statement to work??? i suck at C++!!!!

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <string.h>
    
    int main()
    
    {
    bool valid = false;
    char initialdigit [5];
    int x;
    int tmp;
    int firstinteger;
    
     cout<<"Please enter a non-zero 4 digit number..."<<endl;
     cin>> initialdigit;
    
     firstinteger = atoi (initialdigit);
    
     if (firstinteger > 0)
        valid = true;
    
     while (valid)
        {
         if (initialdigit [0] < initialdigit [1])
           {
             tmp = initialdigit [0];
             initialdigit [0] = initialdigit [1];
             initialdigit [1] = tmp;
            }
        if (initialdigit [1] < initialdigit [2])
            {
              tmp = initialdigit [1];
              initialdigit [1] = initialdigit [2];
              initialdigit [2] = tmp;
            }
        if (initialdigit [2] < initialdigit [3])
           {
              tmp = initialdigit [2];
              initialdigit [2] = initialdigit [3];
              initialdigit [3] = tmp;
            }
    
             }
    valid = false;
    
    cout<<firstinteger<<endl;
    
    
          system("PAUSE");
          return 0;
    }
    thanks guys...

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Lightbulb free knowledge.

    The FAQ has a wicked awesome example of 'int to string' (and string to int) converstions.

    Definately gotta check it out yo.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    i think my main problem here is getting the compiler to arrange the 4 digit number into descending order and then storing that string into a character... after i do that, i'll be gravy... SOMEONE HELP!!! I CAN'T FIND ANYTHING ANYWHERE ON ARRANGING CHARACTERS WITHIN A STRING....

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Woh. That IS a really wicked example, I was expecting itoa() or something.

    bcianfrocca:
    I remember another post like this one. But since you've shown some work this time, I'll try and help.

    I see you've got the number in string form already, so you can proceed to sort the individual digits in the string, which is the part you're having trouble on I'm assuming. It looks like you're trying to sort the string by a swapping method (bubble sort? Can't think of the name), but here's an easier method you can use:
    1) Create a second string.
    2) Find the lowest digit in the original string (converted from the int).
    3) Copy the digit to the first position in the second string.
    4) Find the next lowest digit in the original string.
    5) Copy this digit to the next position in the second string.
    6) Repeat 4 and 5 until you have the entire string sorted in lowest to highest order.

    Since you know the number of digits there are, you can just do a (nested) for loop instead of playing with tricky 'valid' control variables. One for loop just controls the number of digits you copy into the second string, and another loop inside of it helps you to find the lowest number in the original string.

    Hope this helps!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    I change some statements, when input '1234', output is '2341',
    not correct, how to decrease 'if' code?
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <iomanip>
    #include <cstring>
    using namespace std;
    
    int main()
    {
    bool valid = false;
    char initialdigit [5];
    int x;
    int tmp;
    int firstinteger;
    
     cout<<"Please enter a non-zero 4 digit number..."<<endl;
     cin>> initialdigit;
    
     firstinteger = atoi (initialdigit);
    
     if (firstinteger > 0)
        valid = true;
    
     while (valid)
        {
         if (initialdigit [0] < initialdigit [1])
           {
             tmp = initialdigit [0];
             initialdigit [0] = initialdigit [1];
             initialdigit [1] = tmp;
            }
        if (initialdigit [1] < initialdigit [2])
            {
              tmp = initialdigit [1];
              initialdigit [1] = initialdigit [2];
              initialdigit [2] = tmp;
            }
        if (initialdigit [2] < initialdigit [3])
           {
              tmp = initialdigit [2];
              initialdigit [2] = initialdigit [3];
              initialdigit [3] = tmp;
            }
    
             
        valid = false;
         }
    
    //cout<<firstinteger<<endl;
    cout<<initialdigit [0]<<initialdigit [1]
        <<initialdigit [2]<<initialdigit [3]<<endl;
    
          system("PAUSE");
          return 0;
    }

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Sorting is very easy to do:

    Code:
    #include <algorithm>
    #include <functional>
    ...
    //Sort first 4 characters of initialdigit array in descending order
    std::sort(initialdigit,initialdigit+4,std::greater<char>());
    BTW, the stdlib.h header is deprecated, the new version of that header is cstdlib
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    toysoldier: if you want to learn what's wrong with your code review what happens with the while() loop. Since valid is assigned false after the first time through the loop, the loop will stop after sorting just the first value. You need to keep going after the first element is sorted somehow. To use the swap technique like you have it, probably the easiest way is to use two loops that are nested. The outer loop will keep track of how many times to do a series of comparisons. The inner loop will keep track of which comparison you are doing. The ifs can be reduced to just one comparison that is an abstract comparison rather than a hard coded comparison like you use. In general, this technique is called a basic bubble sort. There are lots of variations that can be used to optimize the basic version, so develop/use one that you can understand. There are a number of posts about bubble sorts on the board and I'm sure you can find lots of references by "googling" as well.

    Of course, as hk_mp5kpdw idicated, you can always use the generic sort from the STL algorhithm header file if this isn't some form of homework and you just want something that works whether you understand it or not.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    5

    i think it is suit for you

    I think It may help you:
    - There are a big difference between number and number in character.
    - There are a big difference between string terminated by zero and a character.


    Code:
    //sort number
    //mybe algorithm is Bubble Sort
    //a bruce force algorithm
    //optimized for check input buffer
    #include <iostream>
    using namespace std;
    //convert from a character to a number
    int chartoint(char c)
    	{
    		char buffer[2];
    		buffer[1]=0;//string need to be terminated by ZERO
    		buffer[0]=c;
    		return atoi(buffer);
    	}
    //use to swap
    void c_swap(char &a, char &b)
    	{
    		char buffer;
    		buffer=a;
    		a=b;
    		b=buffer;
    	}
    //check "number" is number or not
    bool checknumberstring(char*s)
    	{
    
    		int l = strlen(s);
    		for (int i=0; i<l+1;i++)
    				if (s[i]<'0' && s[i]>'9')
    					return false;
    		return true;
    	}
    //main here
    
    #define	NUMBER	10
    
    int main()
    
    	{
    	char initialdigit [NUMBER];
    	unsigned int firstinteger;
    	cout<<"Please enter a non-zero digit number..."<<endl;
    	fgets(initialdigit, NUMBER, stdin);
    
    // check input data here
    
    	if (strlen(initialdigit)!=NUMBER-1 || checknumberstring(initialdigit)==false) 
    		{
    			cout << "Error! Input..." << endl;
    			system("PAUSE");
    			return 1;
    		}
    	firstinteger = atoi (initialdigit);
    	if (firstinteger > 0)
    		{
    		for (int i=0; i<NUMBER-1; i++)
    			for (int j=0; j<NUMBER-1; j++)
    				if (chartoint(initialdigit [j]) < chartoint(initialdigit [j+1]))
    					c_swap(initialdigit [j], initialdigit [j+1]);
    		}
    	cout<<initialdigit<<endl;
    	system("PAUSE");
    	return 0;
    	}

  9. #9
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    well use 'sstream' would be a easy way.

    blow me ... ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  3. Need Help with Inheritance assignment
    By mejv3 in forum C++ Programming
    Replies: 6
    Last Post: 11-08-2005, 12:56 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM