| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
General - Help me i dont know how to reduce the code
This is an program to convert an ip address to binairy, but the code is a bit large for that every thing is times 4, i tryed arrays but wouldn't work for me.
can some one help me. #include <iostream> #include <string> using namespace std; int main(void) { //Declerations for the strings and integers. //Because ip address work on 8 bit system. int bit[8] = {128, 64, 32, 16, 8, 4, 2, 1}; //The numbers used for increasing its binairy int binairy[8] = {10000000, 1000000, 100000, 10000, 1000, 100, 10, 1}; //Containers for the decimal and binairy conversion int ipsec1, ipsec2, ipsec3, ipsec4; int binsec1, binsec2, binsec3, binsec4; //a stop so you can see the result int test; //4 strings so the binairy can be shown correct as 8 bit string bintext1, bintext2, bintext3, bintext4; //a few assignings because it wouldn't work without it :S binsec1 = 0; binsec2 = 0; binsec3 = 0; binsec4 = 0; cout << "This is an ip to binairy converter.\n"; cout << "Please input the ip in 4 sections.\n"; //Here comes the input for the ip address in 4 chunks start: cout << "Please input section: "; cin >> ipsec1; cout << "Please input section: "; cin >> ipsec2; cout << "Please input section: "; cin >> ipsec3; cout << "Please input section: "; cin >> ipsec4; //This is the routine for the whole thing and some error checking sec1: if (ipsec1 == 0) { binsec1 = 0; goto sec2; } if (ipsec1 > 255 || ipsec1 < 1) { system("cls"); cout << "Invalid input, please try again.\n"; goto sec1; } //This is the math system for converting it for (int t=0; t < 8; t++) { if (ipsec1 >= bit[t]) { ipsec1 = ipsec1 - bit[t]; binsec1 = binsec1 + binairy[t]; } } //the same but just for the 2de chunk sec2: if (ipsec2 == 0) { binsec2 = 0; goto sec3; } if (ipsec2 > 255 || ipsec2 < 1) { system("cls"); cout << "Invalid input, please try again.\n"; goto sec2; } for (int t=0; t < 8; t++) { if (ipsec2 >= bit[t]) { ipsec2 = ipsec2 - bit[t]; binsec2 = binsec2 + binairy[t]; } } //the same but for the 3de chunk sec3: if (ipsec3 == 0) { binsec3 = 0; goto sec4; } if (ipsec3 > 255 || ipsec3 < 1) { system("cls"); cout << "Invalid input, please try again.\n"; goto sec3; } for (int t=0; t < 8; t++) { if (ipsec3 >= bit[t]) { ipsec3 = ipsec3 - bit[t]; binsec3 = binsec3 + binairy[t]; } } //the same but for the 4th chunk sec4: if (ipsec4 == 0) { binsec4 = 0; goto end; } if (ipsec4 > 255 || ipsec4 < 1) { system("cls"); cout << "Invalid input, please try again.\n"; goto sec4; } for (int t=0; t < 8; t++) { if (ipsec4 >= bit[t]) { ipsec4 = ipsec4 - bit[t]; binsec4 = binsec4 + binairy[t]; } } end: // check and add of 0's if needed so it would be shown correct for instead 10 it is shown as 00000010 if (binsec1 >= 1000000 && binsec1 <= 1111111) bintext1 = "0"; if (binsec1 >= 100000 && binsec1 <= 111111) bintext1 = "00"; if (binsec1 >= 10000 && binsec1 <= 11111) bintext1 = "000"; if (binsec1 >= 1000 && binsec1 <= 1111) bintext1 = "0000"; if (binsec1 >= 100 && binsec1 <= 111) bintext1 = "00000"; if (binsec1 >= 10 && binsec1 <= 11) bintext1 = "000000"; if (binsec1 == 1) bintext1 = "0000000"; if (binsec1 == 0) bintext1 = "00000000"; //the same but for the next chunk if (binsec2 >= 1000000 && binsec2 <= 1111111) bintext2 = "0"; if (binsec2 >= 100000 && binsec2 <= 111111) bintext2 = "00"; if (binsec2 >= 10000 && binsec2 <= 11111) bintext2 = "000"; if (binsec2 >= 1000 && binsec2 <= 1111) bintext2 = "0000"; if (binsec2 >= 100 && binsec2 <= 111) bintext2 = "00000"; if (binsec2 >= 10 && binsec2 <= 11) bintext2 = "000000"; if (binsec2 == 1) bintext2 = "0000000"; if (binsec2 == 0) bintext2 = "00000000"; //the same but for the next chunk if (binsec3 >= 1000000 && binsec3 <= 1111111) bintext3 = "0"; if (binsec3 >= 100000 && binsec3 <= 111111) bintext3 = "00"; if (binsec3 >= 10000 && binsec3 <= 11111) bintext3 = "000"; if (binsec3 >= 1000 && binsec3 <= 1111) bintext3 = "0000"; if (binsec3 >= 100 && binsec3 <= 111) bintext3 = "00000"; if (binsec3 >= 10 && binsec3 <= 11) bintext3 = "000000"; if (binsec3 == 1) bintext3 = "0000000"; if (binsec3 == 0) bintext3 = "00000000"; //the same but for the next chunk if (binsec4 >= 1000000 && binsec4 <= 1111111) bintext4 = "0"; if (binsec4 >= 100000 && binsec4 <= 111111) bintext4 = "00"; if (binsec4 >= 10000 && binsec4 <= 11111) bintext4 = "000"; if (binsec4 >= 1000 && binsec4 <= 1111) bintext4 = "0000"; if (binsec4 >= 100 && binsec4 <= 111) bintext4 = "00000"; if (binsec4 >= 10 && binsec4 <= 11) bintext4 = "000000"; if (binsec4 == 1) bintext4 = "0000000"; if (binsec4 == 0) bintext4 = "00000000"; // the real printing as an whole cout << bintext1 << binsec1 << "." << bintext2 << binsec2 << "." << bintext3 << binsec3 << "." << bintext4 << binsec4 << "\n"; //and an pause so you could see the result cin >> test; return 0; //end } |
|
#2
|
|||||
|
|||||
|
cpp Code:
cin.fail() will be true if the user entered 14.a.51.61 - i.e. expecting a number but getting a letter. The ignore call could be replaced to verify that it is indeed the . it's supposed to be. The code is untested - it might not work, but it should at least be close. |
|
#3
|
|||
|
|||
|
explanation
Thanks for the short version of it, but can some one explain the code to me, and to be precise the part of ostringstream, and what it does and to use it, sorry i know but i'm still learning :P.
thx, for the reply |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > General - Help me i dont know how to reduce the code |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|