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.
#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;
}