Hello,
Here is the problem ive been working on:
write a while loop that displays each int from 1 to 5 together with its square and a cube. Display all three values for integer on a separate line.
My problem is that it does not really do what it is asked to do.
here is my code:

#include<cmath>
#include<iostream>
using namespace std;

int main()
{
	int count;
	int x;
	float xsquared;
	float xcubed;
	
	cout << "enter the value for x: " << endl;
	cin >> x;
	
	count = 0;
	while (count < 4)
	{
		
		xsquared = x * x;
		xcubed = x * x * x;
		cout << xsquared << xcubed <<endl;
		
		count = count + 1;

	}

	system("pause");
	return 0;
}

Thank you ahead of time

Recommended Answers

All 7 Replies

Hello,
Here is the problem ive been working on:
write a while loop that displays each int from 1 to 5 together with its square and a cube. Display all three values for integer on a separate line.
My problem is that it does not really do what it is asked to do.
here is my code:

#include<cmath>
#include<iostream>
using namespace std;

int main()
{
	int count;
	int x;
	float xsquared;
	float xcubed;
	
	cout << "enter the value for x: " << endl;
	cin >> x;
	
	count = 0;
	while (count < 4)
	{
		
		xsquared = x * x;
		xcubed = x * x * x;
		cout << xsquared << xcubed <<endl;
		
		count = count + 1;

	}

	system("pause");
	return 0;
}

Thank you ahead of time

It does exactly what it is asked to do, no more, no less. If it doesn't do what you want it to do, then you're not asking it to do what you want it to do.

Display all three values for integer on a separate line.

cout << xsquared << xcubed <<endl;

You're telling the program to display two values, not three, and you're telling it to do so on the same line. You only have one endl in this line.

while (count < 4)
	{
		
		xsquared = x * x;
		xcubed = x * x * x;
		cout << xsquared << xcubed <<endl;
		
		count = count + 1;

	}

Note that x never changes inside of this loop, so it's going to display the same thing every time.

OH YOU ARE RIGHT!
i see i guess i didnt understood the exercise...
Thank you

Also, I've been thinking if i really need to promt the user to input value of x
How can i make the program to get those int values from 1 to 5 and display all of them?

You can use the while loop you have, intialize x to 1 before the loop and increment x each time in the loop.

Another option is using a for loop

for (int i = 1; i <= 5; i++){
  // compute square of i
 //  compute cube of i
 // display
}

Also, I've been thinking if i really need to promt the user to input value of x
How can i make the program to get those int values from 1 to 5 and display all of them?

No where in the question says you need to prompt the user. You can simply initialize Count to 1 before the while loop, and increment the value within the loop.

I know, i messed up yesterday :)
wasnt thinking well

thank you

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.