To simplify my code

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.


Do You have any ideas?

Thank you!

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
  
#include <iostream>
#include <cmath>
using namespace 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.
Last edited on
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?





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
#include <iostream>
#include <cmath>
using namespace 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;
}
if(c*c*c==x)
}
you don't do an action inside your if statement, which is not allowed. you can do nothing with a ; but that can't be your intent there.
Sorry, Jonnin
I did not get it. Can You please clarify it?
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
Last edited on
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




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
#include <iostream>
#include <cmath>
using namespace 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;
}
m and n are the wrong way round on line 27.

Using more informative variable names would help you remember which was FIRST and which was LAST.
So simple in the end.
Thank you all for tips and helping me out.
Topic archived. No new replies allowed.