+ 2
The code below run but didn't works as i want..why ?
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 <<.
+ 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;
}
+ 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
}
+ 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.
+ 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 ?
+ 2
it works..thank u :) that what i want
+ 2
Here is the version I made that works.
https://code.sololearn.com/c8u45vocT5zX
+ 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