I need help understanding this error.

Pages: 12
I am currently on my 6th week of learning C++, and in class, we're learning about how to effectively use the different types of functions to transfer values between themselves and _main.

I'm fairly confident in using the different types of functions, but in one case, I'm getting an error that I'd like help understanding. The program also doesn't output the correct numbers.

My only Error: warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data

----
Requirements for the program:
01) Input the radius of a circle.
02) Output the Circumference
03) Output the Area
04) Use the following functions:
--- void Banner() function
--- void getValue() function
--- value returning computeCircumference() function
--- void computeArea() function
--- void outputData() function
--- value returning goAgain() bool function

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

void banner();
void getValue (int);
int  computeCircumference(int);
void computeArea(int, int);
void outputData(int, int, int);
bool goAgain();

int main(){
   int radius = ' ';
   int area = ' ';

   banner();

   do{
      getValue(radius); 
      int circumference = computeCircumference (radius);
      computeArea (radius, area);
      outputData (radius, circumference, area);
    } while ( goAgain() );
   
return 0;
}

void banner(){
   cout << "======================================================" << endl;
   cout << "--------------Welcome to Project Circle!--------------" << endl;
   cout << "------------------------------------------------------" << endl;
   cout << "This program will allow you to input a circle's raidus" << endl;
   cout << "------------------------------------------------------" << endl;
   cout << "---and will then output the circumference and area.---" << endl;
   cout << "------------------------------------------------------" << endl;
   cout << "---------------------Let's begin!---------------------" << endl;
   cout << "======================================================" << endl;
   cout << endl; cout << endl;
}

void getValue(int R1){

   cout << "Please input the radius of your circle: "; 
   cin  >> R1;

}

int computeCircumference(int R2){
   int C1;
   int pi = 3.14;

   C1 = (2 * pi * R2);

   return C1;

}

void computeArea(int R3, int A1){
	int pi = 3.14;

	A1 = (pi * R3 * R3);

}

void outputData(int R4, int C2, int A2){

   cout << "======================================================" << endl;
   cout << "--------------Information on your circle--------------" << endl; 
   cout << "------------------------------------------------------" << endl;
   cout << "Radius--------: " << R4 << endl;
   cout << "Circumference-: " << C2 << endl;
   cout << "Area----------: " << A2 << endl;
   cout << "------------------------------------------------------" << endl;
   cout << "-------------Your circle is now complete!-------------" << endl;
   cout << "======================================================" << endl;
   cout << endl; cout << endl;
}

bool goAgain(){
   char run2;

   cout << "Do you want to run this program again? (y/n): ";
   cin  >> run2;
   cout << endl; cout << endl;

   if ( run2 == 'y' || run2 == 'Y' )
      return true;
   else
      return false;

}


/*
Personal Notes on Function Types
01)     int  P( );                        Calls function P
02)     int  P(x); Sends x to function P, Calls function P
03) N = int  P( );                        Calls function P, sets N equal to return value
04) N = int  P(x); Sends x to function P, Calls function P, sets N equal to return value
05)     void P( );                        Calls function P
06)     void P(x); Sets x equal to function P's defined values
*/

Last edited on
That sort of warning message comes when u try to cast a larger datatype variable into a smaller compatible datatype variable.
eg:
1
2
3
int i=0;
double k=3.1423;
i=(int) k;

variable i will be able to hold only 3, the .1423 part will be lost.
i suggest u use either float or double variables to get precise values.
@ anirudh sn; I tried using all double datatypes, but I'm getting the same error, this time on line 54. Any ideas?


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

void banner();
void getValue (double);
int  computeCircumference(double);
void computeArea(double, double);
void outputData(double, double, double);
bool goAgain();

int main(){
   double radius = ' ';
   double area = ' ';

   banner();

   do{
      getValue(radius); 
	  int circumference = computeCircumference (radius);
      computeArea (radius, area);
	  outputData (radius, circumference, area);
    } while ( goAgain() );
   
return 0;
}

void banner(){
   cout << "======================================================" << endl;
   cout << "--------------Welcome to Project Circle!--------------" << endl;
   cout << "------------------------------------------------------" << endl;
   cout << "This program will allow you to input a circle's raidus" << endl;
   cout << "------------------------------------------------------" << endl;
   cout << "---and will then output the circumference and area.---" << endl;
   cout << "------------------------------------------------------" << endl;
   cout << "---------------------Let's begin!---------------------" << endl;
   cout << "======================================================" << endl;
   cout << endl; cout << endl;
}

void getValue(double R1){

   cout << "Please input the radius of your circle: "; 
   cin  >> R1;

}

int computeCircumference(double R2){
   double C1;
   double pi = 3.14;

   C1 = (2 * pi * R2);

   return C1;

}

void computeArea(double R3, double A1){
	double pi = 3.14;

	A1 = (pi * R3 * R3);

}

void outputData(double R4, double C2, double A2){

   cout << "======================================================" << endl;
   cout << "--------------Information on your circle--------------" << endl; 
   cout << "------------------------------------------------------" << endl;
   cout << "Radius--------: " << R4 << endl;
   cout << "Circumference-: " << C2 << endl;
   cout << "Area----------: " << A2 << endl;
   cout << "------------------------------------------------------" << endl;
   cout << "-------------Your circle is now complete!-------------" << endl;
   cout << "======================================================" << endl;
   cout << endl; cout << endl;
}

bool goAgain(){
   char run2;

   cout << "Do you want to run this program again? (y/n): ";
   cin  >> run2;
   cout << endl; cout << endl;

   if ( run2 == 'y' || run2 == 'Y' )
      return true;
   else
      return false;

}
line 7
int computeCircumference(double);
modify it as
 
double  computeCircumference(double);

line 20
int circumference = computeCircumference (radius);
modify it as
double circumference = computeCircumference (radius);
and line 48
int computeCircumference(double R2){
to
double computeCircumference(double R2){
Last edited on
Problem solved. Thanks for helping me learn a new datatype. You rock.
^_^
New problem:
The program works, but the output values are way wrong, and the weird thing is, the output is always the same.

The output (everytime):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
======================================================
------------------------------------------------------
This program will allow you to input a circle's raidus
------------------------------------------------------
---and will then output the circumference and area.---
------------------------------------------------------
---------------------Let's begin!---------------------
======================================================


Please input the radius of your circle: 2
======================================================
--------------Information on your circle--------------
------------------------------------------------------
Radius--------: 32
Circumference-: 200.96
Area----------: 32
------------------------------------------------------
-------------Your circle is now complete!-------------
======================================================


Do you want to run this program again? (y/n):




---

Any ideas?

Here's the working code:
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
#include "stdafx.h"
#include <iostream>
using namespace std; 

void banner();
void getValue (double);
double computeCircumference(double);
void computeArea(double, double);
void outputData(double, double, double);
bool goAgain();

int main(){
   double radius = ' ';
   double area = ' ';

   banner();

   do{
      getValue(radius); 
	  double circumference = computeCircumference (radius);
      computeArea (radius, area);
	  outputData (radius, circumference, area);
    } while ( goAgain() );
   
return 0;
}

void banner(){
   cout << "======================================================" << endl;
   cout << "--------------Welcome to Project Circle!--------------" << endl;
   cout << "------------------------------------------------------" << endl;
   cout << "This program will allow you to input a circle's raidus" << endl;
   cout << "------------------------------------------------------" << endl;
   cout << "---and will then output the circumference and area.---" << endl;
   cout << "------------------------------------------------------" << endl;
   cout << "---------------------Let's begin!---------------------" << endl;
   cout << "======================================================" << endl;
   cout << endl; cout << endl;
}

void getValue(double R1){

   cout << "Please input the radius of your circle: "; 
   cin  >> R1;

}

double computeCircumference(double R2){
   double C1;
   double pi = 3.14;

   C1 = (2 * pi * R2);

   return C1;

}

void computeArea(double R3, double A1){
	double pi = 3.14;

	A1 = (pi * R3 * R3);

}

void outputData(double R4, double C2, double A2){

   cout << "======================================================" << endl;
   cout << "--------------Information on your circle--------------" << endl; 
   cout << "------------------------------------------------------" << endl;
   cout << "Radius--------: " << R4 << endl;
   cout << "Circumference-: " << C2 << endl;
   cout << "Area----------: " << A2 << endl;
   cout << "------------------------------------------------------" << endl;
   cout << "-------------Your circle is now complete!-------------" << endl;
   cout << "======================================================" << endl;
   cout << endl; cout << endl;
}

bool goAgain(){
   char run2;

   cout << "Do you want to run this program again? (y/n): ";
   cin  >> run2;
   cout << endl; cout << endl;

   if ( run2 == 'y' || run2 == 'Y' )
      return true;
   else
      return false;

}
Hmm! u are passing ur variables by value:
try this:
line6:
void getValue (double*);
line 19
getValue(&radius);
line 41
1
2
3
4
5
6
void getValue(double* R1){

   cout << "Please input the radius of your circle: "; 
   cin  >> *R1;

}

line8
void computeArea(double, double*);
line 21
computeArea (radius, &area);
line58
1
2
3
4
5
6
7
void computeArea(double R3, double*  A1){
	double pi = 3.14;

	*A1 = (pi * R3 * R3);

}
Hmm.. we haven't really covered the ampersand's or asterisk's capabilities in class yet. Could you briefly explain them or link me to a tutorial?
In C++, the default way of passing arguments is by value, so when you call

1
2
3
4
5
6
void getValue(int R1){

   cout << "Please input the radius of your circle: "; 
   cin  >> R1;

}


The function gets a copy of the value you fed to it. Then it modifies the copy. Then it returns and the copy it manipulated disappears silently into the night. You could pass by reference or do the following:

1
2
3
4
5
6
7
8
double getValue()
{
     cout << "Please input the radius of your circle: "  ;
 
     double r ;
     cin >> r ;
     return r ;
}


and the call in main becomes:

radius = getValue();

Don't forget to change the forward declaration of the function above main.


Moving along...

1
2
3
4
5
6
void computeArea(double R3, double A1){
	double pi = 3.14;

	A1 = (pi * R3 * R3);

}


Again, A1 is a local copy and this code has no effect on the variables fed to it. Either pass A1 by reference, or:
1
2
3
4
5
double computeArea(double R3){
	double pi = 3.14;

	return (pi * R3 * R3);
}


and the call in main becomes
area = computeArea (radius);

(Don't forget to change your forward declaration above main.)
Last edited on
Also, I found the problem.
When I declare radius and area in _main, I declared them as a space.
This always gave me only 1 output.

When declared them as 2, it gave me the proper outputs as though my input was 2 for the radius. It doesn't even take my inputs into consideration.

Also, I don't want to define what radius and area are; I just want to declare them, but if I don't define them as = something, then the program says that they are 'uninitialized' when I use them .

:\
Elaboration on the problem:

(02): warning C4700: uninitialized local variable 'radius' used
(03): warning C4700: uninitialized local variable 'area' used


This is how I want it to look, but when I do this, it gives me the errors above:
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
int main(){
   double radius;
   double area;

   banner();

   do{
      getValue(radius); 
      double circumference = computeCircumference (radius);
      computeArea (radius, area);
      outputData (radius, circumference, area);
    } while ( goAgain() );
   
return 0;
}

void banner(){
   cout << "======================================================" << endl;
   cout << "--------------Welcome to Project Circle!--------------" << endl;
   cout << "------------------------------------------------------" << endl;
   cout << "This program will allow you to input a circle's raidus" << endl;
   cout << "------------------------------------------------------" << endl;
   cout << "---and will then output the circumference and area.---" << endl;
   cout << "------------------------------------------------------" << endl;
   cout << "---------------------Let's begin!---------------------" << endl;
   cout << "======================================================" << endl;
   cout << endl; cout << endl;
}

void getValue(double R1){

   cout << "Please input the radius of your circle: "; 
   cin  >> R1;

}

double computeCircumference(double R2){
   double C1;
   double pi = 3.14;

   C1 = (2 * pi * R2);

   return C1;

}

void computeArea(double R3, double A1){
	double pi = 3.14;

	A1 = (pi * R3 * R3);

}

void outputData(double R4, double C2, double A2){

   cout << "======================================================" << endl;
   cout << "--------------Information on your circle--------------" << endl; 
   cout << "------------------------------------------------------" << endl;
   cout << "Radius--------: " << R4 << endl;
   cout << "Circumference-: " << C2 << endl;
   cout << "Area----------: " << A2 << endl;
   cout << "------------------------------------------------------" << endl;
   cout << "-------------Your circle is now complete!-------------" << endl;
   cout << "======================================================" << endl;
   cout << endl; cout << endl;
}

bool goAgain(){
   char run2;

   cout << "Do you want to run this program again? (y/n): ";
   cin  >> run2;
   cout << endl; cout << endl;

   if ( run2 == 'y' || run2 == 'Y' )
      return true;
   else
      return false;

}
Please see the post 2 posts up from your last one.
i will have to explain pointers, pass by value pass by reference, better wait for your class.
Meanwhile you can try with global variables, which will solve ur problem.
add the following lines before line 5.
1
2
double radius =0.0;
double area =0.0;

remove line 13,14
@ anirudh sn; declaring them in global did nothing.
Setting them to 0.0 still made the output become 0,0,0.

Nothing I inputted made any difference in all 10 attempts with different inputs.
Last edited on
@ cire; you are asking me to use value returning functions, which as I specified in my first post, I am not supposed to do. Those 2 functions <must> be void functions, as stated by the rules of this assignment.
Last edited on
sry didnt make it clear: use the same global variables in all ur functions whenevr you are using radius or area.
eg:
1
2
3
4
5
6
void computeArea(double R3, double A1){
	double pi = 3.14;

	A1 = (pi * R3 * R3);

}

to
1
2
3
4
5
6
7
void computeArea(){
	double pi = 3.14;

	area= (pi * radius * radius);

}

change similarly in all ur function declarations and definitions.
Then you should use the other option I mentioned. Passing by reference. A reference behaves in every way like an alias (a different name) for the variable it refers to. You declare/define one by inserting a '&' between the type name and the variable name.

forward declaration:
void getValue (double&);

Call in main:
getValue(radius);

definition:
1
2
3
4
5
void getValue(double& R1)
{
   cout << "Please input the radius of your circle: "; 
   cin  >> R1;
}


I'm sure you can accomplish something similar with the computeArea function.
Last edited on
@ anirudh sn; Tried that; the program worked, but it still outputted the 0.0 that I defined the variables radius and area as. It still ignored my inputs.
please share ur modified code.
@ cire; I've tried setting the variables to reference by address (&), but as I've figured out already, the problem is with the initialization of the variables 'radius' and 'area'. Great info, and thanks, but that isn't my problem. Any ideas how to initialize radius and area properly?
Pages: 12