0

What is wrong with this code?

I was trying to make exponentation, "wynik" means "result" in my language. "Wynik to" means "The result is". The problem is that when i run the code the result always equals 0. Why? #include <iostream> using namespace std int main() { int a,b, wynik; cin >> a; cin >> b; for(int i=1; i<=b; b--) { wynik*= a; } cout << "Wynik to: " << wynik; return 0; }

19th Feb 2019, 6:45 PM
Eryk Marciniak
Eryk Marciniak - avatar
5 Respostas
+ 3
if you initialize wynik with 1, it seems to work. int a, b, wynik=1; If you use a variable although you haven't given a value to it yet, strange stuff happens.
19th Feb 2019, 6:52 PM
HonFu
HonFu - avatar
+ 2
Oh i forgot. Thanks for helping.
19th Feb 2019, 6:53 PM
Eryk Marciniak
Eryk Marciniak - avatar
0
Without seeing the code we can't help you. Care to share? You can upload it in Code Playground and link it here!
19th Feb 2019, 6:47 PM
HonFu
HonFu - avatar
0
Can you show us your code (code playground)?
19th Feb 2019, 6:48 PM
Denise Roßberg
Denise Roßberg - avatar
0
I showed the code in description but i can copy it again #include <iostream> using namespace std; int main() { int a,b, wynik; cin >> a; cin >> b; for(int i=1; i<=b; b--) { wynik*= a; } cout << "Wynik to: " << wynik; return 0; }
19th Feb 2019, 6:50 PM
Eryk Marciniak
Eryk Marciniak - avatar