Hello!
I am doing my school assignment and this is the program I made.
My program works but my professor told me to take out first (while) and replace that with (if). But now I'm stuck and can't figure out how to change it, because when I want to replace it I get errors. I'm doing something wrong, so I wrote my code as it was before.
I know this is an easy task but this is my first code and I don't have previous programming experience also I managed this with cplusplus articles but as I said, now I'm stuck and can't figure out.
Task:
The task idea is: There are given two whole numbers m and n. Print out those numbers which are in the interval [n, m] and are whole number cubes.
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
int ok;
do
{
int m, n;
cout << "Please enter first whole number m = ";
cin >> m;
cout << "Please enter second whole number n = ";
cin >> n;
if(m > n)
{
int a = n;
n = m;
m = a;
}
int x = cbrt(m);
while(x * x * x < m)
x++;
while(x * x * x <= n)
{
cout << x * x * x << "\n";
x++;
}
do
{
cout <<"If you would like to continue, press: (1) ,\nIf you would like to finish, press: (0)"<< endl;
cin >>ok;
if((ok!=1) and (ok!=0))
cout<<"Enter 1 or 0!"<<endl;
}while ((ok!=1) and (ok!=0));
}while (ok==1);
return 0;
cube root is not ensured to be a whole number.
so
int x = cbrt(y)
does not ensure that
x*x*x == y.
so the test is
if(x*x*x == y) //then you have a cube root whole number, eg 2 and 8 or 3 and 27
you need a test like this to get the right answer.
so a total re-write to me looks like
for(x = n; x <= m; x++)
{
int c = cbrt(x);
if(c*c*c == x)
//you have one of your answers. save it, print it, do something
}
what happens if you put in 27?
cbrt = 3, and 3*3*3 == 27, its good.
what happens if you put in 50?
cbrt = 3 (the decimal is dropped for int math)
3*3*3 does not equal 50. not an answer.
Ok, thank you, I understood. I previously did not think about value 50. I'm just learning :)
I get an error in 39th line (p|39|error: expected primary-expression before '}' token.
Whats wrong there?
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
int ok;
do
{
int m, n;
cout << "Please enter first whole number m = ";
cin >> m;
cout << "Please enter second whole number n = ";
cin >> n;
if(m > n)
{
int a = n;
n = m;
m = a;
}
int x = cbrt(m);
for(x=n; x<=m; x++)
{
int c = cbrt(x);
if(c*c*c==x)
}
while(x * x * x <= n)
{
cout << x * x * x << "\n";
x++;
}
do
{
cout <<"If you would like to continue, press: (1) ,\nIf you would like to finish, press: (0)"<< endl;
cin >>ok;
if((ok!=1) and (ok!=0))
cout<<"Enter 1 or 0!"<<endl;
}while ((ok!=1) and (ok!=0));
}while (ok==1);
return 0;
}
you have to DO something inside an if statement.
you have an if statement, and the format of that is
if(condition)
{
code if condition is true
}
else //optional alternative code, or do nothing
{
code to do if false
}
your code is exceedingly complicated.
all you need is what I gave you with a little wrapper for the repeat and menu and output.
all you need is (you have most of it) a simple wrapper (already there) around my loop:
do
output
input
for(x = n; x <= m; x++) //replace 27 through 41 with something like this
{
int c = cbrt(x);
if(c*c*c == x)
cout << x << endl; //HERE. an example of doing something in the condition is writing the result.
}
output go again?
get input
while input means keep going
I'm still doing something wrong there, it does not give me cubes of typed numbers, it just skips to entering 1 or 0. Then I put
while(x * x * x <= n)
{
cout << x * x * x << "\n";
x++;
}
And it just asks for m and n and does nothing.
Can you please share whole code so I could understand?
Thank you
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
int ok;
do
{
int m, n, x;
cout << "Please enter first whole number m = ";
cin >> m;
cout << "Please enter second whole number n = ";
cin >> n;
if(m > n)
{
int a = n;
n = m;
m = a;
}
for(x = n; x <= m; x++)
{
int c = cbrt(x);
if(c*c*c == x)
cout << x << endl;
}
do
{
cout <<"If you would like to continue, press: (1) ,\nIf you would like to finish, press: (0)"<< endl;
cin >>ok;
if((ok!=1) and (ok!=0))
cout<<"Enter 1 or 0!"<<endl;
}while ((ok!=1) and (ok!=0));
}while (ok==1);
return 0;
}