IP address incrementation

Hi all! I'm new to C++ and could use a little help with incrementing an IP address.
I'm building a VLSM calculator. The part im having trouble with is that if the 4th octet of an ip address is greater than 254, it sets the value of 4th octet to 0 and increments the 3rd octet by 1.

Any help would be great.
here is what i have so far...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//group 4 vlsm calc version 3.2
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
int netnum,oct1,oct2,oct3,oct4,mask;
string oct1s, oct2s, oct3s, oct4s;



//get user input
//example 192.168.1.0/24
cout << "\t\tWelcome to the VLSM pragram by Group4\n";
cout << "Enter Major Network IP Address (example 192.168.1.0/24)\n:";
getline(cin, oct1s, '.');
getline(cin, oct2s, '.');
getline(cin, oct3s, '.');
getline(cin, oct4s, '/');
cin >> mask;
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
//convert strint to int so that you can use it as a number
oct1 = atoi(oct1s.c_str());
oct2 = atoi(oct2s.c_str());
oct3 = atoi(oct3s.c_str());
oct4 = atoi(oct4s.c_str());

//get the number of nextworks
cout<<"\nEnter Number of Subnets : ";
cin>> netnum;

//Create the array with the size the user input
int *hostnum = new int[netnum];

//array for avalible host
int *allocatedSize = new int[netnum];

//array for subnetmask
int *subnetmask = new int[netnum];

//array for bitvalue2
int *bitvalue2 = new int[netnum];

//array for bitvalue3
int *bitvalue3 = new int[netnum];

//array for bitvalue4
int *bitvalue4 = new int[netnum];

//array for out octet 4
int *totalOct4 = new int [netnum];

//array for out octet 3
int *totalOct3 = new int[netnum];

//array for out octet 2
int *totalOct2 = new int[netnum];

//for loop array set to 0
for(int i=0;i<netnum;i++)
{
bitvalue2[i]=0;
bitvalue3[i]=0;
bitvalue4[i]=0;

totalOct2[i]=0;
totalOct3[i]=0;
totalOct4[i]=0;
}

//put the numbers of host in an array
cout << "Enter the number of host for each Network from greatest to least" << endl;

for(int i=0; i<netnum; i++)
{
double power=0.0;

cout << "Subnet: "<< 1+i <<" : ";
cin >> hostnum[i];

if ((hostnum[i] > 0) && (hostnum[i] <= 126))
{
//get the number we have to raise 2 to for a class C.
while (allocatedSize[i] < hostnum[i])
{
	allocatedSize[i]=pow(2.0,power)-2;
	bitvalue4[i]=pow(2.0,power);
	subnetmask[i]=32-power;
	power++;
}
}

else if ((hostnum[i] > 126) && (hostnum[i] <= 65534))
{
//get the number we have to raise 2 to for a class B.
while (allocatedSize[i] < hostnum[i])
{
	allocatedSize[i]=pow(2.0,power)-2;
	bitvalue3[i]=(pow(2.0,power)/256);
	subnetmask[i]=32-power;
	power++;
}
}

else if ((hostnum[i] > 65534) && (hostnum[i] < 16777214))
{
//get the number we have to raise 2 to for a class A.
while (allocatedSize[i] < hostnum[i])
{
	allocatedSize[i]=pow(2.0,power)-2;
	bitvalue2[i]=pow(2.0,power);
	subnetmask[i]=32-power;
	power++;
}
}

else
{
	cout << "\nRequested host number is way too high!!! What's wrong with you?" << endl;
}

}

//definition
double totalip2=0.0;
double totalip3=0.0;
double totalip4=0.0;
int lastip;
int fistip;

//Header
cout << "\n\tMajor Network IP Address: " << oct1 << "." << oct2 << "." << 
oct3 << "." << oct4 << "/" << mask << endl;
cout << "\n#    Host    Allocated \tNetwork Adrress \tAssignable Range" << endl;

//this for loop executes for every subnet
for(int i=0;i<netnum;i++)
{

//this adds together the ip per octet
totalip2 += bitvalue2[i];
totalip3 += bitvalue3[i];
totalip4 += bitvalue4[i];

//this is for the subnet. it displays the allocated amount of the first in the next ip address
totalOct2[i+1] = totalip2;
totalOct3[i+1] = totalip3;
totalOct4[i+1] = totalip4;

if (totalip4 > 254)
{
	bitvalue4[i]=0;
	totalOct3[i+1]++;
}

//this is for displaying ip ranges
lastip = totalOct4[i]+allocatedSize[i];
fistip=totalOct4[i]+1;

//subnet number starting at 1
cout << i+1 << "    " 
//number of host needed
<< hostnum[i] << "   \t"
//allocated Size
<< allocatedSize[i] << "   \t"
//address & mask
<< oct1 << "." << totalOct2[i] << "." << (totalOct3[i]) << "." << totalOct4[i] << "/" << subnetmask[i] << "   \t" <<endl<<endl;
////first Assignable range
//<< oct1 << "." << totalOct2[i] << "." << totalOct3[i] << "." << totalOct4[i] << " - "
////last Assignable range
//<< oct1 << "." << totalOct2[i] << "." << totalOct3[i] << "." << lastip << endl;
}

cin.ignore();
cin.ignore();
return 0;
}


I need help developing a good way to increment the ip addresses
I guess a better example of what i'm trying to get is...
for instance
Major IP address: 192.168.1.0
subnet mask /16

for 6 subnets
subnets 1=400 host
subnets 2=300
subnets 3=200
subnets 4=100
subnets 5=50
subnets 6=20

the IP address for those subnets should be...
address 1: 192.168.0.0
address 2: 192.168.2.0
address 3: 192.168.4.0
address 4: 192.168.5.0
address 5: 192.168.5.128
address 6: 192.168.5.192

I don't know how to increment from 4th octet to 3rd. I hope this help describe what im trying to do.


Topic archived. No new replies allowed.