472,961 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,961 software developers and data experts.

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 1447
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David E. Smith | last post by:
I want to redirect input from one page to another, but it's POST content. If it were GET, I could just do something like: header("Location: /new/form.php?a=1&b=2") I can't rewrite the...
26
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class...
7
by: Greg | last post by:
I am trying to implement the UNIX pipe command using C but with the "->" operator. Everything works fine with 1 pipe, but when I try to use 2 or more, it hangs up when reading the pipe_in...
3
by: Sudesh | last post by:
Hi, I am a newbie to C# and Im trying to redirect standard input, output and error of a console program written in C (MS VC 6.0) to a textbox on a form. The code for the redirecting looks like...
8
by: Morpheus | last post by:
I am trying to test a function that outputs text to the standard output, presumably using a cout call. I do not have access to the source, but need to test the output. Is this possible? I can...
1
by: sreekeerthi | last post by:
hi friends...this is keerthi...i am doing a project where i want to redirect my page when ever the back button is clicked...for this i am using window.onbeforeunload method..and for page redirection...
10
by: JurgenvonOerthel | last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and Derived2 derive publicly from Base. Given a 'const Base &input' I want to initialize a 'const Derived1 &output'. If the...
9
by: joe | last post by:
Hello, I have a website set up on our server that is especially for errors. When another website encounters an error, it will redirect to this site with error details in the querystring. The...
5
by: emumcard | last post by:
Hi, I've got a subscription form and I want to make it so once someone hits submit, they are redirected to the same page. I thought maybe there was some code that could grab the URL of the...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.