how to convert MS visual c++ codes to dev c++?

i saw this code from a website by mr. Muhammad Tahir Shahzad
but i can't run it in dev c++, please help me debug it so i can run it with dev c++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**************************************************************************
  **************************************************************************

  A C++ Program to show an example of Hashing using Mid-Square Method.

  **************************************************************************
  **************************************************************************/

  /*************************************************************************

	  By :
		Muhammad Tahir Shahzad  [ MTS ]
		B.C.S Honours  [ 2000-04 ]
		Government College University Lahore
		Pakistan

      E-mail :  [email protected]

    Web-Site :  www.mts-home.cjb.net  [ www.wol.net.pk/mtshome ]
		www.mtshome.cjb.net   [ www.geocities.com/mtahirshahzad ]

  *************************************************************************/

 /*************************************************************************/
 /*************************************************************************/
 //---------------------------  Header Files  ----------------------------//
 /*************************************************************************/
 /*************************************************************************/

 # include <iostream.h>
 # include <string.h>
 # include <stdlib.h>
 # include <conio.h>
 # include <math.h>

 /*************************************************************************/
 /*************************************************************************/
 //------------------------------  main( )  ------------------------------//
 /*************************************************************************/
 /*************************************************************************/

 int main( )
 {
    clrscr( );

    cout << endl << "Mid-Square Hashing Method" << endl;
    cout << "*************************" << endl << endl;

    unsigned int iNumber = 0;
    unsigned int iLimit = 0;

    cout << "Enter the Number = N = ";
    cin >> iNumber;

    cout << endl << "Enter the Limit = K = ";
    cin >> iLimit;

    unsigned long lNumber = powl(iNumber, 2);

    char sNumber[50] = {NULL};

    ultoa(lNumber, sNumber, 10);

    int iCount = (strlen(sNumber) - iLimit);
    int iLength = 0;

    char sTemp[30] = {NULL};

    cout << endl << "K * K = "<< lNumber << endl;

    for (int i = 0; i < iCount; i ++)
    {
       iLength = (strlen(sNumber) - 1);

       if ( (i % 2) == 0)
       {
	  strset(sTemp, NULL);
	  strncpy(sTemp, sNumber, iLength);

	  strset(sNumber, NULL);
	  strcpy(sNumber, sTemp);
       }

       else
       {
	  strrev(sNumber);
	  strset(sTemp, NULL);
	  strncpy(sTemp, sNumber, iLength);
	  strrev(sTemp);

	  strset(sNumber, NULL);
	  strcpy(sNumber, sTemp);
       }
    }

    int iHashKey = atoi(sNumber);

    cout << endl << "Hashing Key = " << iHashKey;

    getch( );
    return 0;
 }

 /*************************************************************************/
 /*************************************************************************/
 //-----------------------------  THE END  -------------------------------//
 /*************************************************************************/
 /*************************************************************************/
Last edited on
This is using an old version of the standard (plus some things that are absolutely not standard). A good deal of the code is obsolete.
I recommend you rewrite it from scratch.
Last edited on
- add using namespace std; after the headers
- replace clrscr() with system("cls")
- ultoa converts an integer value to a string, but it isn't supported by all compilers. You could write you're own function using stringstream;
1
2
3
4
5
6
7
8
9
10
// edited
#include <string> //string
#include <sstream> //stringstream
...
string IntToStr(int number) //converts integer to string
{
stringstream myStream;
myStream<<number;
return myStream.str();
}


You would still have to convert the string into a char[], but I think you can figure that out yourself.

This code seems to be pretty outdated, and the use of non-standard functions should be avoided in learning methods. Maybe you should look to an other tutorial?

[edit]
So I completely agree with helios :)
Last edited on
> how to convert MS visual c++ codes to dev c++?

This has no sense at all.
An IDE is not a language.
Read the Stroustrup instead of copying BS.
ultoa converts an integer value to a string, but it isn't supported by all compilers

Thanks for the detailed explanation, Scipio. It's very informative to see how C++ has evolved over time and has different flavors depending on the platform. Would it be correct to say the more C++ has matured, the more "standard" it has become on all compilers? Should platform dependencies be avoided like the plague?
Would it be correct to say the more C++ has matured, the more "standard" it has become on all compilers?


I'm not sure, to be honest. I'm pretty new to programming to. But I think it's the other way around; at the beginning there was one standard, and just a few compilers. Now the standard has been rewritten a few times, outdated books are used, etc.

Should platform dependencies be avoided like the plague?


Yes!! Never use platform depended functions!
Last edited on
> how to convert MS visual c++ codes to dev c++?

This has no sense at all.
An IDE is not a language.
Read the Stroustrup instead of copying BS.


You my friend, are an idiot. This question is valid as some of his code is no depreciated with the newer C++ standards. So it's both valid and reasonable to ask how to modify his code to work on a newer compiler.

Yes!! Never use platform depended functions!

Platform dependent functions are fine. You should avoid compiler specific extensions though. If you don't use platform-dependent code then you end up using alot of platform-independent libraries, which could be for no use if your code will never be compiled on another platform.
Zaite is right of course. I confused platform-depended functions with compiler-depended functions.
I was suppose to add, don't use Dev-C++ as it's no longer supported. Download and use something like wxDev-C++
Topic archived. No new replies allowed.