IPB

Welcome Guest ( Log In | Register )



Tags
This content has not been tagged yet
 
Reply to this topicStart new topic

Commonly Asked C/c++ Questions


kvarnerexpress
no avatar
Super Member
*********
Group: Members
Posts: 407
Joined: 13-December 04
Member No.: 2,696



Post #1 post Jan 23 2005, 10:11 PM
Commonly Asked C/C++ Questions

--------------------------------------------------------------------------------

Here are some answers to commonly asked C/C++ questions on the forum. If you are new to the forum, please read this thread as your question may already be answered here. This guide is divided into many sections:
0. Frequently Asked Newbie Questions (how do I pause output, compile my program etc.)
1. Converting types
2. Formatting Output
3. String manipulation (copy, concatenate, replace etc.)
4. File Manipulation (File size, dir contents, remove files etc.)
5. Secure Programming Practices for Newbies.

This guide is a work in progress, so feel free to PM me with your suggestions
In general, people on this forum generally use the following:
On Unix-like OS (Linux, FreeBSD, OpenBSD, NetBSD, Darwin etc.):
gcc
tendra (this is a relatively unknown compiler though ).

On Windows:
Bloodshed Dev C++ (which uses gcc as the backend)
Visual C++
Borland C++ and Borland C++ Builder


0.2 How do I compile my program?
On unix-like systems:
gcc -o exename myfile.c
This will compile myfile.c to produce an executable file called exename. You can then run exename by typing ./exename

On Windows systems, different compilers have different keys to compile a program.
Bloodshed/Dev C++: Ctrl-F9 compiles, Ctrl-F10 executes.
Visual C++: F7 compiles, F5 executes.
Borland C++ Builder: Ctrl-F9 compiles, F9 executes.
Turbo-C: F9 compiles, Ctrl-F9 executes.


0.3 I cannot see my C compiler output. The window is closing too quickly for me.
This is a common question that many newbie programmers on Windows have. See http://forums.devshed.com/t157960/s.html for many solutions.

0.4 What are some handy C/C++ links?
http://www.eskimo.com/~scs/C-faq/top.html
http://www.parashift.com/c++-faq-lite/

0.5 Why does my scanf/fscanf/sscanf stop working?
Most C input is provided in a stream. That is, it is a series of characters made available one at a time. The scanf() function family are format-sensitive functions; they not only collect the characters for you, but attempt to convert them to a type (such as an integer) that you specify. They have great difficulty converting ZyGH4 to a meaningful number so they fail. The conversion attempt is governed by format specifiers that YOU provide. Since these may not match the input actually encountered, the family returns a value indicating the number of items successfully scanned AND assigned. If this value is zero, you have nothing. If this value is EOF, there was an end-of-file or other error. If you don't examine the return, how will you know? If an error occurs it will not be automatically cleared. Operations on the stream will continue to return an error until clearerr(), fseek(), fsetpos(), or rewind() is called. This means that a loop that is designed to pause for input will loop indefinitely.

The characters that f/s/scanf attempt to convert as one value are all the characters up to the first whitespace character (space, tab, newline) or up to the specified field width, or up to the first character that cannot be converted. (Note: The [ and c format directives are not whitespace delimited, but we won't consider them for the explanation here). If a character conflicts with the format specification, the function terminates and the character is left in the stream as if it had not been read. You probably will not expect it to be there to serve as input for your next call, so your input will not behave as you expect.

Example of proper usage:
int status;
status = scanf ("%s%d\n", name, &number);

Check the value of 'status' after the scanf() call. If it is not what you expect (two, in this case), you didn't get all your fields. If it is EOF, your stream is broken and will remain so until you clear the error.

0.6 Why does getline() not work correctly with Visual C++ (VC++)? Why do I have to type Enter twice for getline to process my input line?
If you're reading this, you've probably noticed that getline() is not functioning the way your book says it should -- you need to hit <enter> twice for the program to read your input. For example:

Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
string name;
cout<<"Enter a name:\n";
getline(cin, name);
cout<<name<<endl;

return 0;
}
.................next day
Go to the top of the page
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   8 j-s 5,458 30th September 2004 - 12:39 PM
Last post by: Zenchi
No New Posts   3 zach101 1,017 16th December 2008 - 04:45 PM
Last post by: minimcmonkey
No New Posts   10 Nameless_ 48 Yesterday, 12:56 PM
Last post by: Nameless_
No New Posts   2 Donegal 4,971 23rd October 2004 - 05:31 PM
Last post by: Donegal
No New Posts   3 boyCradle 2,812 25th October 2004 - 12:21 AM
Last post by: boyCradle
No New Posts   2 logophobia 4,372 25th October 2004 - 05:40 AM
Last post by: logophobia
No New Posts   8 Oni_BlueEyes 6,646 5th November 2004 - 12:46 AM
Last post by: Zenchi
No New Posts   1 Oni_BlueEyes 4,039 7th November 2004 - 04:26 AM
Last post by: DragonKing01
No New Posts   6 Oni_BlueEyes 5,055 12th November 2004 - 08:07 AM
Last post by: -prodigy-
No New Posts   0 Oni_BlueEyes 5,208 10th November 2004 - 02:02 AM
Last post by: Oni_BlueEyes
No New Posts   8 Overlord_Ian 1,418 16th December 2008 - 04:40 PM
Last post by: minimcmonkey
No New Posts   0 TempKnight 4,540 5th December 2004 - 07:27 AM
Last post by: TempKnight
No New Posts   1 kvarnerexpress 2,825 28th August 2009 - 09:57 AM
Last post by: willmark
No New Posts   0 TempKnight 2,733 24th December 2004 - 08:43 AM
Last post by: TempKnight
No New Posts   3 FaLgoR 4,198 4th January 2005 - 10:12 PM
Last post by: osknockout


 



RSS Open Discussion Time is now: 8th November 2009 - 04:28 PM

Web Hosting Powered by ComputingHost.com.