There are a number of issues with your program. Since you're writing C++, I don't see why you want to include stdio.h and stdlib.h. Even if you felt you absolutely needed these, the correct files to include, for C++, are cstdio and cstdlib. The old versions were included for compatibility with pre-standard compilers.
As mentioned earlier, cin should be tested for success because you cannot control whether or not your user is error-prone, ignorant, malicious, or falls asleep with his/her head on the keyboard. Write robust code. That's why error testing is available.
The code is clearer when written using a loop. For instance, the cin test is written once, but is effective for a number of input activities. The accumulation of the seeds, which I have kept separate, could actually be included in the loop.
Since you have defined the seeds as ints, you can easily wind up with negative values and with values greater than 255, which is the maximum for an octet.
Have a look at this. I have added the output value in hex, so you can more easily see the contribution of the octets to the final value. The IP is essentially a 4-digit base 256 number with the digits expressed as decimal values.
Code:
#include <iostream>
#include <iomanip>
#include <string>
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using std::string;
int badNews (string troubleInRiverCity)
{
cerr << troubleInRiverCity << endl;
return EOF;
}
int main (int argc, char *argv[])
{
unsigned seeds [4];
string prompts [4] = {"first", "second", "third", "fourth"};
unsigned total = 0;
int tries = 0;
cout <<
"####################################\n"
"# IP to 32-Bit DWORD #\n"
"# Made by Brandon Dixon #\n"
"# #\n"
"# 1.) Enter IP as Asked #\n"
"# 2.) DWORD is Calculated #\n"
"# 3.) Enjoy #\n"
"####################################\n\n";
cin.clear ();
for (int i = 0; (i < 4) && (tries < 4); i++)
{
cout << "Enter " << prompts [i] << " octet of IP : ";
cin >> seeds [i];
if (!cin.good () || seeds [i] > 255)
{
tries++;
cout << "\nError in entering " << prompts [i] << " octet" << endl;
i--;
cin.sync ();
cin.clear ();
}
}
if (tries > 3) return badNews ("Too many bad input attempts\n");
for (int i = 0; i < 4; i++)
{
total *= 256;
total += seeds [i];
}
cout << "\n\nYour 32-Bit DWORD is " << total << " hex ("
<< std::setw (8) << std::setbase (16) << total << ")" << endl;
cout << "\n\nThank You for using the 32-Bit DWORD Convertor!\n";
cout << "Press ENTER to terminate the program" << endl;
// "pause" is not portable
cin.clear ();
cin.sync ();
cin.get ();
return 0;
}
Quote:
Originally Posted by Output
Code:
####################################
# IP to 32-Bit DWORD #
# Made by Brandon Dixon #
# #
# 1.) Enter IP as Asked #
# 2.) DWORD is Calculated #
# 3.) Enjoy #
####################################
Enter first octet of IP : A
Error in entering first octet
Enter first octet of IP : 512
Error in entering first octet
Enter first octet of IP : 192
Enter second octet of IP : 168
Enter third octet of IP : 1
Enter fourth octet of IP : 1
Your 32-Bit DWORD is 3232235777 hex (c0a80101)
Thank You for using the 32-Bit DWORD Convertor!
Press ENTER to terminate the program