+ 2

The code below run but didn't works as i want..why ?

https://code.sololearn.com/cFyT4Pyiun30/?ref=app

14th Jun 2017, 5:57 PM
De Vinci
8 Antworten
+ 5
Yea if you want b to be the loop count. Something like this: for(; b >= 0; b--){ } PS: The cout line shouldn't have those commas. Look at how @Enas separated the lines with <<.
14th Jun 2017, 6:13 PM
Rrestoring faith
Rrestoring faith - avatar
+ 7
//Is that what you want ? #include <iostream> using namespace std; int main() { cout<<"how many of phone numbers you wanna save ?"; int b; cin>>b; long int num [b]; for (int i=0;i<b;i++) { cout<<"phone number"<<b<<"\n"; cin>>num[b]; cout<<num[b]; } return 0; }
14th Jun 2017, 6:05 PM
Enas Emad
Enas Emad - avatar
+ 4
I'm actually surprised that it runs at all. Usually a compiler would be upset that you are trying to use a runtime variable to initialize an array: long int num [b]; You have to use a vector or an array from the array header or a pointer. vector<long int> num(b); long int* num = new long int[b]; delete [] num; Then, you need a separate variable for your loop. There's no other way around that. Just do this: for (int i = 0; i < b; i++) { // do stuff }
14th Jun 2017, 6:23 PM
Zeke Williams
Zeke Williams - avatar
+ 3
Have a look at your for loop. for(b = 0; b; b++) The condition is b?? so.. the condition is false I guess. Since 0 is false. So, There's nothing to run in the loop.
14th Jun 2017, 6:08 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
yes i tried that before..but i don't want to add another variable like "i" as you did..i want just to work with the first variable "b"..isn't it possible ?
14th Jun 2017, 6:09 PM
De Vinci
+ 2
it works..thank u :) that what i want
14th Jun 2017, 6:16 PM
De Vinci
+ 2
Here is the version I made that works. https://code.sololearn.com/c8u45vocT5zX
14th Jun 2017, 6:32 PM
Zeke Williams
Zeke Williams - avatar
+ 2
yes i got it...thank u so much for this version..actually i don't know yet about vector, but i'll will learn it
14th Jun 2017, 6:36 PM
De Vinci