Question:
Help with a C++ NaN error?
Cesar
2010-03-16 06:59:05 UTC
I made this program to find the volume of a sphere or cube depending on the radius or height given by the user. However for the volume of the sphere, I have problems. I think it has to do with the data type, but I don't know how to fix it. One way I get back the same number I put in, another way I get NaN. Can any one help, here is my code:

#include

using namespace std;

double sphereVolume(int r);
int cubeVolume(int);

int main()
{
char shape= 'a';
int h;
int r;
double Svolume;
int Cvolume;

while (!(shape=='s' || shape=='c' || shape=='q'))
{
cout<cin>> shape;

if (shape== 's')
{
cout<cin>> r;
Svolume= sphereVolume(r);
cout<shape= 'a';
}

else
{
if (shape== 'c')
{
cout<cin>> h;
Cvolume= cubeVolume(h);
cout<< endl<<"The volume is "<shape= 'a';
}
else
{
if (shape== 'q')
{
break;
}}}}
return 0;
}

double sphereVolume(int r)
{
double Svolume=1.34*3.14*r*r*r;
}

int cubeVolume(int h)
{
int Cvolume=h*h*h;
}
Four answers:
?
2010-03-16 08:10:59 UTC
There were a couple of things I changed:



1.) after debugging of the code I noticed that there is no difference of results, whether I provide 3.5 or 3 as height or radius

2.) sphereVolume: did not return a value

3.) cubeVolume: did not return a value

4.) too many brackets

5.) I ommitted Svolume and Cvolume variables, because they are not needed in the current context. It could have been though that you introduced those variables in order to use them later on elsewhere. So I could very well be considered ignorant here.

6.) I changed the wording of your choice prompt. I believe it helps guide the user, now, but I may be considered ignorant (i.e. of your assignment)

7.) With my compiler I could not duplicate a "NaN" result. NaN stands for Not a Number.

8.) The assignment shape='a' I took from the if condition to the end of the loop.

I did this for the following reasons:

a) code reduction

b) increase code transparency

Your approach could be considered speed optimized and my change ignorant to that intent

9) I changed the nesting of your if statements. I believe they meet your requirements and are easier to understand, now



#include

using namespace std;

double sphereVolume(int r);

int cubeVolume(int);



int main()

{

char shape= 'a';

int h = 0; // initialized

int r = 0; // initialized

// double Svolume = 0.0; // initialized - not needed

// int Cvolume; // initialized - not needed



while (!(shape=='s' || shape=='c' || shape == 'q')) {

cout<<"s sphere volume\nc cube volume\nq quits > ";

cin>> shape;

shape = tolower(shape);



if (shape== 's')

{

cout< ";

cin>> r;

cout<
}

else if (shape== 'c')

{

cout< ";

cin>> h;

cout<< endl<<"The volume is "<< cubeVolume(h)<
}

else if (shape== 'q') {

break;

}

else {

cout << "Input not recognized " << endl;

}

shape = 'a'; // initialize shape in the loop

cin.ignore();

}

system("pause");

return 0;

}



double sphereVolume(int r)

{

return 1.34*3.14*r*r*r;

}



int cubeVolume(int h)

{

return h * h * h;

}
Ngo Cong Huan
2010-03-16 07:58:00 UTC
In functions:

double sphereVolume(int r) and

int cubeVolume(int h)

You have to return a value for function



double sphereVolume(int r)

{

double Svolume=1.34*3.14*r*r*r;

return Svolume;

}



int cubeVolume(int h)

{

int Cvolume=h*h*h;

return Cvolume;

}
Nasty old uncle Mike
2010-03-16 07:08:20 UTC
You have a type problem.



quit the int r input and make that a double or at least a float.
?
2016-12-12 10:37:50 UTC
remember, in case you go with a DOM element's assets (checklist.getElementById.fee, checklist.getElementsByTagName("enter")[0... etc), that fee is a string; in spite of if it relatively is "0". you may convert that element of a quantity (integer for a complete quantity or pick the flow with decimal. The purposes parseFloat(fee) or parseInt(fee) could be useful. i've got not ran your complete code yet while my suggestion would not help, tell me what fee is NaN and that i could desire to furnish you extra particular suggestion. Edit ======= NaN does propose not a quantity and DOM is checklist merchandise module (it relatively is the way JavaScript and HTML reference merchandise on a 'canvass' to reference aspects). for occasion if I supply you a string (a observe) it relatively is "sixteen", JavaScript can't upload a quantity to that as a calculator could; for the reason it relatively is a string (i.e. observe). Therefor if I extra the side "a million" to the interest "sixteen" i could have "161"; a similar way if I extra "b" to "aa" i could get "aab". First i could convert the string "sixteen" to the quantity sixteen till now i will carry out mathematical purposes to it; therefor the string "sixteen" isn't a quantity. i will study your particular code while i'm slightly extra sober yet i'm hoping this helps.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...