Lowest price ever! Learn Generative AI for 48% less!
Get my discount0
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; }
5 Answers
+ 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
0
aha.. yes in this case right ..i will see how to edit it..thnx
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
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 ?
0
Sumer Fattoum yes we can declare them where they can be accessed from ;)
https://stackoverflow.com/questions/28613436/if-statements-without-brackets/28613629