#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]


    In the program below I'M trying to add '0' to inputNumber which is a copy of argv[1]. I want to make sure that the number of characters in inputNumber is a factor of 3 (evenly divided by 3). Note that I am no concerned with the value in inputNumber just the number of characters. If the user enters 1 the program should turn inputNumber into "001", if the user enters 12 the program should turn inputNumber into "012", and if the user enters 1234 the program should turn inputNumber into "001234". Right now I'M currently trying to use strcpy but with no luck. I also tried to use a while loop that I have now commented out because I couldn't get the code right. Also, I'M trying to do this by simply malipulating the array inputNumber, I would like to avoid any fancy functions that I don't even know about or how to use.

    Here's the code
    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)
    	{
    		// pad inputNumber with at leat 1 zero and no more than 2
    		char inputNumber[13] = {'0','0'};
    
    		// 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;
    std::cout << "The streng length is: " << strlen(inputNumber) << std::endl;
    
    	}
    	else
    	{
    std::cout << "Number is evenly divisible by 3 " << std::endl;
    	}
    
    	return 0;
    }
    /*END*/
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,407
    Rep Power
    1871
    It was 2 weeks ago, and the code was the same.
    Code:
    $ diff left_cmp.c right_cmp.c
    10c10,11
    <     char inputNumber[13];
    ---
    >     // pad inputNumber with at leat 1 zero and no more than 2
    >     char inputNumber[13] = {'0','0'};
    15c16
    < std::cout << "The inputNumber variable is :" << inputNumber << std::endl;
    ---
    > std::cout << "The inputNumber variable is: " << inputNumber << std::endl;
    18c19
    <     int backwardCounter = strlen(inputNumber);
    ---
    >     /*int backwardCounter = strlen(inputNumber);
    31c32
    <     }
    ---
    >     }*/
    33c34,35
    <     std::cout << inputNumber << std::endl;
    ---
    > std::cout << inputNumber << std::endl;
    > std::cout << "The streng length is: " << strlen(inputNumber) << std::endl;
    42a45
    > /*END*/
    > I would like to avoid any fancy functions that I don't even know about or how to use.
    If you're never going to challenge yourself to learn something new, you should give up programming.
    You're basically just telling us "I've learnt enough to write 'hello world' and that's all I'm interested in".

    Here, challenge yourself to figure these out.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    string foo ( string answer ) {
      while ( answer.length() % 3 != 0 ) {
        answer = "0" + answer;
      }
      return answer;
    }
    string bar ( string answer ) {
      string pad("000");
      if ( answer.length() % 3 != 0 ) {
        answer = pad.substr(0, 3 - answer.length() % 3) + answer;
      }
      return answer;
    }
    
    int main ( int argc, char *argv[] ) {
      string answer(argv[1]);
      cout << "Results = " << foo(answer) << " " << bar(answer) << endl;
    }
    
    
    $ g++ -g bar.cpp
    $ ./a.out 1
    Results = 001 001
    $ ./a.out 12
    Results = 012 012
    $ ./a.out 123
    Results = 123 123
    $ ./a.out 1234
    Results = 001234 001234
    $ ./a.out 12345
    Results = 012345 012345
    $ ./a.out 123456
    Results = 123456 123456
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo