Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Commonly Asked C/c++ Questions
kvarnerexpress
post Jan 23 2005, 10:11 PM
Post #1


Super Member
*********

Group: Members
Posts: 407
Joined: 13-December 04
Member No.: 2,696



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

Topics Topics
  1. Difference Between C, C++ And C#?(15)
  2. Wap Forum(9)
  3. Rpg Maker 2003 Questions(29)
  4. Meta Tags(7)
  5. Dell D600 Questions(2)
  6. Allie's Advice(5)
  7. Joomla Noob Questions(4)
  8. Sql Advantage?(5)
  9. Dog Questions(26)
  10. Questions About The Budget Freedom Plan(4)
  11. I Have Several Random Questions...(8)
  12. Webdesign Frequently Askd Questions(11)
  13. Some American Football Questions(6)
  14. What's The Problem With America's Youth?(69)
  15. Group Limitations(0)
  1. Questions About Upgrading A Freedom Package(0)
  2. Php Questions?!(19)
  3. Flash And Javascript Interaction(1)
  4. Questions?!(4)
  5. Document.write & Noscript Questions (javascript)(1)
  6. Finance Section Suggested(0)
  7. 3 Guys Asked Me Out O.o(0)
  8. Some Questions About Qupis Hosting !(2)
  9. A Couple Of Questions About Delphi(3)
  10. Tax Deduction Questions...(0)
  11. Some Of The Biggest Questions In Life.(3)
  12. Warped Tour!(0)
  13. Some Questions On Html(6)


 



- Lo-Fi Version Time is now: 11th October 2008 - 08:12 AM