ISO C++

I was experimenting with multithreading to try to make this program faster but it does not compile, instead it Dev C++ opens a new window with this error:

#error This file requires compiler and library support for the \
ISO C++ 2011 standard. This support is currently experimental, and must be \
enabled with the -std=c++11 or -std=gnu++11 compiler options.

Thanks in advance for any reply

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
#include <iostream>
#include <iomanip>
#include <cmath>
#include <time.h>
#include <stdlib.h>
#include <thread>
using std::thread;
#include <vector>
using std::vector;
using namespace std;

void wait(int seconds)
{
	clock_t endwait;
	endwait = clock() + seconds * CLOCKS_PER_SEC;
	while (clock() < endwait) {}
}

void Autovelox (double v1, a1, v2, a2, v1max, v2max)
{
	if(v1 < L) {
        	v1 = v1 + a1 * t;
		 } 
		if(v1 >= L) {
			v1 = L;			
		}
        if(v2 < L) {
        	v2 = v2 + a2 * t;
		}
		if(v2 >= L) {
			v2 = L;
		}
		if(abs(v1) > abs(v1max)) {
			v1max = v1;
		}
		if(abs(v2) > abs(v2max)) {
			v2max = v2;
		}
}

int main()
{

    const double G = 6.67408e-11;  // costante gravitazionale.
    const double L = 299792458;  // velocità della luce.

	int n = std::thread::hardware_concurrency();
    double m1, m2, d, i, p, F, a1, a2, s1, s2, t, cont,days, distparz,time, v1, v2,r1,r2,v1k,v2k,conth,contg,distmin,ao1,v1max,v2max,ao2,sdiff1,sdiff2;
	double datiR[6] = { 6.371e+6 , 1.737e+6  , 6.96e+8 , 3.39e+6 , 6.9911e+7 , 13e+9 };
	double datiM[6] = { 5.972e+24 , 7.34767309e+22 , 1.989e+30 , 3.844e+8 , 1.898e+27 , 8.573e+36 };
//                       terra       luna           sole         marte       giove	   sagittarius A*
	double datiD[7] = { 3.84403e+8 , 1.496e+11 , 9.2451e+10 , 2.279e+11 , 7.785e+11 , 6.287e+11 , 2.46e+20 };
//                         dtl         dts         dtm        dsm          dgs           dgt         dss
    double separation; 
    int sel;
    float caricamento;
    
    vector<ThreadItem> threadlist;
	threadlist.resize(n);
	
	printf("\nQuesto programma analizza due corpi che collassano su se stessi senza attriti.\nTutti i dati inseriti vanno interpretati nelle unita' dell'MKS.\nInserisci la massa del corpo n'1. Se si inserisce 0 si potra' scegliere tra dati predefiniti:\n");
    scanf("%lf", &m1);
    if(m1 == 0) {
    	do{
			printf("O: Terra\n1: Luna\n2: Sole\n3: Marte\n4: Giove\n5: Sagittarius A* (Buco nero al centro della Via Lattea)\n");
			scanf("%i",&sel);
		}
		while(sel > 5||sel < 0);
		m1 = datiM[sel];
		r1 = datiR[sel];
		goto input2;
	}
    printf("Inserisci il raggio del corpo n'1. Inserire 0 per considerare un corpo puntiforme:\n");
    scanf("%lf", &r1);
    input2:
    printf("Inserisci la velocita' iniziale del corpo 1 (si intende una velocita' in direzione del corpo 2. Accetta valori negativi):\n");
    scanf("%lf",&v1);
    v1max = v1;
	printf("\nInserisci la massa del corpo n'2. Se si inserisce 0 si potra' scegliere tra dati predefiniti:\n");
    scanf("%lf", &m2);
    if(m2 == 0) {
    	do{
			printf("O: Terra\n1: Luna\n2: Sole\n3: Marte\n4: Giove\n5: Sagittarius A* (Buco nero al centro della Via Lattea)\n");
			scanf("%i",&sel);
		}
		while(sel > 5||sel < 0);
		m2 = datiM[sel];
		r2 = datiR[sel];
		goto input3;
	}
    printf("Inserisci il raggio del corpo n'2. Inserire 0 per considerare un corpo puntiforme:\n");
    scanf("%lf", &r2);
    input3:
    printf("Inserisci la velocita' iniziale del corpo 2 (si intende una velocita' in direzione del corpo 1. Accetta valori negativi):\n");
    scanf("%lf",&v2);
    v2max = v2;
    distmin = r1 + r2;
	printf("\nInserisci distanza tra i due corpi. Per inserire la distanza tra le due superfici inserire 1, poi il valore. Se si vuole inserire la distanza tra i due centri di massa inserire 2, poi il valore. Se si inserisce 0 si potra' scegliere tra dati predefiniti. Se si vuole sapere di di piu' sulle distanze inserire un numero negativo. ATTENZIONE! Se viene inserito 1 ricordarsi di digitare un valore maggiore alla distanza minima di %f m\n",distmin);
    do {
		scanf("%i", &sel);
		if(sel<0) {
			printf("Le distanze tra pianeti e sole è la distanza media fra i due. Le distanze fra pianeti sono distanze minime.");
		}
	}
	while(sel<0||sel>2);
	if(sel == 0) {
    	do{
			printf("O: Terra-Luna\n1: Terra-Sole\n2: Terra-Marte\n3: Sole-Marte\n4: Sole-Giove\n5: Terra-Giove\n6: Sagittarius A*-Sistema Solare\n");
			scanf("%i",&sel);
		}
		while(sel > 7||sel < 0);
		d = datiD[sel];
		goto input4;
	}
	scanf("%lf",&d);
	if(sel == 2) {
    	d = d - r1 - r2;
	}
	if(d < 0) {
		printf("ATTENZIONE! La distanza tra le superfici dei due corpi e' minore a zero! Questo significa che i due corpi sono l'uno dentro l'altro. Il programma si chiudera'...");
		for(;;) {
                }
	}
	input4:
	printf("Decidi l'intervallo di tempo tra un calcolo e quello successivo in secondi (piu' il numero e' vicino a 0 piu' i calcoli saranno precisi ma occorreranno piu' risorse per svolgerlo):\n");
    do {
    	scanf("%lf", &t);
	}
	while(t<=0);
    long long int  count = 0;
    double distance = 0;

    std::cout.precision(6);
    std::cout << std::fixed;
    for (;;)
    {
        separation = d - distance;
        double F = G * m1 * m2 / (separation*separation);
		double a1 = F / m1;
        double a2 = F / m2;
        threadlist[i].worker = thread(Autovelox(v1, a1, v2, a2, v1max, v2max),&threadlist[i].result);
		
		double s1 = v1 * t;
        double s2 = v2 * t;
		
        count++;
        distance = distance + s1 + s2;

		caricamento = 100 - (100 * (d-distance)) / d;
        if (count%1000000LL == 0)
        {
            days = count * t / 86400;
            std::cout << "tempo: " << std::setw(12) << count
                      << "    distanza: " << std::setw(18) << separation
                      << "  giorni: "       << std::setw(14) << days
                      << "  \ns1: "         << std::setw(14) << s1
                      << "  s2: "         << std::setw(14) << s2
                      << "  v1: "         << std::setw(14) << v1
                      << "  v2: "         << std::setw(14) << v2
			 		  << "  \nCaricamento... " << caricamento << "% della distanza iniziale completato..."
                      << '\n';
        }
		ao1 = a1;
		ao2 = a2;
        if (distance >= d) {
                            time = count * t;
							sdiff1 = (-0.5) * separation * s1 / s2;
                            sdiff2 = -(separation + sdiff1);
							time = time - (2 * sdiff1 / ao1 + 2 * sdiff1 / ao1);
                            separation = separation + sdiff1 + sdiff2;
                            break;
        }
    }
	for(int i = 0;i < n;i++) {
		threadlist[i].worker.join();
	}
    v1k = v1max * 3.6;
    v2k = v2max * 3.6;
    conth = days*24;
    time = conth * 60;
    cout << "\nCALCOLI COMPLETATI\n";
    wait(2);
	cout << "\nSe questo valore e' 0 i calcoli sono corretti: " << separation << "\n";
	printf("Dati: m1 = %lf m2 = %lf\nd = %lf (tra le superfici)\nI corpi collasseranno su se stessi in %lf s (%f h o %f giorni)", m1,m2,d,time,conth,days);
	printf("\nLa velocita' massima raggiunta dal corpo 1 e' %lf m/s o %lf km/h\nLa velocita' massima raggiunta dal corpo 2 e' %lf m/s o %lf km/h\n", v1max,v1k,v2max,v2k);
    system("pause");
}
So - did you add the "-std=c++11" as suggested?
Thanks, I thought I had to download a new library.
Now there is another error tho
I am trying to assign an std::thread to a variable

1
2
v1 = threadlist[1].worker = thread(Autovelox1(v1, v1max, t, a1),&threadlist[2].result);
v2 = threadlist[2].worker = thread(Autovelox2(v2, v2max, t, a2),&threadlist[3].result);		


the compiler does not like that. How can I assign to variable a function in multithreading?
Hi,

Some observations unrelated to your problem:

Why do you use printf and scanf ? If you don't like std::cout and std::cin boost has a format library that is safe to use.
http://www.boost.org/doc/libs/1_55_0/libs/format/doc/format.html

If you were to stick with scanf , make use of it's return value to see that it worked.

There is no need to have massive print statements that are 5 screens wide: the output can built up in stages. Either with several calls to std::cout , or put part of the staement starting with << on a new line in the code. The compiler doesn't care if statements are split across lines, it's looking for the semicolon:

1
2
3
4
5
6
std::cout << "A big line of output "
               << "with more and more stuff "
               << "and yet more and more "
               << " with extra, just to make sure. "
                << "just in case you missed it, a little bit more. "
               << "That's it all finished now\n";


I see you are already doing this, why use printf the way you do?

If you want to show a menu, that should be in it's own function.

If you configure your editor to convert tabs to 4 spaces say, then your code will display better here. This site converts tabs to 8 spaces, resulting in unwanted extra indenting.

