473,395 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 1469
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.