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

    Join Date
    Mar 2006
    Posts
    58
    Rep Power
    9

    Adding '0' to argv[1]


    I'M trying to add '0's to inputNumber until inputNumber becomes >= 3 or becoms a factor of 3 (evenly diveded by 3). So if you input 1 inputNumber should become "001", if you input 12 inputNumber should become "012", and if you were to input 1234 inputNumber should become "012345" and if you were to input 123 the program should simply continue. The problem I'M having is in the while loop. Any ideas what I'M doing wrong?

    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];
    
    		// insert the char elements from argv[1] into the elements of inputNumber
    		strcpy(inputNumber, argv[1]);
    
    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[backwardCounter] = '0';
    				//strcpy(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,642
    Rep Power
    4247
    I think you have two different problems going here. 12 is divisible by 3 whether you write it as 12 or 012. Similarly, 1 is not divisible by 3 whether you write it as 1 or 001.

    With that said, if you're simply trying to left pad by adding 0s, the best way to do this is to use the iomanip functions (setw() and setfill() ) to do proper padding.
    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
    58
    Rep Power
    9

    strlen


    In this program it wouldn't be 12 divided by 3 it would be 2 divided by 3 because 12 is two digits in length. Likewise it wouldn't be 012 divided by 3 it would be 3 divided by 3. I'M using strlen() to get the length of the sting to count against.


    Originally Posted by Scorpions4ever
    I think you have two different problems going here. 12 is divisible by 3 whether you write it as 12 or 012. Similarly, 1 is not divisible by 3 whether you write it as 1 or 001.

    With that said, if you're simply trying to left pad by adding 0s, the best way to do this is to use the iomanip functions (setw() and setfill() ) to do proper padding.

IMN logo majestic logo threadwatch logo seochat tools logo