-1

I need to make a program where the user types numbers( such as 193643, in one line). Then have each number (such as"1" "9" "3" etc) turned in to its own variable.

how do i do it?

I haven't attempted as i dont get it, I will try my best to explain.

similar to...

 printf("Please enter three numbers: ");
 scanf("%d",&number1);

The user types 137

Instead of entering 137 and having that become the variable. I need to make it so that If the person enters a number like 137, The program takes "1" as a variable, "3" as another variable and "7" as the last variable. It must be in one line...

SO instead of asking three times as so....

 printf("Please enter 1st numbers: ");
 scanf("%d",&number1);
 printf("Please enter 2nd numbers: ");
 scanf("%d",&number2);
 printf("Please enter 3rd numbers: ");
 scanf("%d",&number3);

have it ask in one line, one time and record each number as a variable...

Adeel Anwar
  • 729
  • 1
  • 5
  • 8
  • 1
    One, it's unclear what you're asking, two, you need to show what you have tried. – djechlin Feb 12 '13 at 16:45
  • What have you tried so far? Generally these types of questions ("please show me how to do it") don't get answers without first showing that you have put some effort into it. – Brett McCann Feb 12 '13 at 16:47
  • And what would the variables be named? – Hot Licks Feb 12 '13 at 16:48
  • You want to store the ascii values of the digits typed, or the values they represent? And what variables are you storing them in to? some array? – Mike Feb 12 '13 at 16:49
  • I would probably put the values in an _array_ of `char` or `int`. Since this is a pretty basic `C` concept you might be interested in a [good `C` book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to learn the fundamentals. – Blastfurnace Feb 12 '13 at 16:52
  • @Adeel Anwar Read [this](http://stackoverflow.com/questions/12618675/dynamically-declare-variables-structure-in-c) –  Feb 12 '13 at 16:52
  • In a loop store the values into an incrementing array, use `getchar()` for the numbers, subtract `'0'` from each value that you get. (don't forget to discard the `'\n'` if you're going to get more than 3 values) – Mike Feb 12 '13 at 16:56
  • 1
    Really should be reopened. With the updates it becomes a valid question. – Mike Feb 12 '13 at 18:07
  • 1
    @Mike - It seems to me that this is (as updated) a very simplistic question that exhibits no comprehension of even character arrays in C. The OP would be better advised to spend some more time studying his C textbook. – Hot Licks Feb 12 '13 at 18:31
  • 1
    @HotLicks - I do not argue that to you, I, or anyone with C experience that this is a simplistic question. However I submit that nowhere in the FAQ does it say you can't ask a simplistic question, just that it needs to be well formatted, on topic, and preferably have some code in it. Sometimes individuals can explain things better than text books. – Mike Feb 12 '13 at 18:39

2 Answers2

0

If I understand your question, you are trying to extract every number and store it into a variable. A small example is as below

#include <stdio.h> 
#include <string.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{ 
    char inpString[20];
    int num;

    strcpy(inpString, argv[1]);
    num = atoi(inpString);
    printf("Number: %d\n", num);

    while(num > 0) 
    {
        printf("%d\n", (num % 10));
        num = num / 10;
    }
    printf("Size of void *: %d\n", sizeof(void *));

return 0;
}
Ganesh
  • 5,880
  • 2
  • 36
  • 54
0

I figured it out thanks everyone! it just required some simple problem solving, I will share just in case some one else is trying to figure it out as well....

Note: separate is just a variable i created to separate each number one by one. I made all the variables integers.

separate = 137 for this example.

lastDigit = separate % 10;
separate = separate / 10; 
secondDigit = separate % 10; 
separate = separate / 10; 
firstDigit = separate % 10; 

7 is stored in lastDigit. Then 7 is shaved off from separate variable (137 to 13).

3 is stored in secondDigit. Then 3 is shaved off from separate variable (13 to 1).

1 is stored in firstDigit.

Adeel Anwar
  • 729
  • 1
  • 5
  • 8