468,115 Members | 2,078 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,115 developers. It's quick & easy.

const * and a problem with redirecting input

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!

Feb 20 '06 #1
1 1259
Gaijinco wrote:
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.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Feb 20 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by David E. Smith | last post: by
26 posts views Thread by Michael Klatt | last post: by
7 posts views Thread by Greg | last post: by
3 posts views Thread by Sudesh | last post: by
8 posts views Thread by Morpheus | last post: by
5 posts views Thread by emumcard | last post: by
5 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.