0

why it shows undeclared variable although it is declared with int?

I am trying this code to calculated how many coins change are back to a costumer, for 25 , 10 , 5 and 1 cents...whatever the function but the weird thing it shows undeclared variable for back,back2,back3, back4 alothough I declared them all with int, and it shows me unused for them all although I use them in the summation which is backk. include <iostream> using namespace std; int main() { int x = 61 / 25; if (x>0) int back=x; int n=61%25; int c=n/10; if (c>0) int back2=c; int d=n%10; int f=d/5; if (f>0) int back3=f; int h=f%5; int back4=h; int backk= back + back2 +back3 + back4; cout<<x<<endl<<n<<endl<<c<<endl<<d<<endl<<f<<endl<<h<<endl<<backk; return 0; }

30th May 2020, 10:31 AM
Sumer Fattoum
Sumer Fattoum - avatar
5 Réponses
+ 1
Why are you not declaring all back1...4 upfront and why not use {}, do you want to keep them in one scope ?? and add code via code-playground please
30th May 2020, 12:01 PM
Daljeet Singh
Daljeet Singh - avatar
0
aha.. yes in this case right ..i will see how to edit it..thnx
30th May 2020, 11:05 AM
Sumer Fattoum
Sumer Fattoum - avatar
0
so I deleted everything after if (f>0) the use of back3 and back4 and h... but it still gives me unused for back and back2 alothough their conditions are true...and gives me undeclared for both alothough they are declared above
30th May 2020, 11:45 AM
Sumer Fattoum
Sumer Fattoum - avatar
0
well I declared them separetly before main and thank you it worked...but shouldn't declaring them any where in the Programm do the same ? why It didn't work ?
30th May 2020, 12:15 PM
Sumer Fattoum
Sumer Fattoum - avatar
31st May 2020, 4:15 PM
Daljeet Singh
Daljeet Singh - avatar