How to make "for" loop print every power raised till given value in this code?

Hi! Very new to C and programming in general. I am just learning the ropes with mathematical console programs. Here is the code of what I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 #include <iostream>
int main()
{
	using namespace std;
	float x;
	int y;
	cout << "Number:\n";
	cin >> x;
	cout << "Power?\n";
		for (y = 0; cin >> y; y = y + 1)
	{
		x = pow(x,y); 
		cout << x << "\n";
	}
	cin >> x;
	return 0;
}

I have two questions:

- I want the program to not just give the final number for raising power, but print every value until the max power on a new line.
E.g. When user enters "4" and then "3" I want it to print
4
16
64
Instead of just 64.

- I used a very cheap breaking method back there where i just put cin>>x so the debug console doesn't close after executing so i can read the results. How can I do it properly?

first: start with 1 in the loop on line 10.
second: do not assign the result on line 12 to x. So instead of line 12/13 write

cout << pow(x,y) << "\n";
Sticky thread: http://www.cplusplus.com/forum/beginner/1988/
(In my opinion the "right way" is to run programs from console.)

You line 10 is probably not what you want. On every iteration you do ask for new number, and the loop ends only if that fails.

Power: think it like this prod = 1 * x * x * .. * x. What if one each iteration does one multiplication?
Without testing

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
#include <iostream>

int main()
{
	using namespace std;

	float x;
	unsigned int y;

	cout << "Number:\n";
	cin >> x;

	cout << "Power?\n";
	while ( cin >> y )
	{
		float product = 1;
		if ( y == 0 ) 
		{
			cout << product << '\n';
		}
		else
		{
			while ( y-- )
			{
				product *= x;
				std::cout << product << '\n';
			}
		}
		cout << '\n';
	}

	return 0;
}
Vlad's code worked exactly like I wanted, however I don't understand neither what "y--" means not what "*=" is. I initially thought this type of program will work best with "for" loop, as that one is what I wanted to learn. I can't see to figure out how to make this work with the operators that I know.

Thanks for all the help. I wish i could understand what those strange operators mean in vlad's code.
Have a read of the tutorial & reference at the top left of this page.

HTH :)
Have a read of the tutorial & reference at the top left of this page.

You could not type a couple of words explaining 2 operators?

If for every beginner's code I see on the support forum I will have to go to the reference documentation and search every single header (because I don't understand what those mean and where to search), I would never have any hope of learning this language.

=_=, this is why it's so hard to learn a language without any kind of courses. Anyway, that's beside the point. I will go ahead and mark it as solved and try to find those symbols on Google.

Edit:
Ok I think I sort of get it now. Time to move onto the next topic.
Last edited on
y--decreases 1 from y

product *= x; is equivalent to product = product * x; -> product ist multiplied by x and the result is stored back to product
Ok it's crystal clear now! So Vlad takes 1 away from y every time he multiplies x by itself until y reaches 0, at which point execution stops. Thank you!
Megakoresh wrote:
You could not type a couple of words explaining 2 operators?


You could not spend 2 minutes reading about it yourself in the reference? Or should people like me type in replies saying the same thing over & over to different people too lazy to look first? What is so hard about google C++ operators?

There is a wealth of clear well explained reference info on this site - now that you know it's there, don't have a go at me for not replying to your liking. People replying put in a little effort (sometimes a lot) to do so, so we appreciate some demonstration of a little effort in return.

Then don't reply at all. If I read a reply I automatically perceive it as a person wanting to help because of the context that it's in. So if you don't want to or can't why type anything at all? To you explaining it would take just as much time as typing what you did, so why post at all? I am actively searching for new info on problems I post about.

If nobody helps, I will eventually figure it out on my own. I just don't see the purpose of "Google/search for it" messages I have been sometimes getting on other forums too, since they are telling me to do what I am already doing right now.

And that tutorial and references don't have search function, btw. At least I did not find one.
Last edited on
By the way it would be better to write

while ( cout << "Power?\n", cin >> y )

or

while ( cout << "Power?\n" && cin >> y )

instead of

1
2
	cout << "Power?\n";
	while ( cin >> y )
The tutorial has table of contents.

The search function of this site -- when it has returned results -- shows three tabs: All, Reference, and Forum.
Megakoresh wrote:
Then don't reply at all.


Should I make a decision before each post that I do: I wonder if they will get upset if I point them towards the reference section?

You too, could have refrained from replying the way you did to my post.

I have made over 2200 posts on this forum - the vast majority of those were to help people, so don't say that I am not. You didn't find the search feature? It's right there in the banner at the top of this page. Half the reason I point people towards the reference section, is that they don't even realise it's there - you not finding the search feature demonstrates that in spades. And I say again, googling "C++ operators" would lead you to the exact same place, which would have taken you less time than writing your question.

Interesting that other forums are saying the same thing to you. It seems to me that you need to get away from this idea that you expect instant answers from some sort of real time hot plate wiki service, without applying any effort at all. I am guessing you are of Y generation age, and your mother does everything for you.

You attitude seems to imply that you don't want help.

Despite that, here's a link - which might help you with the real basic thing like types, operators & flow control:

http://zanasi.chem.unisa.it/download/C.pdf


Just don't complain that it is C & not C++. If you want a C++ book, then try one written by Bjarne Stroustrup - who invented the language.

So that is the end of my rant & I am not worried about how much effort it took - if the message sinks in, it will have been worth it.

Topic archived. No new replies allowed.