This code is C/C++ and runs without warnings or debug messages. I’m using Code::blocks with the GNU GCC compiler. This app worked perfectly once, then somewhere along the lines I messed up without noticing. Now every time it will allow a ip address input, but then freeze up and close. Why?
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int ip[3];
char * inputIP;
int x;
string classValue;
void subnetClass()
{
if (x==0) classValue="Error: first octet may not be zero.";
if (x>0 && x<=126) classValue="Class A";
if (x==127) classValue="Loopback Address";
if (x>=128 && x<=191) classValue="Class B";
if (x>=192 && x<=223) classValue="Class C";
if (x>=224 && x<=239) classValue="Class D";
if (x>=240 && x<=255) classValue="Class E";
if (x>255) classValue="Error: an octet may not be more than 255.";
cout << classValue << endl;
}
int main()
{
cout << "Enter IP address in dotted-decimal form." << endl;
cin >> inputIP;
scanf(inputIP, "%d.%d.%d.%d" , &ip[0],&ip[1],&ip[2],&ip[3]);
int x=ip[0];
subnetClass();
return 0;
}
Build Log:
Checking for existence: C:…\IP subnetting app\bin\Debug\IP subnetting app.exe
Executing: “C:…\CodeBlocks/cb_console_runner.exe” “C:…\IP subnetting app\bin\Debug\IP subnetting app.exe” (in C:…\IP subnetting app.)
Process terminated with status -1073741510 (0 minutes, 27 seconds)
It might have worked with a little help from sheer luck even if you messed things up later, I believe. More or less everything is wrong. First you read the line into the area pointed to by uninitialized pointer (or maybe you read the pointer value, I’m not even sure what
>> (char*)is supposed to do). You better change the definition tothen you try to parse it used
scanfand pass this pointer as a format string. What you meant is usingsscanf. Assuming you changed theinputIPtype, you can useThen you assign to local main variable
xthat shadows global, which remains uninitialized when you use it in the function. Just remove theintpart in the assignment like this:and make the
iparray of four elements.Then it may work. Unless I missed something else.
And one more thing: if you use some source control (for instance using
gityou may start new project in no time) then you’d know what you’ve changed when you mess up, just commit early, commit often.