+ 3

why is the output wrong??

sooo , shouldn't this program give me the multiple of a and b? what did i do wrong here? #include <iostream> using namespace std; int main() { int a,b,c,i =0; cout << "insert an integer : "; cin >> a; cout << endl << "insert another : "; cin >> b; for (i=0; i >= b; i = i+1) { c = a+c; } cout << a << "x" << b << "=" << c; return 0; }

3rd Jul 2019, 7:05 AM
Khalid Sakka
Khalid Sakka - avatar
8 Answers
+ 3
The condition part of the for loop is false from the beginning. You would only need to change >= to <.
3rd Jul 2019, 7:24 AM
Seb TheS
Seb TheS - avatar
+ 2
thank you that was so stupid of me😂
3rd Jul 2019, 7:27 AM
Khalid Sakka
Khalid Sakka - avatar
+ 2
Rather do: int a,b,c=0,i;
3rd Jul 2019, 7:41 AM
Seb TheS
Seb TheS - avatar
+ 2
oh , i thought all four variables would be initialized as 0 with this. thank you for taking the time to teach me.
3rd Jul 2019, 7:48 AM
Khalid Sakka
Khalid Sakka - avatar
+ 1
i did initialize c in this line int a,b,c,i =0;
3rd Jul 2019, 7:41 AM
Khalid Sakka
Khalid Sakka - avatar
+ 1
endl ( in line 8th) must be end of statemant and the conditions in your for loop is wrong ( line 10th)
14th Aug 2019, 10:27 AM
Seyed ali Zolfaghar
Seyed ali Zolfaghar - avatar