from DEv C++ to C++

Hi, i try to run my code using visual c++ 2012 but it doesn't work. i created it using Dev C++ bloodshed. Does anyone knows how to change the coding so it can run in visual 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  #include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream.h>

float x[10],y[10];
float distance(int k, int l);

int main()
{
    FILE *fpt;
    fpt=fopen("input.txt","r");
   
    FILE *WRITE;
    WRITE=fopen("output.txt","w");

    int r=0,c=0,i=0,j=0,a1=0,a2=0,a3=0,a4=0,a5=0;
    float no[10],lat[10],log[10],dist[6],b[5][5],max,min;
    float dx12,dy12,dx23,dy23,dx34,dy34,dx45,dy45,dx51,dy51;
    float px12,py12,px23,py23,px34,py34,px45,py45,px51,py51;
    
    char m[33],z[10][15]; 
    
    printf("\n\n\t\tAROUND THE WORLD TRIP PLANNER");
    printf("\n\t\t-----------------------------\n");
    fprintf(WRITE,"\n\n\t\tAROUND THE WORLD TRIP PLANNER");
    fprintf(WRITE,"\n\t\t-----------------------------\n");
    
    printf("\n\nThis program will calculate distance between city after four journey. ");
    printf("\nUser required to enter which city to start and every city to visit.");
    printf("\nAfter that it will return back to the city where it starts.");
    printf("\nReport will be print out in output.txt");
    printf("\n------------------------------------------------------------------------\n");
    
    printf("\na) Prints out list of city from input.txt file ..\n\n");
    fprintf(WRITE,"\na) Prints out list of city from input.txt file ..\n\n");
    
    for (r=0;r<1;r++)
   {
       for(c=0;c<33;c++)
       {
        fscanf(fpt,"%c",&m[i]);
        printf("%c",m[i]);
        fprintf (WRITE,"%c",m[i]);
        i++;
       }
   }
       
   r=1;
   for (r=1;r<11;r++)
   {
       fscanf(fpt,"%f",&no[r]);
       printf("\n%.0f",no[r]);
       fprintf(WRITE,"\n%.0f",no[r]);
       i=0;
       for(c=0;c<15;c++)
       {
        fscanf(fpt,"%c",&z[r][i]);
        printf("%c",z[r][i]);
        fprintf(WRITE,"%c",z[r][i]);
        i++;
       }
       i=0;
              
       fscanf(fpt,"%f",&lat[r]);
       printf("\t%3.3f",lat[r]);
       fprintf(WRITE,"\t%3.3f",lat[r]);
       
       fscanf(fpt,"%f",&log[r]);
       printf("\t\t%3.3f",log[r]);
       fprintf(WRITE,"\t\t%3.3f",log[r]);
       
       fscanf(fpt,"%f",&x[r]);
       fscanf(fpt,"%f",&y[r]);
       
       printf("     \t(%.2f,%.2f)",x[r],y[r]);
       fprintf(WRITE,"     \t(%.2f,%.2f)",x[r],y[r]);
       
   }
     
   printf("\n\n\t>Please enter city no for start: ");
   scanf("%d",&a1);
         
   printf("\t>Please enter city no as your 1st journey: ");
   scanf("%d",&a2);   
 
   printf("\t>Please enter city no as your 2nd journey: ");
   scanf("%d",&a3);  
    
   printf("\t>Please enter city no as your 3rd journey: ");
   scanf("%d",&a4);  
    
   printf("\t>Please enter city no as your 4th journey: ");    //user
   scanf("%d",&a5);
     
   if(a1>10||a2>10||a3>10||a4>10||a5>10)                       //Error message if user input >10
   { printf("\nError!!! Please enter city within 1 to 10 only.Please run again the program\n\n."); 
    system("pause"); return 0;}
    
   printf("\n\nb) Selected destination are as below :-\n");
   fprintf(WRITE,"\n\nb) Selected destination are as below :-\n");
   
   for(i=0;i<15;i++)
       {
        printf("%c",z[a1][i]);
        fprintf(WRITE,"%c",z[a1][i]);
       }   
   printf(">>");
   fprintf(WRITE,">>");
  
   for(i=0;i<15;i++)
       {
        printf("%c",z[a2][i]);
        fprintf(WRITE,"%c",z[a2][i]);
       }   
   printf(">>");
   fprintf(WRITE,">>");
   
   for(i=0;i<15;i++)
       {
        printf("%c",z[a3][i]);
        fprintf(WRITE,"%c",z[a3][i]);
       }   
   printf(">>");
   fprintf(WRITE,">>");
   
   for(i=0;i<15;i++)
       {
        printf("%c",z[a4][i]);
        fprintf(WRITE,"%c",z[a4][i]);
       }   
   printf(">>");
   fprintf(WRITE,">>");
   
   for(i=0;i<15;i++)
       {
        printf("%c",z[a5][i]);
        fprintf(WRITE,"%c",z[a5][i]);
       }   
   printf(">>");
   fprintf(WRITE,">>");
   
   for(i=0;i<15;i++)
       {
        printf("%c",z[a1][i]);
        fprintf(WRITE,"%c",z[a1][i]);
       } 
       
   printf("\n\nc) Travel distance between each destination is presented in as below..\n");
   fprintf(WRITE,"\n\nc) Travel distance between each destination is presented in as below..\n");
      
                                             
   dist[0] = distance(a1,a2);                        //call function to calculate each destination distance
   //printf("\nDistance12: %.4f",dist[0]);           //to display checking value
   
   dist[1]= distance(a2,a3);
   //printf("\nDistance23: %.4f",dist[1]);
   
   dist[2]= distance(a3,a4);
   //printf("\nDistance34: %.4f",dist[2]);
   
   dist[3]= distance(a4,a5);
   //printf("\nDistance45: %.4f",dist[3]);
   
   dist[4]= distance(a5,a1);
   //printf("\nDistance51: %.4f",dist[4]);
  
   for(i=0;i<15;i++)
       {
        printf("%c",z[a1][i]);
        fprintf(WRITE,"%c",z[a1][i]);
       }   
   printf(">> %.4f >>",dist[0]);
   fprintf(WRITE,">> %.4f >>",dist[0]);
  
   for(i=0;i<15;i++)
       {
        printf("%c",z[a2][i]);
        fprintf(WRITE,"%c",z[a2][i]);
       }   
   printf(">> %.4f >>",dist[1]);
   fprintf(WRITE,">> %.4f >>",dist[1]);
   
   for(i=0;i<15;i++)
       {
        printf("%c",z[a3][i]);
        fprintf(WRITE,"%c",z[a3][i]);
       }   
   printf(">> %.4f >>",dist[2]);
   fprintf(WRITE,">> %.4f >>",dist[2]);
   
   for(i=0;i<15;i++)
       {
        printf("%c",z[a4][i]);
        fprintf(WRITE,"%c",z[a4][i]);
       }   
   printf(">> %.4f >>",dist[3]);
   fprintf(WRITE,">> %.4f >>",dist[3]);
   
   for(i=0;i<15;i++)
       {
        printf("%c",z[a5][i]);
        fprintf(WRITE,"%c",z[a5][i]);
       }   
   printf(">> %.4f >>",dist[4]);
   fprintf(WRITE,">> %.4f >>",dist[4]);
   
   for(i=0;i<15;i++)
       {
        printf("%c",z[a1][i]);
        fprintf(WRITE,"%c",z[a1][i]);
       } 
     
   printf("\n\nd) The distance on each destination is presented in below table.\n");
   printf("\n\n   (Please refer output data 'output.txt') \n");
   fprintf(WRITE,"\n\nd) The distance on each destination is presented in below table.\n");
   
   b[0][0]= distance(a1,a1);
   b[0][1]= distance(a1,a2);
   b[0][2]= distance(a1,a3);  
   b[0][3]= distance(a1,a4); 
   b[0][4]= distance(a1,a5); 
   
Firstly, well done for dumping a compiler from twenty years ago (Bloodshed is an IDE and compiler bundle). Sadly, the world of C++ underwent some changes back in 1998 (when it was standardised) that makes your code non-standard and thus incorrect. You need to bring it into line with standard C++.

Start with those headers. They've look like this for the last 17 years:

1
2
3
4
#include <cstdio>
#include <string>
#include <cmath>
#include <iostream> 


You're going to need to learn about namespaces. They were introduced back in '98. For the moment, you could stick

using namespace std;

on line 5 of your code. Be aware that this is awful practice and something you shouldn't ever do, but to just try to get your code back to at least compiling again, you might as well try it.

When you've done that, make a note of whatever the first compilation error is and come back.
Last edited on
I see at least one missing }, but maybe it was just cut off by an incomplete copy+paste.

