G
Gaijinco
Hi! I was coding something which had some lines like these:
int main(){
int n;
scanf("%d",&n);
fflush(stdin);
int* ans = new int[n];
int idx=0;
for(int i=0; i<n; ++i){
dist=0;
char s1[45];
char s2[45];
gets(s1);
gets(s2);
// some code which calculated 'dist'
ans[idx] = dist;
idx++;
}
return 0;
}
First I had a problem with the redirection of input. If a tried the
program and enter the input by myself, the code worked fine. But if
instead I read the input from a file (in Windows using something like
main.cpp < file.in) The data saved was wrong!
At last the only way to get it right was doing something like:
char num[1000];
gets(num);
int n=atoi(num);
My question is: The problem of using scanf() and gets() comes from the
language, from the OS, from the code itself? Are there better functions
to do what I wanted?
The other problem in which I ran was that I tried:
int const* ans = new int[n];
but then the line
ans[q] = dist;
gave my an error. I thought that ans[q] didn't change the pointer, is
it? If the pointer is supposed to by const, then how can change the
value that is q times ahead of the pointer?
Thanks!
int main(){
int n;
scanf("%d",&n);
fflush(stdin);
int* ans = new int[n];
int idx=0;
for(int i=0; i<n; ++i){
dist=0;
char s1[45];
char s2[45];
gets(s1);
gets(s2);
// some code which calculated 'dist'
ans[idx] = dist;
idx++;
}
return 0;
}
First I had a problem with the redirection of input. If a tried the
program and enter the input by myself, the code worked fine. But if
instead I read the input from a file (in Windows using something like
main.cpp < file.in) The data saved was wrong!
At last the only way to get it right was doing something like:
char num[1000];
gets(num);
int n=atoi(num);
My question is: The problem of using scanf() and gets() comes from the
language, from the OS, from the code itself? Are there better functions
to do what I wanted?
The other problem in which I ran was that I tried:
int const* ans = new int[n];
but then the line
ans[q] = dist;
gave my an error. I thought that ans[q] didn't change the pointer, is
it? If the pointer is supposed to by const, then how can change the
value that is q times ahead of the pointer?
Thanks!