Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In c++ i want to get in cnic number...if i enter more then 13 number.it have to show a message that number of integers is more the given space.


#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
	
	int cnic,a=0;
	
	while (cnic[a]!='/0')
	{
	cin>>cnic;
	a++;
	}
	if(a==12)
	cout<<"valid";
	else 
	cout<<"Invalid";
	return 0;
}


What I have tried:

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
	
	int cnic,a=0;
	
	while (cnic[a]!='/0')
	{
	cin>>cnic;
	a++;
	}
	if(a==12)
	cout<<"valid";
	else 
	cout<<"Invalid";
	return 0;
}
Posted
Updated 20-Mar-21 0:50am

1 solution

You need to be using array instead of a value type variable,
C++
int cnic,a=0;
That will be changed to,
C++
int cnic[13];
int a = 0;
This will be a container, that can contain 13 values. And also, you might want to take a look at do...while, instead of while loops as I believe that is what you are looking for, as your code won't work as you are expecting it to work. And moreover, '\0' is what you are looking for, not '/0'. :laugh:

A simple tip would be, consider using a for(;;) loop instead of this while loop.

So much to learn brother, please follow the following links and explore a bit more of C++,
Arrays - C++ Tutorials[^]
loops - The difference between while and do while C++? - Stack Overflow[^]
c# - 'do...while' vs. 'while' - Stack Overflow[^]
 
Share this answer
 
Comments
Richard MacCutchan 25-Nov-18 10:09am    
I think it would be better to declare cnic as char type for this problem.
Afzaal Ahmad Zeeshan 25-Nov-18 11:34am    
Yes, but mostly here we store 13 digit values for the CNIC which are numbers. The dashes are placed afterwards, as a mask. So that is why I did not pay much attention to that part. A normal CNIC over here is 12345-1234567-1.

But yes you are right, it could be character data type, and 15 length.
Richard MacCutchan 25-Nov-18 12:45pm    
It just occurred to me (after reading the Wikipedia entry for CNIC) that storing as char type would make it easier to split out the two major fields and convert them to their numeric values, or compare them against whatever rules are in force.
[no name] 22-Mar-21 5:44am    
sir! how can i add cnic number in c++ for ATM machine?
[no name] 22-Mar-21 3:45am    
How can i add cnic number in c++ program to ATM ? Anyone plz help me

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