Thread: Groups of 3

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

    Join Date
    Mar 2006
    Posts
    49
    Rep Power
    9

    Groups of 3


    In the following program I'M trying to take a number no more than 12 digits long and make it a factor of 3.

    If the input is 1, the program would turn the number into 001.
    If the input is 12, the program would turn the number into 012.
    If the input is 123, the program would simple continue.

    The magic should be happening after the second while loop. Can anyone tell me what I'M doing wrong. When I input one digit I get the error "Segmentation fault (core dumped)", when I input two digits it seems to be replacing the first digit with a copy of the second, the only thing it does right is insert the '0' into position[0]. Code is below, thanks.

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string.h>
    #include "functions.h"
    
    int main(int argc, char *argv[])
    {
    	if(strlen(argv[1]) % 3 != 0 || strlen(argv[1]) < 3)
    	{
    		char inputNumber[13];
    		char *numberProcessor = inputNumber;
    
    		// insert the char elements from argv[1] into the elements of inputNumber
    		char *counter = argv[1];
    		while(*counter != '\0')
    		{
    			*numberProcessor = *counter;
    
    			counter++;
    			numberProcessor++;
    		}
    std::cout << "The inputNumber variable is :" << inputNumber << std::endl;
    
    		// move each elemtn to the right one space and insert a '0' as the first element of inputNumber until the condition fails.
    		int backwardCounter = strlen(inputNumber);
    		while(strlen(inputNumber) % 3 != 0 || strlen(inputNumber) < 3)
    		{
    			inputNumber[backwardCounter + 1] = inputNumber[backwardCounter];
    			backwardCounter--;
    
    			if(backwardCounter == 0)
    			{
    				inputNumber[0] = '0';
    			}
    		}
    
    		std::cout << inputNumber << std::endl;
    
    	}
    	else
    	{
    std::cout << "Number is evenly divisible by 3 " << std::endl;
    	}
    
    	return 0;
    }
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,595
    Rep Power
    4207
    If you want to simply add 0s in front as needed, you can use std::cout along with std::setfill and std::setw()
    e.g.
    Code:
    #include <iostream>
    #include <iomanip>
    
    std::cout << std::setfill('0') << std::setw(3) << number;
    setw() sets the width of the output field and setfill() sets the character to pad the extra space with.
    Last edited by Scorpions4ever; May 3rd, 2013 at 02:52 PM.
    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
    Mar 2006
    Posts
    49
    Rep Power
    9

    thanks but


    Thanks but I need to accomplish this using arrays & pointers. I'll have to look into that though.


    Originally Posted by Scorpions4ever
    If you want to simply add 0s in front as needed, you can use std::cout along with std::setfill and std::setw()
    e.g.
    Code:
    #include <iostream>
    #include <iomanip>
    
    std::cout << std::setfill('0') << std::setw(3) << number;
    setw() sets the width of the output field and setfill() sets the character to pad the extra space with.

IMN logo majestic logo threadwatch logo seochat tools logo