'How to make a char equal to user input?
For example if the char was 4 blank spaces
_ _ _ _
_ _ _ _
Then the user enters 1234, I want it to look like
1 2 3 4
_ _ _ _
I have tried things like
grid[1][1] = let1; // let1 is the variable of the first number the user inputs, it's an int
but it's not working. Do you have any ideas on how I can do this?
Solution 1:[1]
Edited Original content of question has changed significantly since first posting this. Therefore a disclaimer:
This answer applies to the Title question and original content of question plus the section in the first `else if` statement to address latest OP edit.
One way can use command line arguments (requires using C signature int main(int argc, char *argv[]);) and then enter the digits 1 2 3 4 with space just as you have illustrated, then convert them in the following ways using format specifiers and in one case using a string to number conversion function:
Compile the following code into an exe.
int main(int argc, char *argv[])
{
if(argc == 1)
{
printf("%c %c %c %c\n", '_','_','_','_');
printf("%c %c %c %c\n", '_','_','_','_');
}
else if(argc == 2 && strlen(argv[1]) == 4)
{
//if entering 1234 and need output 1 2 3 4
printf("%c %c %c %c\n", argv[1][0], argv[1][1], argv[1][2], argv[1][3]);
printf("%c %c %c %c\n", '_','_','_','_');
}
else if(argc == 5)
{
//three options: char, string, number
printf("%c %c %c %c\n", argv[1][0], argv[2][0], argv[3][0], argv[4][0]);//print char
printf("%c %c %c %c\n", '_','_','_','_');
printf("%s %s %s %s\n", argv[1], argv[2], argv[3], argv[4]);//print pointer to char (string)
printf("%c %c %c %c\n", '_','_','_','_');
printf("%d %d %d %d\n", atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));//print integer
printf("%c %c %c %c\n", '_','_','_','_');
}
else printf("Wrong number of inputs. Try again\n");
getchar();
return 0;
}
On command line enter
program.exe 1 2 3 4 //using the real name of your program
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