First thing, thank you for moving away from Bloodshed Dev-C++. <3

Second, the most obvious issue is the .h file extensions on your header files. Those are used by C for its header files. C++ has its own versions of those header files, that are prefixed with c instead of suffixed with .h.

So, for instance, #include <stdio.h> becomes #include <cstdio> .

The exception is <iostream>, which is not in the C standard library. It shouldn't have a .h on the end, nor should it be prefixed with c in this case.

EDIT: Ninja'd by Moschops.

Um, one more more thing. This code is pretty much pure C. I don't see iostream actually get used anywhere. If you meant to write C and not C++, then disregard what I said above, remove <iostream.h>, move all your variable declarations to the tops of your functions (because Visual C's compiler is ancient and that is a necessary step to placate it) and make sure your code is being compiled with a C compiler.

-Albatross
Last edited on
closed account (48T7M4Gy)
float distance(int k, int l); prototype but no definition.
closed account (48T7M4Gy)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <stdio.h>
#include <string>
#include <math.h>
#include <iostream>

float x[10], y[10];
float distance(int k, int l);

int main()
{
	FILE *fpt;
	fpt = fopen("input.txt", "r");

	FILE *WRITE;
	WRITE = fopen("output.txt", "w");

	int r = 0, c = 0, i = 0, j = 0, a1 = 0, a2 = 0, a3 = 0, a4 = 0, a5 = 0;
	float no[10], lat[10], log[10], dist[6], b[5][5], max, min;
	float dx12, dy12, dx23, dy23, dx34, dy34, dx45, dy45, dx51, dy51;
	float px12, py12, px23, py23, px34, py34, px45, py45, px51, py51;

	char m[33], z[10][15];

	printf("\n\n\t\tAROUND THE WORLD TRIP PLANNER");
	printf("\n\t\t-----------------------------\n");
	fprintf(WRITE, "\n\n\t\tAROUND THE WORLD TRIP PLANNER");
	fprintf(WRITE, "\n\t\t-----------------------------\n");

	printf("\n\nThis program will calculate distance between city after four journey. ");
	printf("\nUser required to enter which city to start and every city to visit.");
	printf("\nAfter that it will return back to the city where it starts.");
	printf("\nReport will be print out in output.txt");
	printf("\n------------------------------------------------------------------------\n");

	printf("\na) Prints out list of city from input.txt file ..\n\n");
	fprintf(WRITE, "\na) Prints out list of city from input.txt file ..\n\n");

	for (r = 0;r < 1;r++)
	{
		for (c = 0;c < 33;c++)
		{
			fscanf(fpt, "%c", &m[i]);
			printf("%c", m[i]);
			fprintf(WRITE, "%c", m[i]);
			i++;
		}
	}

	r = 1;
	for (r = 1;r < 11;r++)
	{
		fscanf(fpt, "%f", &no[r]);
		printf("\n%.0f", no[r]);
		fprintf(WRITE, "\n%.0f", no[r]);
		i = 0;
		for (c = 0;c < 15;c++)
		{
			fscanf(fpt, "%c", &z[r][i]);
			printf("%c", z[r][i]);
			fprintf(WRITE, "%c", z[r][i]);
			i++;
		}
		i = 0;

		fscanf(fpt, "%f", &lat[r]);
		printf("\t%3.3f", lat[r]);
		fprintf(WRITE, "\t%3.3f", lat[r]);

		fscanf(fpt, "%f", &log[r]);
		printf("\t\t%3.3f", log[r]);
		fprintf(WRITE, "\t\t%3.3f", log[r]);

		fscanf(fpt, "%f", &x[r]);
		fscanf(fpt, "%f", &y[r]);

		printf("     \t(%.2f,%.2f)", x[r], y[r]);
		fprintf(WRITE, "     \t(%.2f,%.2f)", x[r], y[r]);

	}

	printf("\n\n\t>Please enter city no for start: ");
	scanf("%d", &a1);

	printf("\t>Please enter city no as your 1st journey: ");
	scanf("%d", &a2);

	printf("\t>Please enter city no as your 2nd journey: ");
	scanf("%d", &a3);

	printf("\t>Please enter city no as your 3rd journey: ");
	scanf("%d", &a4);

	printf("\t>Please enter city no as your 4th journey: ");    //user
	scanf("%d", &a5);

	if (a1 > 10 || a2 > 10 || a3 > 10 || a4 > 10 || a5 > 10)                       //Error message if user input >10
	{
		printf("\nError!!! Please enter city within 1 to 10 only.Please run again the program\n\n.");
		system("pause"); return 0;
	}

	printf("\n\nb) Selected destination are as below :-\n");
	fprintf(WRITE, "\n\nb) Selected destination are as below :-\n");

	for (i = 0;i < 15;i++)
	{
		printf("%c", z[a1][i]);
		fprintf(WRITE, "%c", z[a1][i]);
	}
	printf(">>");
	fprintf(WRITE, ">>");

	for (i = 0;i < 15;i++)
	{
		printf("%c", z[a2][i]);
		fprintf(WRITE, "%c", z[a2][i]);
	}
	printf(">>");
	fprintf(WRITE, ">>");

	for (i = 0;i < 15;i++)
	{
		printf("%c", z[a3][i]);
		fprintf(WRITE, "%c", z[a3][i]);
	}
	printf(">>");
	fprintf(WRITE, ">>");

	for (i = 0;i < 15;i++)
	{
		printf("%c", z[a4][i]);
		fprintf(WRITE, "%c", z[a4][i]);
	}
	printf(">>");
	fprintf(WRITE, ">>");

	for (i = 0;i < 15;i++)
	{
		printf("%c", z[a5][i]);
		fprintf(WRITE, "%c", z[a5][i]);
	}
	printf(">>");
	fprintf(WRITE, ">>");

	for (i = 0;i < 15;i++)
	{
		printf("%c", z[a1][i]);
		fprintf(WRITE, "%c", z[a1][i]);
	}

	printf("\n\nc) Travel distance between each destination is presented in as below..\n");
	fprintf(WRITE, "\n\nc) Travel distance between each destination is presented in as below..\n");


	dist[0] = distance(a1, a2); 
	
	dist[1] = distance(a2, a3);
	//printf("\nDistance23: %.4f",dist[1]);

	dist[2] = distance(a3, a4);
	//printf("\nDistance34: %.4f",dist[2]);

	dist[3] = distance(a4, a5);
	//printf("\nDistance45: %.4f",dist[3]);

	dist[4] = distance(a5, a1);
	//printf("\nDistance51: %.4f",dist[4]);

	for (i = 0;i < 15;i++)
	{
		printf("%c", z[a1][i]);
		fprintf(WRITE, "%c", z[a1][i]);
	}
	printf(">> %.4f >>", dist[0]);
	fprintf(WRITE, ">> %.4f >>", dist[0]);

	for (i = 0;i < 15;i++)
	{
		printf("%c", z[a2][i]);
		fprintf(WRITE, "%c", z[a2][i]);
	}
	printf(">> %.4f >>", dist[1]);
	fprintf(WRITE, ">> %.4f >>", dist[1]);

	for (i = 0;i < 15;i++)
	{
		printf("%c", z[a3][i]);
		fprintf(WRITE, "%c", z[a3][i]);
	}
	printf(">> %.4f >>", dist[2]);
	fprintf(WRITE, ">> %.4f >>", dist[2]);

	for (i = 0;i < 15;i++)
	{
		printf("%c", z[a4][i]);
		fprintf(WRITE, "%c", z[a4][i]);
	}
	printf(">> %.4f >>", dist[3]);
	fprintf(WRITE, ">> %.4f >>", dist[3]);

	for (i = 0;i < 15;i++)
	{
		printf("%c", z[a5][i]);
		fprintf(WRITE, "%c", z[a5][i]);
	}
	printf(">> %.4f >>", dist[4]);
	fprintf(WRITE, ">> %.4f >>", dist[4]);

	for (i = 0;i < 15;i++)
	{
		printf("%c", z[a1][i]);
		fprintf(WRITE, "%c", z[a1][i]);
	}

	printf("\n\nd) The distance on each destination is presented in below table.\n");
	printf("\n\n   (Please refer output data 'output.txt') \n");
	fprintf(WRITE, "\n\nd) The distance on each destination is presented in below table.\n");

	b[0][0] = distance(a1, a1);
	b[0][1] = distance(a1, a2);
	b[0][2] = distance(a1, a3);
	b[0][3] = distance(a1, a4);
	b[0][4] = distance(a1, a5);

	return 0;
}

float distance(int k, int l)
{
	//stub
	return 99;
}


This compiles in VS2015 but crashes because you need a data file and probably a few tweaks with file handling errors.

Note the return 0 and closing brace, along with a stub for the missing function definition.

The output from this on the console was:


                AROUND THE WORLD TRIP PLANNER
                -----------------------------


This program will calculate distance between city after four journey.
User required to enter which city to start and every city to visit.
After that it will return back to the city where it starts.
Report will be print out in output.txt
------------------------------------------------------------------------

a) Prints out list of city from input.txt file ..

That's as far as it got before it crashed.

PS The Prime Minister of Singapore uses something like Dev C++ as a hobby so don't feel totally alone. It really is crap though, considering VS2015 is free and up to date. :)
Topic archived. No new replies allowed.