C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
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  
Old March 28th, 2008, 02:29 PM
kiltux kiltux is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 2 kiltux User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 58 sec
Reputation Power: 0
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
}

Reply With Quote
  #2  
Old March 31st, 2008, 07:21 AM
MaHuJa MaHuJa is offline
Contributing User
Click here for more information.
 
Join Date: Dec 2007
Posts: 342 MaHuJa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 15 h 1 m 39 sec
Reputation Power: 1
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
cpp Code:
Original - cpp Code
  1. #include <string>
  2. #include <iostream>
  3. #include <sstream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     ostringstream s;
  10.  
  11.     cout << "Enter an IP address: ";
  12.     for (int i=1;i<=4;++i) {
  13.         unsigned int j;
  14.         cin >> j;
  15.         if (i!=4) cin.ignore()// ignore the .
  16.         if (j>255 || cin.fail()) { // handle error
  17.             cerr << "Input error";
  18.             return 1;
  19.         }
  20.         for (unsigned int k=128; k>0; k/=2) {
  21.             if (j>=k) {
  22.                 s << '1';
  23.                 j -= k;
  24.             } else
  25.             s << '0';
  26.         }
  27.         s << '.';
  28.     }
  29.     cout << "\nBinary is " << s.str();
  30.    
  31.     //system("pause");  //Uncomment on windows, if you need the program itself to keep the window open. Better yet; set up your environment to do it anyway.
  32.     return 0;
  33. }


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.

Reply With Quote
  #3  
Old March 31st, 2008, 08:49 AM
kiltux kiltux is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 2 kiltux User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 58 sec
Reputation Power: 0
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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > General - Help me i dont know how to reduce the code


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five &quot;checkpoints&quot; for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Iron Speed




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway