I don't know why it doesn't make the (+)

I don't know why it doesn't make the (+)

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
#include <iostream>
using namespace std;

void main()
{
	
	cout<< "\t\t***PEDIR DATOS***\n\n\n";
	int numfrac;
	cout <<"How many fractions do you whant: ";
	cin>>numfrac;
	//estructurando
	struct fraccion
	{
		int numerador;
		int denominador;
	};
	fraccion *frac = new fraccion[numfrac];
	for ( int i = 0; i<numfrac; i++ )
	{
		cout<<"f"<<i+1<<":"<<endl;
		cout<<"\t NUMERADOR"<<i+1<<" = ";
		cin >> frac[i].numerador;
		cout<<"\t DENOMINADOR"<<i+1<<" = ";	
		cin >> frac[i].denominador;
	}
	system("cls"); 

	if (numfrac <1)
		{cout<<"RESULTADO = 0";}
	else if(numfrac == 1)
		{cout<<"RESULTADO = "<<frac[1].numerador<<endl;cout<<"            -"<<endl;cout<<"            "<<frac[1].denominador;}
	else if (numfrac > 1)
	{
	
	while(1)
	{
		short option;
		cout<<"Say us what is what you whant to do"<<endl;
		cout << "1) +"<<endl;
		cout << "2) -"<<endl;
		cout << "3) x"<<endl;
		cout << "4) /"<<endl;
		cout << "5) a^b^c^d^e..."<<endl;
		cout << "6) CLEAR THE SCREEN"<<endl;
		cout << "7 u otro) END THE PROGRAM"<<endl;
		cin >> option;
		cout <<endl;
double suma = 0;
		if (option ==1)
			{	
				
				for ( int i = 0; i<numfrac; i++ )
				{
					suma = suma +(frac[i].numerador/frac[i].denominador);
				}
				cout<<"RESULTADO: "<< suma<< endl;
			}
		else if (option == 2)
			{
				system("cls"); 
			}
		else if (option == 3)
			{
				system("cls"); 
			}
		else if (option == 4)
			{
				system("cls"); 
			}
		else if (option == 5)
			{
				system("cls"); 
			}
		else if (option == 6)
			{
				system("cls"); 
			}
		else
			{
				exit(0);
			}
		
	}
	}
	delete [] frac;
}
Last edited on
What do you mean by "doesn't make"?

Integer division discards remainder. Try
suma += static_cast<double>( frac[i].numerador ) / frac[i].denominador;
Line 85: You'll never free the memory because there is no way to reach this line. Your compiler should have warned you of this.
Sorry because of my bad english, and thaks because of the ideas, now it works, but please somebody can expline me why we have to do static_cast<double> ???????????????????????????????????????????????

I don't finish the program, but the option 1 works perfectly

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
#include <iostream>
using namespace std;

void main()
{
	
	cout<< "\t\t***PEDIR DATOS***\n\n\n";
	int numfrac;
	cout <<"How many fractions do you whant: ";
	cin>>numfrac;
	//estructurando
	struct fraccion
	{
		int numerador;
		int denominador;
	};
	fraccion *frac = new fraccion[numfrac];
	for ( int i = 0; i<numfrac; i++ )
	{
		cout<<"f"<<i+1<<":"<<endl;
		cout<<"\t NUMERADOR"<<i+1<<" = ";
		cin >> frac[i].numerador;
		cout<<"\t DENOMINADOR"<<i+1<<" = ";	
		cin >> frac[i].denominador;
	}
	system("cls"); 

	if (numfrac <1)
		{cout<<"RESULTADO = 0";}
	else if(numfrac == 1)
		{cout<<"RESULTADO = "<<frac[1].numerador<<endl;cout<<"            -"<<endl;cout<<"            "<<frac[1].denominador;}
	else if (numfrac > 1)
	{
	
	while(1)
	{
		short option;
		cout<<"Say us what is what you whant to do"<<endl;
		cout << "1) +"<<endl;
		cout << "2) -"<<endl;
		cout << "3) x"<<endl;
		cout << "4) /"<<endl;
		cout << "5) a^b^c^d^e..."<<endl;
		cout << "6) CLEAR THE SCREEN"<<endl;
		cout << "7 u otro) END THE PROGRAM"<<endl;
		cin >> option;
		cout <<endl;
		double suma = 0.0;
		double division=0.0;
		if (option ==1)
			{	
				for ( int j = 0; j<numfrac; j++ )
				{
					suma += static_cast<double>( frac[j].numerador ) / frac[j].denominador;
					
				}
				cout<<"RESULTADO: "<< suma<< endl;
delete [] frac;
			}
		else if (option == 2)
			{
				system("cls"); 
			}
		else if (option == 3)
			{
				system("cls"); 
			}
		else if (option == 4)
			{
				system("cls"); 
			}
		else if (option == 5)
			{
				system("cls"); 
			}
		else if (option == 6)
			{
				system("cls"); 
			}
		else
			{
				exit(0);
			}
		
	}
	}
}
Last edited on
Other 2 question refering to this peace of code:

1st) ¿Why apears a problem when i press 1 in the execution time?
2nd)¿Why the operation (-) it doesn't do what it has to do ?¿why appears all the time 0?

The code actually:

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

#include <iostream>
using namespace std;

void main()
{
	
	cout<< "\t\t***ASK INFORMATION***\n\n\n";
	int numfrac;
	cout <<"How many fractions do you whant: ";
	cin>>numfrac;
	//estructurando
	struct fraccion
	{
		int numerador;
		int denominador;
	};
	fraccion *frac = new fraccion[numfrac];
	for ( int i = 0; i<numfrac; i++ )
	{
		cout<<"f"<<i+1<<":"<<endl;
		cout<<"\t NUMERADOR"<<i+1<<" = ";
		cin >> frac[i].numerador;
		cout<<"\t DENOMINADOR"<<i+1<<" = ";	
		cin >> frac[i].denominador;
	}
	system("cls"); 

	if (numfrac <1)
		{cout<<"RESULTADO = 0";}
	else if(numfrac == 1)
		{cout<<"RESULTADO = "<<frac[1].numerador<<endl;cout<<"            -"<<endl;cout<<"            "<<frac[1].denominador;}
	else if (numfrac > 1)
	{
	
	while(1)
	{
		short option;
		cout<<"Say us what is what you whant to do"<<endl;
		cout << "1) +"<<endl;
		cout << "2) -"<<endl;
		cout << "3) x"<<endl;
		cout << "4) /"<<endl;
		cout << "5) a^b^c^d^e..."<<endl;
		cout << "6) CLEAR THE SCREEN"<<endl;
		cout << "7 u otro) END THE PROGRAM"<<endl;
		cin >> option;
		cout <<endl;
		double suma = 0.0;
		double division=0.0;
		if (option ==1)
			{	
						system("cls"); 
						for ( int j = 0; j<numfrac; j++ )
						{
							suma += static_cast<double>( frac[j].numerador ) / frac[j].denominador;
						}
						cout<<"RESULTADO: "<< suma<< endl;
						delete [] frac;
			}
		else if (option == 2)
			{
						system("cls"); 
						double resta = static_cast<double>( frac[1].numerador ) / frac[1].denominador;
						for ( int jj =1 ; jj<numfrac; jj++ )
						{
							resta -= static_cast<double>( frac[jj].numerador ) / frac[jj].denominador;
						}
						cout<<"RESULTADO: "<<resta<< endl;
						delete [] frac;
			}
		else if (option == 3)
			{
				system("cls"); 
			}
		else if (option == 4)
			{
				system("cls"); 
			}
		else if (option == 5)
			{
				system("cls"); 
			}
		else if (option == 6)
			{
				system("cls"); 
			}
		else
			{
				exit(0);
			}
		
	}
	}
	
}
Moving delete frac to lines 59 and 70 will work the first time through the loop, but will not work the second time through the loop.

You allocate frac at line 18. Lets assume you enter 1 the first time through the loop. frac now gets deleted at line 59. Lets assume you choose option 1 again the second time through the loop. You proceed to calculate using frac at line 56, but frac has been deleted and now points to garbage.
Ok, I think that I have impluve a lot the program, but I ned to know what happens in the line XX with the pow() I don't know why appear this error: "Error 1 error C2039: 'elevacion' : is not a member of 'main::fraccion' "

line ====> 101

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
#include <iostream>
using namespace std;
#include <math.h>

void main()
{
	
	cout<< "\t\t***ASK INFORMATION***\n\n\n";
	int numfrac;
	cout <<"How many fractions do you whant: ";
	cin>>numfrac;
	//estructurando
	struct fraccion
	{
		double numerador;
		double denominador;
	};
	fraccion *frac = new fraccion[numfrac];
	for ( int i = 0; i<numfrac; i++ )
	{
		cout<<"f"<<i+1<<":"<<endl;
		cout<<"\t NUMERADOR "<<i+1<<" = ";
		cin >> frac[i].numerador;
		cout<<"\t DENOMINADOR "<<i+1<<" = ";	
		cin >> frac[i].denominador;
	}
	system("cls"); 

	if (numfrac <1)
		{cout<<"RESULTADO = Si no quiere hacer operaciones, adios"; exit(0);}
	else if(numfrac == 1)
		{cout<<"RESULTADO = "<<frac[0].numerador<<endl;cout<<"            -"<<endl;cout<<"            "<<frac[0].denominador<<endl;}
	else if (numfrac > 1)
	{
	
	while(1)
	{
		short option;
		cout<<"Say us what is what you whant to do"<<endl;
		cout << "1) a+b+c+..."<<endl;
		cout << "2) a-b-c-..."<<endl;
		cout << "3) axbxcx..."<<endl;
		cout << "4) a/b/c/..."<<endl;
		cout << "5) a^b^c^d^e..."<<endl;
		cout << "6) CLEAR THE SCREEN"<<endl;
		cout << "7 u otro) END THE PROGRAM"<<endl;
		cin >> option;
		cout <<endl;
		
		if (option ==1)
			{	
						double suma = 0;
						system("cls"); 
						for ( int j = 0; j<numfrac; j++ )
						{
							suma += ( frac[j].numerador)  / (frac[j].denominador);
						}
						cout<<"RESULTADO: "<< suma<< endl;
						cout<<endl;
			}
		else if (option == 2)
			{
						double resta = ( frac[0].numerador ) / frac[0].denominador;
						system("cls"); 
						for ( int j =1 ; j<numfrac; j++ )
						{
							resta -=( frac[j].numerador ) / frac[j].denominador;
						}
						cout<<"RESULTADO: "<<resta<< endl;
						cout<<endl;
			}
		else if (option == 3)
			{
						system("cls"); 
						double multiplicacion = ( frac[0].numerador ) / frac[0].denominador; 
						for ( int j =1 ; j<numfrac; j++ )
						{
							multiplicacion *= frac[j].numerador  / frac[j].denominador;
						}
						cout<<"RESULTADO: "<<multiplicacion<< endl;
						cout<<endl;
			}
		else if (option == 4)
			{
						system("cls"); 
						double division = ( frac[0].numerador ) / frac[0].denominador; 
						for ( int j =1 ; j<numfrac; j++ )
						{
							division /= frac[j].numerador  / frac[j].denominador;
						}
						cout<<"RESULTADO: "<<division<< endl;
						cout<<endl; 
			}
		else if (option == 5)
			{
						system("cls"); 
						double elevacion = ( frac[0].numerador ) / frac[0].denominador; 
						double division=0;
						for ( int j =1 ; j<numfrac; j++ )
						{
							division  = frac[j].elevacion  / frac[j].elevacion;
							elevacion = pow ( elevacion ,division );
						}
						cout<<"RESULTADO: "<<elevacion<< endl;
						cout<<endl; 
			}
		else if (option == 6)
			{
				system("cls"); 
			}
		else
			{
				delete [] frac;
				exit(0);
			}
		
	}
	}
	
}
'elevacion' : is not a member of 'main::fraccion' 
1
2
3
4
5
6
7
8
9
10
struct fraccion
{
  double numerador;
  double denominador;
  // do you see name 'elevacion' here? The compiler does't.
};
////

fraccion frac;
double x = frac.elevacion; // error C2039 

jaja Thanks keskiverto because of the solution, and because of the sarcasm ;P

The code finally is:

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
#include <iostream>
using namespace std;
#include <math.h>

void main()
{
	
	cout<< "\t\t***ASK INFORMATION***\n\n\n";
	int numfrac;
	cout <<"How many fractions do you whant: ";
	cin>>numfrac;
	//estructurando
	struct fraccion
	{
		double numerador;
		double denominador;
	};
	fraccion *frac = new fraccion[numfrac];
	for ( int i = 0; i<numfrac; i++ )
	{
		cout<<"f"<<i+1<<":"<<endl;
		cout<<"\t NUMERADOR "<<i+1<<" = ";
		cin >> frac[i].numerador;
		cout<<"\t DENOMINADOR "<<i+1<<" = ";	
		cin >> frac[i].denominador;
	}
	system("cls"); 

	if (numfrac <1)
		{cout<<"RESULTADO = Si no quiere hacer operaciones, adios"; exit(0);}
	else if(numfrac == 1)
		{cout<<"RESULTADO = "<<frac[0].numerador<<endl;cout<<"            -"<<endl;cout<<"            "<<frac[0].denominador<<endl;}
	else if (numfrac > 1)
	{
	
	while(1)
	{
		short option;
		cout<<"Say us what is what you whant to do"<<endl;
		cout << "1) a+b+c+..."<<endl;
		cout << "2) a-b-c-..."<<endl;
		cout << "3) axbxcx..."<<endl;
		cout << "4) a/b/c/..."<<endl;
		cout << "5) a^b^c^d^e..."<<endl;
		cout << "6) CLEAR THE SCREEN"<<endl;
		cout << "7 u otro) END THE PROGRAM"<<endl;
		cin >> option;
		cout <<endl;
		
		if (option ==1)
			{	
						double suma = 0;
						system("cls"); 
						for ( int j = 0; j<numfrac; j++ )
						{
							suma += ( frac[j].numerador)  / (frac[j].denominador);
						}
						cout<<"RESULTADO: "<< suma<< endl;
						cout<<endl;
			}
		else if (option == 2)
			{
						double resta = ( frac[0].numerador ) / frac[0].denominador;
						system("cls"); 
						for ( int j =1 ; j<numfrac; j++ )
						{
							resta -=( frac[j].numerador ) / frac[j].denominador;
						}
						cout<<"RESULTADO: "<<resta<< endl;
						cout<<endl;
			}
		else if (option == 3)
			{
						system("cls"); 
						double multiplicacion = ( frac[0].numerador ) / frac[0].denominador; 
						for ( int j =1 ; j<numfrac; j++ )
						{
							multiplicacion *= frac[j].numerador  / frac[j].denominador;
						}
						cout<<"RESULTADO: "<<multiplicacion<< endl;
						cout<<endl;
			}
		else if (option == 4)
			{
						system("cls"); 
						double division = ( frac[0].numerador ) / frac[0].denominador; 
						for ( int j =1 ; j<numfrac; j++ )
						{
							division /= frac[j].numerador  / frac[j].denominador;
						}
						cout<<"RESULTADO: "<<division<< endl;
						cout<<endl; 
			}
		else if (option == 5)
			{
						system("cls"); 
						double elevacion = ( frac[0].numerador ) / frac[0].denominador; 
						double division=0;
						for ( int j =1 ; j<numfrac; j++ )
						{
							division  = frac[j].numerador / frac[j].denominador;
							elevacion = pow ( elevacion ,division );
						}
						cout<<"RESULTADO: "<<elevacion<< endl;
						cout<<endl; 
			}
		else if (option == 6)
			{
				system("cls"); 
			}
		else
			{
				delete [] frac;
				exit(0);
			}
		
	}
	}
	
}
Topic archived. No new replies allowed.