GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Jan-2007, 20:31
root computing root computing is offline
New Member
 
Join Date: Jan 2007
Posts: 3
root computing is on a distinguished road

Math problem, I am stumped.


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.

CPP / C++ / C 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;
}

Last edited by LuciWiz : 04-Jan-2007 at 22:25. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 04-Jan-2007, 20:59
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: Math problem, I am stumped.


Quote:
Originally Posted by root computing
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.
I see this,
CPP / C++ / C Code:
  seed1 * 16777216;
  seed2 * 65536;
  seed3 * 256;
That will not save the results.
You need to,
CPP / C++ / C Code:
some_var = seed1 * 16777216;
or
CPP / C++ / C Code:
  seed1 *= 16777216;
  seed2 *= 65536;
  seed3 *= 256;
  #3  
Old 04-Jan-2007, 21:04
root computing root computing is offline
New Member
 
Join Date: Jan 2007
Posts: 3
root computing is on a distinguished road

Re: Math problem, I am stumped.


I had them set up in variables, but right before I posted it I took them out, even with them in it still didn't work. I just noticed the second option you mentioned but that gave me the same wrong result. I don't get why this is not working, its such a simple math equation.
  #4  
Old 04-Jan-2007, 21:11
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: Math problem, I am stumped.


Take a look at this thread,

"Here comes some questions"
http://www.gidforums.com/t-12900.html
  #5  
Old 04-Jan-2007, 21:19
root computing root computing is offline
New Member
 
Join Date: Jan 2007
Posts: 3
root computing is on a distinguished road

Re: Math problem, I am stumped.


Thanks that helped in some way. Now I just know that its not possible. Ugh.
  #6  
Old 04-Jan-2007, 21:54
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,496
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Math problem, I am stumped.


Quote:
Originally Posted by root computing
its not possible.

You are trying to store four octets in a variable, so you need a variable whose length is 32 bits (four chars).

Most compilers these days have 32-bit ints. If yours has 16-bit ints, then you should be able to use long ints.

Try this:

CPP / C++ / C Code:
#include <iostream>
using namespace std;

int main()
{
    cout << "sizeof(unsigned int)  = " << sizeof(unsigned int) << endl;
    cout << "sizeof(unsigned long) = " << sizeof(unsigned long) << endl;
    return 0;
}


If is says that the size an unsigned int is equal to 4, then you can use unsigned ints.
If it says that the size of an unsigned long is equal to 4, then you can use unsigned long ints.

Whichever one you try, if you still have trouble then:

1. Post the code. The exact code that you are using. Paste it into your post. It really doesn't make sense for you to run one file but post another and expect someone to help. Your first post would not compile on any of the several compilers to which I have access.

2. Tell us what compiler you are using and what operating system. Post any compiler messages that you got. Paste them directly into your post. Don't paraphrase.

3. Show the exact input and output (just post the text into your post). Simply saying, "It doesn't work," doesn't give us much to work with when we are trying to help you understand how to go forward. And that's the point, right?


Regards,

Dave
  #7  
Old 05-Jan-2007, 04:50
killzone killzone is offline
Junior Member
 
Join Date: Nov 2006
Posts: 66
killzone is an unknown quantity at this point

Re: Math problem, I am stumped.


had that error once
took long to figure out

but then i realised it was easier than i thought.
  #8  
Old 09-Jan-2007, 02:10
Danny*Cai Danny*Cai is offline
New Member
 
Join Date: Jan 2007
Posts: 8
Danny*Cai is on a distinguished road

Re: Math problem, I am stumped.


Quote:
Originally Posted by root computing
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.

CPP / C++ / C 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;
}

The problem is that you used 'int total'. In C++, the range of int is -2^31 to 2^31. Obviously it cannot load your 32 bits data. Try to use 'unsigned int total' or 'unsigned long'. They all work well.
 
 

Recent GIDBlogPh.D. progress by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Math problem: Total Noob Targsmom C Programming Language 3 20-Oct-2006 05:05
Buffer problem with CD-RWriter KieranC Computer Software Forum - Windows 2 07-Jan-2006 01:45
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 12:31
Runtime Problem involving "printf" in C Program supamakia C Programming Language 2 09-Oct-2005 10:09
a significant problem after installing Xp mohammad Computer Software Forum - Windows 10 09-Aug-2005 07:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:44.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.