VB icon

Check IP Vaild

Email
Submitted on: 11/4/2016 11:09:37 PM
By: Ben  
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general), Microsoft Visual C++
Views: 2175
 
     This project checks if an IPv4 address is in the correct and informs you of an incorrect IP format. hope it helps someone.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Check IP Vaild
// Description:This project checks if an IPv4 address is in the correct and informs you of an incorrect IP format. hope it helps someone.
// By: Ben
//**************************************

// Program to check if a IP address is vaild.
// By DreamVB 21:04 04/11/2016
#include <iostream>
#include <string>
using namespace std;
using std::cout;
using std::endl;
bool IsIPGood(string src){
	int i = 0;
	int j = 0;
	int num = 0;
	string Parts[4];
	string buffer = "";
	int cnt = 0;
	bool IsGood = true;
	while (i < src.length()){
		//Check for .
		if (src[i] == '.'){
			if (cnt > 2){
				IsGood = false;
				break;
			}
			//Check buffer length
			if (buffer.length() == 0){
				IsGood = false;
				break;
			}
			else{
				Parts[cnt] = buffer;
				buffer.clear();
				cnt++;
			}
		}
		if (src[i] != '.'){
			//Build string
			buffer += src[i];
		}
		i++;
	}
	if (buffer.length() == 0){
		IsGood = false;
	}
	else{
		Parts[cnt] = buffer;
		buffer.clear();
	}
	if (cnt != 3){
		IsGood = false;
	}
	//Check for vaild digits
	i = 0;
	j = 0;
	while (i <= cnt){
		//Check for only digits.
		for (j = 0; j < Parts[i].length(); j++){
			if (!isdigit(Parts[i][j])){
				IsGood = false;
				break;
			}
		}
		//Get number from string
		num = 0;
		for (j = 0; j < Parts[i].length(); j++){
			num = num * 10 + Parts[i][j] - '0';
		}
		//Check for vaild range
		if (num < 0 | num>255){
			IsGood = false;
			break;
		}
		i++;
	}
	return IsGood;
}
int main(int argc, char *argv[]){
	char c = 'Y';
	string sIp = "";
	while (c == 'Y'){
		cout << "Enter a IP address /> ";
		cin.ignore();
		getline(cin,sIp);
		if (!IsIPGood(sIp)){
			cout << "The IP address entered is invalid." << endl;
		}
		else{
			cout << "The IP address is valid." << endl << endl;
		}
		cout << "Do you want to check another y/n : ";
		cin >> c;
		if (c > 97){
			c = (char)(c - 32);
		}
		//Check choice
		if (c == 'N'){
			cout << "Thanks for using the program." << endl;
		}
		else{
			if (c != 'Y'){
				cout << "Invalid choice '" << c << "' entered." << endl;
				break;
			}
		}
	}
	system("pause");
	return 0;
}


Other 50 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.