Also not keen on your use of infinite for loops. Especially the one on line 135 ending on line 176. The solitary break is inside an if statement, which might be a potential worry. Can you come up with a proper end condition to be used with a while statement?

Don't use goto, it should be avoided if at all possible. That's general advice, sometimes experts have a valid reason for using it.

Your code could do with more functions, and this might help solve the goto issue. There is kind of a rule that functions should not exceed 40 LOC, including main. Some go for even less. Sometimes a function with 4 LOC can improve readability a great deal. Any compound statement (in braces) is a candidate for a function.

Consider using std::size_t instead of long long int count = 0;

Avoid using namespace std; just put std:: before each std thing. Pulling in all of std might bite you one day. For example std::distance, will be used if you #include the iterator header. So avoid those potential dramas by using std::

Ideally one should have their own code in it's own namespaces.

If using a do loop, put the while part on the same line as the closing brace.

Avoid "magic numbers" like 3.6 24 60 86400 in your code, make them const variables.

With functions, some of the parameters could be marked const.

Try to avoid declaring multiple variables on 1 line. Ideally delay the declaration until you have a sensible value to assign to it, otherwise 1 variable per line, with initialisation to something, and a comment about it's expected valid range. Un-initialised variables are one of the biggest source of errors.

If you do division anywhere, it's prudent to make sure that the divisor is not zero, or near enough to zero to be a problem. So consider explicitly checking that with code.

Try to make use of the compound operators += and -= and others.

Another convention is to forward declare your functions before main, then put their definitions after main. Then one doesn't have to go looking for main, it always at the top of the file.

I had this output from the cpp.sh compiler with all the warnings turned on:

