0

(SOLVED) for_while!! x, sum integers are not defined in this scope, why?

#include <iostream> using namespace std; int main() { do { int x = 0; int numbers = 0; int sum = 0; x++; cin >> numbers; sum = sum + numbers; }while(x<=5); cout << sum; return 0; }

16th May 2020, 10:53 PM
fergoka
fergoka - avatar
3 Réponses
+ 6
fergoka you have defined sum inside do which will be treat as local variable. Do like this: int sum = 0; do { sum += 5; } while (x <= 5);
16th May 2020, 11:00 PM
A͢J
A͢J - avatar
+ 3
You haven't defined x and sum in the in the main function ,they are only defined in the do Loop scope intialize and define outside do Loop int x=0; int numbers; int sum=0;
16th May 2020, 11:01 PM
Abhay
Abhay - avatar
+ 1
Uf..how silly I am. Thank you, AJ! Edit: Thanks Abhay.
16th May 2020, 11:02 PM
fergoka
fergoka - avatar