I am a complete newbie in C++ programming. After a very basic introductory class I am now reading a little more advanced book and trying to teach myself and I am slowly learning. Anyway, the book mentions a method for calculating the cube root of a number but it doesn't give any actual examples. It left me a little confused. Is there a website that any of you are familiar with where it shows the code for this and other basic programs for me to quickly refer to when I get stuck on a particular problem? I googled "cube root calculator" but it didn't return much. I don't want to continue reading forward until I get the hang of this problem.

## All 13 Replies

IIRC:

``pow ( abs ( val ), 1.0 / 3.0 )``

Thanks for your help but with me being such a C++ idiot it would greatly help me to see the entire program from start to finish. I'm still getting the hang of loops and it would be nice to see the while loops referred to in the book. I'm trying to write it out myself but I'm not having much luck. I'll keep searching. Thanks again

>it would greatly help me to see the entire program from start to finish

``````#include <cstdlib>
#include <cmath>
#include <iostream>

int main()
{
int val;

std::cout<<"Enter a number: ";

if ( std::cin>> val ) {
std::cout<<"Cube root of "<< val <<": "
<< std::pow ( std::abs ( val ), 1.0 / 3.0 ) <<std::endl;
}
}``````

Holy cow! That one is more advanced than I am. I was trying to follow the book and mine looks like this. It doesn't work yet but I'm still working on it. Can you tell me if I am even close?

``````#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
double x;
double a = 1;
double b;
char c;

c = ' ';

while (c != 'q')
{
cout << "Cube Root Calculator" << endl;
cout << endl;
cout << "Enter a number: ";
cin >> x;

while (x - (a)(a)(a)) < 0.001)
{
a = b;
}

cout << "Answer is: " << fixed << showpoint << setprecision(5) << a << endl;
cout << endl;
cout << "Press c to continue, q to quit" << endl;
cin >> c;
}

cout << "Exiting Cube Root Calculator" << endl;
cout << endl;

return 0;
}``````

Code reformatted and tags added. -Narue

>That one is more advanced than I am
Aroo? And then you post a manual attempt at it? :eek:

>while (x - (a)(a)(a)) < 0.001)
(a)(a)(a) likely doesn't do what you think it does. ;) Perhaps you were trying to do:

``while (x - (a * a * a)) < 0.001)``

>a = b;
b doesn't have a value yet. You'll probbly get strange results.

b doesn't have a value yet. You'll probbly get strange results.

What should i do with b?

>What should i do with b?
How should I know, it's your program. ;) You put b there for a reason, right?

I guess I need to come up with a formula for computing b.
This could take a while :-|

Does my code make sense to you at all?
It has really got me confused. Can you make any suggestions on how to finish it?

I think I got it.

``````#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

int main()
{
double x;
double a;
char c;

c = ' ';

while (c != 'q')
{
cout << "Cube Root Calculator" << endl;
cout << endl;
cout << "Enter a number: ";
cin >> x;

a = 1;
while (fabs(x - (a*a*a)) >= 0.001)
{
b = (2*a+x/(a*a))/3;
a = b;
}

cout << "Answer is: " << fixed << showpoint << setprecision(5) << a
<< endl;
cout << endl;
cout << "Press c to continue, q to quit" << endl;
cin >> c;
}

cout << "Exiting Cube Root Calculator" << endl;
cout << endl;

return 0;
}``````

<< moderator edit: added [code][/code] tags >>

I decided to name the variables to make it easier to understand if I want to look back at this sometime in the future and to help my brother understand it. I named x (userNumber) and c (programRepeat). What would be a good name for a and b?

``````int main()
{
double userNumber;
double a;
double b;
char programRepeat;

c = ' ';

while (programRepeat != 'q')
{
cout << "Cube Root Calculator" << endl;
cout << endl;
cout << "Enter a number: ";
cin >> userNumber;

a = 1;
while (fabs(x - (a*a*a)) >= 0.001)
{
b = (2*a+userNumber/(a*a))/3;
a = b;
}
``````

I changed b to calCubeRoot. Would these variable names make sense to you if you were trying to understand this program?
I'm also still trying to come up with a good name for a.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.