I'm currently working on a homework and can't get the result I want from the code below.
#include <stdio.h>
int main() {
int base, num, x, y, z, t, choice;
char hexa = 0, a, b, c, d, k, l, m, n, h, g, f, v;
switch (hexa) {
case 'A':
num = 10;
break;
case 'B':
num = 11;
break;
case 'C':
num = 12;
break;
case 'D':
num = 13;
break;
case 'E':
num = 14;
break;
case 'F':
num = 15;
break;
}
printf("Please enter the base:\n");
scanf("%d", &base);
if (base == 10) {
printf("Please enter your ip address:\n");
scanf("%c.%c.%c.%c", &a, &b, &c, &d);
//if((a<0 || a>255) && (b<0 || b>255) && (c<0 || c>255) && (d<0 || d>255)){ //FIX THIS
// printf("Sorry, that is not a valid address!");}
printf("Please enter the sub-net mask\n"); //Skips this line
scanf("%d.%d.%d.%d", &x, &y, &z, &t);
}
else if (base == 16) {
printf("Enter your ip address\n");
scanf("%c.%c.%c.%c", &k, &l, &m, &n);
printf("Enter the subnet mask\n");
scanf("%c.%c.%c.%c", &h, &g, &f, &v);
}
scanf("%d", &choice);
if (choice == 1) { //there is a part that asks which
printf("%c.%c.%c.%c", a, b, c, d); //prints only dots
}
else if (choice == 2) {}
else if (choice == 3) {}
else if (choice == 4) {}
else if (choice == 5) {}
else if (choice == 6) {}
return 0;
}
So basically I'm trying to get an IP and subnet and convert it to binary but as instructed I can't use bitwise shift and %x
to get the IP address as base 16. The problem is when I try to print the IP address given printf
only prints dots and nothing else. And also one of the skips the subnet mask scanf
. I would be really glad if I can get some help from you guys as I don't have many friends to ask questions. Thanks.
EDIT: I don't know if anyone is going to face this problem but my solution was rather silly but it works. You basically take the input as scanf("%c%c.%c%c.%c%c.%c%c%c")
, the last %c
is for the enter as char takes enter as input.