Click here to Skip to main content
16,019,619 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to assign a 4 digit number to array no should be of 4 digit only and it is not then ignore number and again take the input

[edit]Code block added - OriginalGriff[/edit]

What I have tried:

C++
#include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int a[5]; //array
        int i=0;
        int temp,rem=0;
        for(i=0;i<5;i++)
        {
            scanf("%d",&a[i]); //taking input from user
            while(a[i]/10!=0)
            {
                    rem=a%10;  //condition check but this part is not working
                    temp++;   //increasing temp one by one
                    a[i]=a[i]/10; //dividing the no by 10
            }
    
                if(temp==4)
                {
                        printf("no is acceptable "); //if digit is of 4 then no //is acceptable
                        temp=0;
                }
                else
                {
                    printf("please again enter the 4 digit no");
                    scanf("%d",&a[i]); //again scan the no and again check the //condition
                    while(a[i]/10!=0)
                    {
                            rem=a%10;
                            temp++;
                            a[i]=a[i]/10;
                    }
    
                        if(temp==4)
                        {
                                printf("no is acceptable "); //if count is 4 //then only no is acceptable
                                temp=0;
                        }
                }
        }
        for(i=0;i<5;i++)
        {
            printf("%d",a[i]); //printing the no
        }
        return 0;
    }
Posted
Updated 19-Feb-17 1:16am
v2

Why are you checking the number has four digits with modulus in a loop?
C++
if (a[i] >= 1000 && a[i] <= 9999)
   {
   // It has four digits
   ...
   }
 
Share this answer
 
v2
Comments
nikhil arora 19-Feb-17 5:36am    
thanks! alot
OriginalGriff 19-Feb-17 5:41am    
You're welcome!
Your code look complicated !
Any integer with 4 digits is between 1000 and 9999.
C++
if (a[i]>=1000 && a[i]<=9999) {
    ...
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900