19:28: error: 'a1' has not been declared 
19:32: error: 'v2' has not been declared 
19:36: error: 'a2' has not been declared 
19:40: error: 'v1max' has not been declared 
19:47: error: 'v2max' has not been declared In function 'void Autovelox(double, int, int, int, int, int)': 
21:10: error: 'L' was not declared in this scope 
22:20: error: 'a1' was not declared in this scope 
22:25: error: 't' was not declared in this scope 
24:12: error: 'L' was not declared in this scope 
27:12: error: 'v2' was not declared in this scope 
27:17: error: 'L' was not declred in this scope 
28:20: error: 'a2' was not declared in this scope 
28:25: error: 't' was not declared in this scope 
30:6: error: 'v2' was not declared in this scope 
30:12: error: 'L' was not declared in this scope 
33:20: error: 'v1max' was not declared in this scope 
36:10: error: 'v2' was not declared in this scope 
36:20: error: 'v2max' was not declared in this scope In function 'int main()': 
58:12: error: 'ThreadItem' was not declared in this scope 
58:22: error: template argument 1 is invalid 
58:22: error: template argument 2 is invalid 
58:34: error: invalid type in declaration before ';' token 
59:13: error: request for member 'resize' in 'threadlist', which is of non-class type 'int' 
141:21: error: invalid types 'int[double]' for array subscript 
41:92: error: invalid types 'int[double]' for array subscript 
141:100: error: invalid use of void expression 
175:15: error: invalid types 'int[int]' for array subscript 
45:18: warning: unused variable 'L' [-Wunused-variable] 
48:26: warning: unused variable 'p' [-Wunused-variable] 
48:29: warning: unused variable 'F' [-Wunused-variable] 
48:32: warning: unused variable 'a1' [-Wunused-variable] 
48:36: warning: unused variable 'a2' [-Wunused-variable] 
48:40: warning: unused variable 's1' [-Wunused-variable] 
48:44: warning: unused variable 's2' [-Wunused-variable] 
48:51: warning: unused variable 'cont' [-Wunused-variable] 
48:62: warning: unused variable 'distparz' [-Wunused-variable] 
48:104: warning: unused variable 'contg' [-Wunused-variable]


Those warnings come from using the -Wall -Wextra -pedantic flags, even then there are some which are not turned on. I use these extra ones as well:

http://www.cplusplus.com/forum/general/183731/#msg89920
Hello again,

Just on the menu thing, I like to do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
bool Quit = false;

while(!Quit) {
   // Show Menu using a function call, make sure it has a quit option
   // get user selection

  switch (MenuOption) {
      case 1:
         // function call to do option 1 stuff
         break;
      case 2:
         // function call to do option 2 stuff
         break;
// ...
// ...
      case 6:  // user wants to quit
         Quit = true;
         break;
      default:  // catch bad input
         std::cout << "Bad Menu input try again \n";
         break;
  } //end switch
} // end while 


I think this idea might help organise all of your code much better.

Good Luck !!
Thank you IdeasMan for your reply, those tips can be really useful.
They teach me C++ at school and the teacher says I should use scanf and printf because some compilers don't support std::cout. I'll look up a way to make that break statement more reliable. I am also trying to use functions more (fixing the code right now :D). About all those errors: I noticed before posting this and now they are all fixed. I will also do the menu. Thanks for all the help, I'll post the new code once it's ready! By the way, how can I assign to variable a function with the multithreading stuff? I know it's useless in this program since all the stuff has to happen simultaneously but still....
Hi,

I am glad you found the info useful :+)

They teach me C++ at school and the teacher says I should use scanf and printf because some compilers don't support std::cout.


Well IMO that's just ridiculous. If a C++ compiler can't do that it's not a C++ compiler. Maybe they mean something like the ancient TurboC++v3.0 which is about 25 years old, and was prior to the std namespace. Rather, prior to namespaces at all. Apparently TCV3 is still widely used in India, and is a requirement for school exams.

By the way, how can I assign to variable a function with the multithreading stuff?


I don't know anything about threading, maybe someone else can help you there.

Good Luck !!
I am trying to assign an std::thread to a variable
It doesn't make sense to call such a function as a thread since there is no need to paralize anything (because the main thread is not blocked).

However, see:

http://www.cplusplus.com/reference/thread/thread/thread/

Change this:

thread(Autovelox1(v1, v1max, t, a1),&threadlist[2].result);

To

thread(Autovelox, v1, a1, v2, a2, v1max, v2max);
Thanks coder, I'll look into multithreading better when I'll make a program that actually supports it better. I decided not to use multithreading on this one because it seems pretty useless since all the processing has to happen sequentially.

