Thread: Taking a variable and input each value of it into an array

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    8

    Taking a variable and input each value of it into an array

    Hi guys, I am new to programming and am quite enjoying it. I have been given a project where i need to write a program that asks for a 4 digit input from the user and then sorts out how many thousands, hundreds, tens and ones make up that number. This is what i have written so far but it is not working. I was wondering if anyone could help.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int i = 0;
    int main()
    {
        int sortingstring[4];
        printf("Please enter a 4 digit number\n");
    
    for (i=0;i<3;i++);
    	{
    		scanf("%d", &sortingstring[i]);
    		
    	}    
            
        for ( i = 0; i < 3; ++i );
    {
            if (i==0);
            {
                  printf("\n There are %d thousands in your number\n", sortingstring[0]);
                  }
            if (i==1);
            {
                  printf("\n There are %d hundreds in your number\n", sortingstring[1]);
                  }
            if (i==2);
            {
                  printf("\n There are %d tens in your number\n", sortingstring[2]); 
                  }
            if (i==3);
            {
                  printf("\n There are %d ones in your number\n\n", sortingstring[3]); 
                  }
    }
    
        system("PAUSE"); 
        return 0;
        
    }
    Thanks alot

    Sam

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1) You don't need to iterate 4 times and print the same things 4 times. You don't need to iterate at all you have all the information you need in sortingstring[0], [1], [2], [3]

    2) A semicolon after an if statement makes the if not do anything. Furthermore the ifs are completely irrelevant just like the last for loop.

    3) What happens if the user enters a number larger than 9 for a digit?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    8
    so i don't need any of the if statements of the for loops. I have removed them and this is what it looks like but it still doesn't work

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int i = 0;
    int main()
    {
        int sortingstring[4];
        printf("Please enter a 4 digit number\n");
        scanf("%d", sortingstring);
    		  
    
                  printf("\n There are %d thousands in your number\n", sortingstring[0]);
    
                  printf("\n There are %d hundreds in your number\n", sortingstring[1]);
    
                  printf("\n There are %d tens in your number\n", sortingstring[2]); 
    
                  printf("\n There are %d ones in your number\n\n", sortingstring[3]); 
    
    
        system("PAUSE"); 
        return 0;
        
    }
    Cheers
    Sam

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You should have kept the first for loop. Now you are not reading anything in, you are attempting to read a pointer to your array. Your compiler should be screaming at you and you should take note of what it says.

    Still the question remains: How can you be sure that when you ask me to type in digits I won't type in 99 as the last digit?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    8
    I realized that after i posted it haha. I have put it back in and still have the same problem. The compiler didn't do anything it ran it but with no success. this is what the question asks. At the moment i am just trying to get it to work when you input 4 numbers. i will have to edit it so it will work out numbers that only contain 1,2 or 3 numbers.

    Design and code a program that will take in an integer number from the user. This number has to be between 1 and 9999 (inclusive). This number will then be analysed by your code as to how many 1000s, 100s, 10s, and 1s make up this number. For example, an entry of 4208 by the user should make your program output something like: 4298 is made up of 4 lots of 1000, 2 lots of 100, 0 lots of 10s, and 8 lots of 1s.
    So 4208 = 4 x 1000 + 2 x 100 + 0 x 10 + 8 x 1
    Another example is 233. The output should be something like “0 lots of 1000, 2 lots of 100, 3 lots of 10s, and 3 lots of 1s”.

    Cheers
    Sam

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-08-2011, 11:22 AM
  2. how to avoid taking in a variable
    By bijan311 in forum C++ Programming
    Replies: 3
    Last Post: 12-19-2009, 03:47 AM
  3. Help with taking input from a file
    By babe20042004 in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2009, 08:25 AM
  4. Taking input in C
    By GUIPenguin in forum C Programming
    Replies: 1
    Last Post: 04-12-2006, 01:53 PM
  5. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 01:50 AM