New and need help with strings

I'm in the process of writing a subnet calculator.
Without writing an 'if' statement that checks for a period at every likely position between possible subnet octets I want to write a for loop that can go through and find the periods, hopefully.

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
#include <iostream>
#include <iomanip>
using namespace std;
char ca, *cs;
int call(char ss[], int ii);
int main(void) {
	int mamx = 30, mbmx, mcmx;
	char fef[] = {""};
	cout << "Enter a subnet." << endl; 
        // e.g. "255.255.240.0" or "192.168.1.18 /24"
	cin >> setw(mamx) >> fef;
	mbmx = call(fef, mbmx);
	cout << "Subnet " << mbmx << " is..." << endl;
	return 0;
}

int call(char ss[], int ii) {
	cs = &ss[0];
				
	int io;
	io = ii + 3;
				
	for (ii = ii; /*ii < io,*/ *cs == '.'; *(cs++), ii++) {
		*cs = ss[ii];
		if (*cs == '.') {
			break;
		}
		if (ii == io) {
			break;
		}
	}
	return (ii);
}


'mbmx' isn't the value I want it to be.
I don't necessarily want the answer. Just some insight into where I should be going is all.
Last edited on
mbmx is uninitialized when it is passed to call on line 12.

On line 23. Is there a reason you write *(cs++) instead of just cs++?
Last edited on
cs++ wouldn't be the same as incrementing a constant (which isn't possible, for all I know)?

I'm going to be looking at mbmx, ty.

Ok, I'll update, line 18 and and set 'cs' to the address of 'ss'.
Last edited on
K, I've acquainted myself with the string library. I took the lack of replies to mean that my mistake was simple.. I think I've made great headway in comparison to my first post. Right now, setting the fourth octet to a variable (nhs) is causing me trouble. On line 19, I've tried setting the delimiter to the line terminator with no luck..

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
#include <iostream>
#include <iomanip>
#include <string.h>
#include <string>
using namespace std;

int main() {

	char *nes, *nfs, *ngs, *nhs; char S[] = { "" };	
	int sen, sfn, sgn, shn;
	cout << "Enter a subnet!" << endl;
	cin >> setw(30) >> S;
	nes = strtok( S, "." );
	cout << nes << endl;     //
	nfs = strtok ( NULL, "." );
	cout << nfs << endl;     //
	ngs = strtok ( NULL, "." );
	cout << ngs << endl;     //
	nhs = strtok ( NULL, "" );
	cout << nhs << endl;     // display value
	cin >> setw(30) >> S;
	sen = stoi(nes);
	sfn = stoi(nfs);
	sgn = stoi(ngs);
	shn = stoi(nhs);
	/*
	...
	*/
	return 0;
}
char S[] = { "" };
How many chars do you think S contains?
0.

I initialized it to null because setting it to

char S[30]

wait... if I set it to char S[30] and then set the delimiter arg of strtok on line 19 to a space, will that fix it?

BRB
"" is an array of size 1 so when you define S as char S[] = { "" }; S will be an array of size 1. It's not possible to change the size of an array so it's probably not what you want.

char S[30] will make S an array of 30 chars which looks like what you want.
Last edited on
I tried initializing S to [30] and nhs is containing the value I want now.

Deciding how the calculation of subnets will be carried out will take me a while.

Thanks.
I've been working on this subnet calculator for the past few days. Hopefully I didn't piss anyone off by not showing what the finished program looks like. It's nearly complete now. For the most part, it's completely functioning. The thing is, is if it's working on an octet that is 255 it returns a negative number for usable hosts per subnet. (So, I'm still working on it.)

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
/// my very own subnet calculator 3
/// ~speci4lzero
#include <iostream>
#include <iomanip>
#include <string.h>
#include <string>
#include <cmath>
using namespace std;
char tv2[8];					        ////
int we, yo;						// some useful global variables
/* define a struct that allows subnet manipulation */
struct ipv4 {
	int oa, ob, oc, od;
	/* func ct determines the subnet class, assigns we # of masks in "working subnet octet" */
	static int ct(ipv4 SUB) {
		if (SUB.ob == 0 && SUB.oc == 0 && SUB.od == 0) {
			we = cr(SUB.oa);
		}
		if (SUB.oa == 255 && SUB.ob >= 128) {
			we = cr(SUB.ob);
		}
		if (SUB.oa == 255 && SUB.ob == 255 && SUB.oc >= 128) {
			we = cr(SUB.oc);
		}
		if (SUB.od >= 128) {
			we = cr(SUB.od);
		}
		yo = 8 - we;			//// get # of zeroes in subnet mask
		we = cs(we);
		yo = cs(yo);
		return 0;
	}
	/* func cr counts ones in octet */
	static int cr(int subnet) {
		int na = 0;
		itoa (subnet, tv2, 2);
		for (int j = 0; j < 8; j++) {
			if (tv2[j] == '1')
				++na;
		}
		return na;
	}
	/* func cs is the formula that gives number of usable subnets/usable hosts per subnet */
	static int cs(int ne) {
		return (pow(2, ne) - 2);
	}
};
int main() {

	char *nes, *nfs, *ngs, *nhs, S[30];
	int sen, sfn;
	cout << "Enter a subnet!" << endl;
	cout << "Enter \"x\" to exit." << endl;
	cin >> setw(30) >> S;
	nes = strtok( S, "." );				        //// 
	nfs = strtok ( 0, "." );					// 
	ngs = strtok ( 0, "." );					// 
	nhs = strtok ( 0, " " );					// split S into 4 strings of octets
	ipv4 subn;							//// declare ipv4 variable
	subn.oa = stoi(nes);					////
	subn.ob = stoi(nfs);						//
	subn.oc = stoi(ngs);						//
	subn.od = stoi(nhs);					// cast char octets to type int
	ipv4::ct(subn);							//// run subnet calculation on subn
	sen = we;
	sfn = yo * we;
	cout << sen << "\tUSABLE SUBNETS" << endl;
	cout << sfn << "\tUSABLE HOSTS PER SUBNET" << endl;
	cin >> setw(30) >> S;						//// keep command prompt open
	return 0;
}
Topic archived. No new replies allowed.