IdeasMan, I am fixing the code, what do you think? I havn't done everything, still WIP

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
#include <iostream>
#include <iomanip>
#include <cmath>
#include <sstream>
#include <time.h>
#include <stdlib.h>
#include <string>
using namespace std;

void wait(int seconds)
{
	clock_t endwait;
	endwait = clock() + seconds * CLOCKS_PER_SEC;
	while (clock() < endwait) {}
}

double Autovelox (double v, double vmax, double t, double a)
{
	const double L = 299792458;
	if(v < L) {
        v = v + a * t;
	} 
	else {
		v = L;			
	}
	return v;
}

int main()
{

    int selM;
	do {
	const double G = 6.67408e-11;  // costante gravitazionale.
    const double L = 299792458;  // velocità della luce.
    const int M = 86400 //60*60*24

    double m1, m2, d, i, p, F, s1, s2, t, cont,days, distparz,time, v1, v2, r1, r2, v1k, v2k, conth, contg, distmin, a1, v1max, v2max, a2, sdiff1, sdiff2;
	double datiR[6] = { 6.371e+6 , 1.737e+6  , 6.96e+8 , 3.39e+6 , 6.9911e+7 , 13e+9 };
	double datiM[6] = { 5.972e+24 , 7.34767309e+22 , 1.989e+30 , 3.844e+8 , 1.898e+27 , 8.573e+36 };
	std::string datiC[6] = {"Terra" , "Luna" , "Sole" , "Marte" , "Giove" , "Sagittarius A"};
	double datiD[7] = { 3.84403e+8 , 1.496e+11 , 9.2451e+10 , 2.279e+11 , 7.785e+11 , 6.287e+11 , 2.46e+20 };
	std::string datiDS[7] = {"Terra-Luna" , "Terra-Sole" , "Terra-Marte" , "Sole-Marte" , "Sole-Giove" , "Terra-Giove" , "Sagittarius A*-Sistema Solare"};
	std::string m1s,m2s,r1s,r2s,dStr;
    double separation; 
    int sel;
    float caricamento;
    
	stringstream ss (stringstream::in | stringstream::out);
	
	m1 = datiM[0];
	r1 = datiR[0];
	m2 = datiM[1];
	r2 = datiR[1];
	v1 = 0;
	v2 = 0;
	t = 0.01;
	d = datiD[0];
	sel = 0;
	
	m1s = "Terra";
	r1s = "Terra";
	m2s = "Luna";
	r2s = "Luna";
	dStr = "Terra-Luna";
	printf("COLLASS-O-MATIC 3.0\nQuesto programma analizza due corpi che collassano su se stessi senza attriti.\nTutti i dati inseriti vanno interpretati nelle unita' dell'MKS.\n");
	do {
		cout << "\n\nScegliere cio' che si vuole modificare o confermare per iniziare a calcolare:" <<
				"\n0: massa corpo 1 (selezione attuale: " << m1s << ")\n1: raggio corpo 1 (selezione attuale: " << r1s << ")\n2: velocita' corpo 1 (selezione attuale: " << v1 << ")" <<
				"\n3: massa corpo 2 (selezione attuale: " << m2s << ")\n4: raggio corpo 2 (selezione attuale: " << r2s << ")\n5: velocita' corpo 2 (selezione attuale: " << v2 << ")" <<
				"\n6: distanza tra i due corpi (selezione attuale: " << dStr << ")\n7: intervallo di tempo (seleziona attuale: " << t << ")\n8: CONFERMA\n9: ESCI\n";
		do {
			std::cin >> selM;
		}
		while(selM < 0 && selM > 9);
		switch(selM) {
			case 0 :	printf("Inserisci la massa del corpo n'1. Se si inserisce 0 si potra' scegliere tra dati predefiniti:\n");
		    			scanf("%lf", &m1);
					    if(m1 == 0) {
					    	do{
								printf("O: Terra\n1: Luna\n2: Sole\n3: Marte\n4: Giove\n5: Sagittarius A* (Buco nero al centro della Via Lattea)\n");
								scanf("%i",&sel);
							}
							while(sel > 5||sel < 0);
							m1 = datiM[sel];
							m1s = datiC[sel];
						}
						else {
							ss.str("");
							ss.clear();
							ss << m1;
							m1s = ss.str();
						}
						break;
		    case 1 : 	printf("Inserisci il raggio del corpo n'1. Se si inserisce -1 si potra' scegliere tra dati predefiniti:\n");
		    			scanf("%lf", &r1);
					    if(r1 < 0) {
					    	do{
								printf("O: Terra\n1: Luna\n2: Sole\n3: Marte\n4: Giove\n5: Sagittarius A* (Buco nero al centro della Via Lattea)\n");
								scanf("%i",&sel);
							}
							while(sel > 5||sel < 0);
							r1 = datiR[sel];
							r1s = datiC[sel];
						}
						else {
							ss.str("");
							ss.clear();
							ss << r1;
							r1s = ss.str();
						}
						break;
		    case 2 : 	printf("Inserisci la velocita' iniziale del corpo 1 (si intende una velocita' in direzione del corpo 2. Accetta valori negativi):\n");
						scanf("%lf",&v1);
						v1max = v1;
						break;
			case 3 :		printf("\nInserisci la massa del corpo n'2. Se si inserisce 0 si potra' scegliere tra dati predefiniti:\n");
		    			scanf("%lf", &m2);
					    if(m2 == 0) {
					    	do{
								printf("O: Terra\n1: Luna\n2: Sole\n3: Marte\n4: Giove\n5: Sagittarius A* (Buco nero al centro della Via Lattea)\n");
								scanf("%i",&sel);
							}
							while(sel > 5||sel < 0);
							m2 = datiM[sel];
							m2s = datiC[sel];
						}
						else {
							ss.str("");
							ss.clear();
							ss << m2;
							m2s = ss.str();
						}
						break;
			case 4 :	printf("\nInserisci il raggio del corpo n'2. Se si inserisce -1 si potra' scegliere tra dati predefiniti:\n");
		    			scanf("%lf", &r2);
					    if(r2 < 0) {
					    	do{
								printf("O: Terra\n1: Luna\n2: Sole\n3: Marte\n4: Giove\n5: Sagittarius A* (Buco nero al centro della Via Lattea)\n");
								scanf("%i",&sel);
							}
							while(sel > 5||sel < 0);
							r2 = datiR[sel];
							r2s = datiC[sel];
						}
						else {
							ss.str("");
							ss.clear();
							ss << r2;
							r2s = ss.str();
						}
						break;
		    case 5 :	printf("Inserisci la velocita' iniziale del corpo 2 (si intende una velocita' in direzione del corpo 1. Accetta valori negativi):\n");
		    			scanf("%lf",&v2);
		    			v2max = v2;
		    			break;
		    case 6 :	printf("\nInserisci distanza tra i due corpi. Per inserire la distanza tra le due superfici inserire 1, poi il valore. Se si vuole inserire la distanza tra i due centri di massa inserire 2, poi il valore. Se si inserisce 0 si potra' scegliere tra dati predefiniti. Se si vuole sapere di di piu' sulle distanze inserire un numero negativo. ATTENZIONE! Se viene inserito 1 ricordarsi di digitare un valore maggiore alla distanza minima di %f m\n",distmin);
					    do {
							scanf("%i", &sel);
							if(sel<0) {
								printf("Le distanze tra pianeti e sole è la distanza media fra i due. Le distanze fra pianeti sono distanze minime.");
							}
						}
						while(sel<0||sel>2);
						if(sel == 0) {
					    	do{
								printf("O: Terra-Luna\n1: Terra-Sole\n2: Terra-Marte\n3: Sole-Marte\n4: Sole-Giove\n5: Terra-Giove\n6: Sagittarius A*-Sistema Solare\n");
								scanf("%i",&sel);
							}
							while(sel > 7||sel < 0);
							d = datiD[sel];
							dStr = datiDS[sel];
						}
						else {
							scanf("%lf",&d);
							if(sel == 1) {
						    	d = d + r1 + r2;
							}
							ss.str("");
							ss.clear();
							ss << d;
							dStr = ss.str();
							dStr += " tra le superfici";
						}
						if(d < 0) {
							printf("ATTENZIONE! La distanza tra le superfici dei due corpi e' minore a zero! Questo significa che i due corpi sono l'uno dentro l'altro. Il programma si chiudera'...");
							exit(0);
						}
						break;
			case 7 :	printf("Decidi l'intervallo di tempo tra un calcolo e quello successivo in secondi (piu' il numero e' vicino a 0 piu' i calcoli saranno precisi ma occorreranno piu' risorse per svolgerlo):\n");
					    do {
					    	scanf("%lf", &t);
						}
						while(t<=0);
						break;
			case 8 :	break;
			case 9 : 	break;
			default :   printf("\nInserire un numero compreso tra 0 e 9\n");
						break;
		}
	}
	while(selM < 8);
	if(selM == 9)
		exit(0);
    long long int count = 0;
    double distance = 0;
    count = 0;
    std::cout.precision(6);
    std::cout << std::fixed;
    distmin = r1 + r2;
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
	for (;;)
    {
		separation = d - distance;
        F = ( m1 + m2 ) * G / (separation*separation);
		a1 = F / m1;
        a2 = F / m2;
		v1 = Autovelox(v1, v1max, t, a1);
		v2 = Autovelox(v2, v2max, t, a2);
		s1 = v1 * t;
        s2 = v2 * t;
        count++;
        distance += s1 + s2;
		
		caricamento = 100 - (100 * (d-distance)) / d;
        if (count%1000000LL == 0)
        {
            //cout << endl << "F:  " << F << endl << a1 << endl << m1;
			time = count * t;
            days = count * t / M;
			std::cout << "tempo: " << std::setw(12) << time
                      << "    distanza: " << std::setw(18) << separation
                      << "  giorni: "       << std::setw(14) << days
                      << "  \ns1: "         << std::setw(14) << s1
                      << "  s2: "         << std::setw(14) << s2
                      << "  v1: "         << std::setw(14) << v1
                      << "  v2: "         << std::setw(14) << v2
			 		  << "  \nCaricamento... " << caricamento << "% della distanza iniziale completato..."
                      << '\n';
        }
        if (distance >= d) {
                            time = count * t;
							sdiff1 = (-0.5) * separation * s1 / s2;
                            sdiff2 = -(separation + sdiff1);
							time = time - (2 * sdiff1 / a1 + 2 * sdiff1 / a1);
                            separation = separation + sdiff1 + sdiff2;
                            break;
        }
    }
    v1k = v1max * 3.6;
    v2k = v2max * 3.6;
    time = count*t;
    conth = time/3600;
	days = conth/24;
		cout << "\nCALCOLI COMPLETATI\n";
		wait(1);
		cout << "\nSe questo valore e' 0 i calcoli sono corretti: " << separation << "\n";
		printf("Dati: m1 = %.5lf m2 = %.5lf\nd = %.5lf (tra i centri di massa)\n\nI corpi collasseranno su se stessi in %.2lf s (%.2f h o %.2f giorni)\n", m1,m2,d,time,conth,days);
		printf("\nL'accelerazione nell'ultimo %lf s del corpo 1 e' stata %lf m/s2. Quella del corpo 2 invece e' stata %.2lf m/s2\n",t,a1,a2);
		printf("\nLa velocita' massima raggiunta dal corpo 1 e' %.2lf m/s o %.0lf km/h\nLa velocita' massima raggiunta dal corpo 2 e' %.2lf m/s o %.0lf km/h\n\n", v1max,v1k,v2max,v2k);
	}
	while(selM != 9);
    system("pause");
}
IdeasMan, I am fixing the code, what do you think? I havn't done everything, still WIP


It's good you have implemented a couple of things I mentioned, there is quite a bit more to do.

Good code should be easy to follow, at the moment IMO your code is a long way from that.

Now that you have the switch you don't need the outer do loops. You haven't used the bool Quit to control the switch. Don't need the do loop on line 72, the switch takes care of that.

Please try to avoid really long statements. You still have very long printf statements. Configure your editor to fix the tab problem.

Desperately need more functions.

Haven't checked for divide by zero for any of the statements involving division.
Topic archived. No new replies allowed.