const * and a problem with redirecting input

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!
 
P

Phlip

Gaijinco said:
Hi! I was coding something which had some lines like these:

You need to read /Accelerated C++/ by Koenig (and Moo?).

It will tell you how to start C++ by using only the advanced features that
make simple code like this more robust.
int main(){

int n;
scanf("%d",&n);
fflush(stdin);

Even C programmers know not to use scanf(). Create a string, and get it like
this, with a turnaround of <enter>:

using namespace std;
string input;
getline(cin, input);
int* ans = new int[n];

If you must use new[], use delete[] at the bottom of this function, or the
program will leak memory.
int idx=0;

for(int i=0; i<n; ++i){

dist=0;
char s1[45];
char s2[45];
gets(s1);
gets(s2);

replace those with string s1 and getline(cin, s1), etc.
// some code which calculated 'dist'

ans[idx] = dist;
idx++;

If idx and i are always the same value, make them the same.
}

return 0;
}
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?

Yes, yes, yes, and yes.

Scanf() turns around when it stops scanning its pattern. This makes it
nearly useless for simple user interfaces. Gets() turns around when you hit
<enter>, making it more useful, however...

....if the user writes more than your 45 characters in a line, gets() will
write off the end of its array.

All these problems are why you should learn the C++ Standard Library first,
and only use high-level strings and streams, before learning the low-level
stuff that accesses memory directly.
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?

'const' associates to the thing on its left. (Only if there's nothing there
does it associate to the right.)

So your const qualifies the ints in the array. You can't write on them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top