I am working on a homework assignment for a Beginning C++ course. I'm using Microsoft Visual C++ 2008 (required software for the course).
I have to write a program that can take any number, and calculate the square, cube, or fourth root, accurate to three decimal places. I am not allowed to use #include <cmath>. It needs to be done using only loops (for or while) and switches. The instructor wants me to define the function in a header file, and call it in the main calling program.
He doesn't want us to use cin (even though I already know how); instead, he will insert whichever numbers he wants in our source code and then run the program.
I have a header file named "Roots.h" and the main calling program called "Project07.cpp"
Here is my code so far:
Roots.h
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
|
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
void RootCalc(double Number, int RootValue)
{
double RootOfNumber;
switch(RootValue)
{
case 2:
for (RootOfNumber = 0; RootOfNumber < Number; RootOfNumber += .001)
{
if (RootOfNumber*RootOfNumber==Number)
{
cout << "The square root of " << Number
<< "accurate to three decimal places is "
<< RootOfNumber;
}
}
break;
case 3:
for (RootOfNumber = 0; RootOfNumber < Number; RootOfNumber += .001)
{
if (RootOfNumber*RootOfNumber*RootOfNumber==Number)
{
cout << "The cube root of " << Number
<< "accurate to three decimal places is "
<< RootOfNumber;
}
}
break;
case 4:
for (RootOfNumber = 0; RootOfNumber < Number; RootOfNumber += .001)
{
if (RootOfNumber*RootOfNumber*RootOfNumber*RootOfNumber==Number)
{
cout << "The fourth root of " << Number
<< "accurate to three decimal places is "
<< RootOfNumber;
}
}
break;
default:
cout << "Please enter valid parameters!!!";
break;
}
}
|
Program07.cpp
1 2 3 4 5 6 7 8 9 10 11
|
#include "stdafx.h"
#include "Roots.h"
#include <iostream>
using namespace std;
int main()
{
RootCalc(5,2);
return 0;
}
|
So far, I've got it down to the point where the compiler gives me no errors or warnings, but the output I see is "Press any key to continue ... "
I can't figure out why it won't output anything else. I guess it could either be that I didn't include a crucial header file, or maybe it's something much more advanced than I am familiar with so far.
I hope I did a sufficient job of describing the problem and putting in enough effort before asking for help. Thanks in advance for any tips/pointers/advice/help!