Math problem, I am stumped.

root computing's Avatar, Join Date: Jan 2007
Newbie Member
Ok, I am not a programmer and only write small EXE programs to take care of easy tasks. I threw a program together real quick to do some simple math functions to an IP address. The point of this is to take the IP and convert it to whats called a DWORD. I won't go in depth of why I am doing this, but I am having trouble. The math doesnt seem to be doing what its supposed to. I am coming up with some crazy answers and I am not sure why. Someone please help me out.

Heres what it should do...

First Octet * 16,777,216
Second Octet * 65,536
Third Octet * 11,010,048
Forth Octet * 1

Everything compiles fine and it works, just the wrong ending value. Just as a reference 192.168.1.1 should equal 3,232,235,777. Thanks in advance.

Code:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  int seed1, seed2, seed3, seed4, total;

  cout << "
  ####################################
  # IP to 32-Bit DWORD               #
  #        Made by Brandon Dixon     #
  #                                  #
  # 1.) Enter IP as Asked            #
  # 2.) DWORD is Calculated          #
  # 3.) Enjoy                        #
  ####################################";
  cout << "\n\nEnter First Octet of IP : ";
  cin >> seed1;
  cout << "\n\nEnter Second Octet of IP : ";
  cin >> seed2;
  cout << "\n\nEnter Third Octet of IP : ";
  cin >> seed3;
  cout << "\n\nEnter Forth Octet of IP : ";
  cin >> seed4;

  seed1 * 16777216;
  seed2 * 65536;
  seed3 * 256;

  total = seed1C + seed2C + seed3C + seed4;

  cout << "\n\nYour 32-Bit DWORD is " << total << endl;

  cout << "\n\nThank You for using the 32-Bit DWORD Convertor!\n\n";
  system("pause");


  return 0;
}
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
So where are you assigning these results?
Code:
  seed1 * 16777216;
  seed2 * 65536;
  seed3 * 256;
Also, you're not smart if you don't check cin to see if it's in error. What makes you think your user won't strike an alpha key, which can't be converted, and will therefore cause a failure. Once that stream breaks, it's broken until you repair it.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by root computing
Everything compiles fine and it works, just the wrong ending value. Just as a reference 192.168.1.1 should equal 3,232,235,777. Thanks in advance.
Everything compiling does not mean it will give correct result.

You should be assigning some thing to seed1C, seed2C, seed3C, seed4 before doing total = seed1C + seed2C + seed3C + seed4;
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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