question (c++)

Open discussion about any topic, as long as you abide by the rules of course!
Post Reply
bork[e]
Posts: 4357
Joined: Tue Mar 23, 2004 8:00 am

question (c++)

Post by bork[e] »

Ran into another problem, three functions deep into this and can't find a way out. I was thinking a for loop would be the solution, but now that I am trying it I guess not.

What I have is a set of 4 variables. oc1, oc2, oc3 and oc4. They hold the value of an IP address that will be subnetted. The variable oc4 is the fourth octet of the address :p , and is to be incremented by another number until it is equal to or less than 255.

My problem is I don't understand how I can increment that variable (oc4) using a loop until it reaches the requirements stated above. Maybe I don't use a loop, or use something other than a for... but I'm lost atm...Any help?

This will be a subnetting calculator btw...if I can get past this.
Fender
Posts: 5876
Joined: Sun Jan 14, 2001 8:00 am

Post by Fender »

You must not be describing the problem fully because this is trivial. Either that or I'm not understanding you.

Code: Select all

while(oc4 <= 255) 
    oc4 += increment;
}
Fender
Posts: 5876
Joined: Sun Jan 14, 2001 8:00 am

Post by Fender »

Code: Select all

for( ; oc4 <= 255; oc4 += increment);
mjrpes
Posts: 4980
Joined: Tue Nov 28, 2000 8:00 am

Post by mjrpes »

I can't figure out what you're trying to do.

oc4 is incremented by another number... is this other number static or changing?

does oc4 start at 0?

so the only requirement is to increment oc4 by another number until it is equal or less than 255?

something like...

Code: Select all


int anotherNumber = 7;
int oc4 = 0;

while (oc4 <= 255 - anotherNumber)
    oc4 += anotherNumber;

but then why would you want to increment? you could just say:

Code: Select all

int divisions = 255 / anotherNumber;
int oc4 = divisions * anotherNumber;
mjrpes
Posts: 4980
Joined: Tue Nov 28, 2000 8:00 am

Post by mjrpes »

funny how this topic gets nothing for an hour and we both reply at nearly the same time :)
Fender
Posts: 5876
Joined: Sun Jan 14, 2001 8:00 am

Post by Fender »

homework3world
your solution is waiting
bork[e]
Posts: 4357
Joined: Tue Mar 23, 2004 8:00 am

Post by bork[e] »

Code: Select all

void calculate()
{
    system("CLS");
    cout << "How many bits are being borrowed?\n";
    cin >> bitsB;
    Exp = bitsB;

    TSub = pow(2, bitsB);
    VSub = TSub - 2;
    
    cout << "There will be " << TSub << " totals subnets.\n";
    cout << "There will be " << VSub << " valid subnets.\n";
    
    system("PAUSE");   
    listSubnts();
}

//************//
// listSubnts //
//************//
void listSubnts()
{
    cout << "The first and last subnet can't be used.\n";
    cout << "The first is the wire address, and the last is the broadcast address\n\n";
    
    cout << oc1 << "." << oc2 << "." << oc3 << "." << "0\n";
}
Maybe that can explain better, though I don't think so. I'm not great at this stuff, only know a few commands and the such but I like learning all this.

Now let me explain what I am doing a little better.

I'll just say the number of bits that are being borrowed is 4.

So, the Increment is 2^4.

The Ip is going to be 192.168.10.5
The count starts at 192.168.10.0 and 16 is added to that last octet until hits 255.

In the code above you can see where I started the layout of the 192.168.10.0 but don't know where to go after that.



Maybe that will explain the questions you had?

edit: I'll explain the variables in a sec...got to run into town.
Last edited by bork[e] on Fri Apr 08, 2005 1:52 am, edited 1 time in total.
bork[e]
Posts: 4357
Joined: Tue Mar 23, 2004 8:00 am

Post by bork[e] »

Fender wrote:homework3world
your solution is waiting
Also, this isn't homework. We are doing this stuff in my Cisco class, the subneting part. And I thought it would be cool to see how much I remembered from my C++ class. Though not much. :(
mjrpes
Posts: 4980
Joined: Tue Nov 28, 2000 8:00 am

Post by mjrpes »

something like this?

Code: Select all

//************//
// listSubnts //
//************//
void listSubnts()
{
    cout << "The first and last subnet can't be used.\n";
    cout << "The first is the wire address, and the last is the broadcast address\n\n";
  
    for (int i = 0; i < 255; i += 16)
        cout << oc1 << "." << oc2 << "." << oc3 << "." << i << "\n";
} 
Post Reply