1. Home >
  2. Apps >
  3. Groups >

Tips For C++ Programmers - Read & Learn

Question asked by Vishal Sharma in #Coffee Room on Aug 28, 2012
Vishal Sharma
Vishal Sharma 路 Aug 28, 2012
Rank B3 - LEADER
Small tips for programmers::

NOTE-> THESE TIPS ARE SUPPORTED IN DEV-C++ WHICH USES A GCC COMPILER. I SUGGEST EVERYONE TO USE DEV or VISUAL C++ BECAUSE IT WILL SURELY ENHANCE YOUR PROGRAMMING SKILLS AND INTELLIGENCE.

1) Most of us use clrscr( ); to clean up the output screen. Well, this function is not a standard function to be used in your program. Use system("cls"); which is included in stdlib.h this function is more preferable than clrscr();



2) While programming for C++, instead of
#include <iostream.h>


use

#include <iostream>
 
using namespace std;
as the programming environments have been developed and none of the people use the ancestor blue screen programming environment. Similarly, the header files ctype.h, assert.h etc can be directly writen as cctype, cassert (without .h extension)

3) In C++, using getch( ) to hold the output on screen is a bad practice. Which is not a standard function provided by ANSI C. Use
fflush (stdin);  // cstdio header
 
std::cin.get();  // iostream header

4) DO NOT USE void main( ) since it returns a garbage value to the main function, which makes it impossible to be called explicitly in some other program.
use int main( ) so that you know what value it returns.

5) And, at the end I'd like to tell something about presentation. Presentation of code is very important in all aspects. The code must be understandable not only by you but also to the other person. It doesn't matter to whom you are presenting the code. I've seen people (mostly my classmates) writing the code in very clumsy way, and when they ask me any doubt, I'm unable to understand their code, just because their way of presentation is too bad.
The same thing must be kept in mind while asking the doubts on the forums. Your codes must be understandable by the person who is seeing it, or else they loose interest in helping you out..

> Writing comments is a very good habit. Write a comment about the action of the statement in your code. It must be done for all the loops you're using. If you're able to determine where you are getting an error, write a comment indicating the error.

> Make your code more expanded and clear. Use the white spaces (space bar or tab keys ). They don't effect your program in anyway as the compiler skips all the white spaces used. But, it helps the user to understand the code easily.
ex:

int main() {printf("HELLO CEANS"); return 0;}
the compiler faces no problem in faces no problem in executing the above code. But is it presented well?? Well, everyone knows NO! We can instead write it in different way as

int main()
{
  printf ("HELLO CEANS");
  return 0;
}

The same things can be done while using the loop conditions or conditional statements

// make your code look clear...
 
for(i = 0 ; i < condition ; i++)  // spaces between the variables and operators
{                     // opening and closing braces are in same line
  .............        // loop statements are well organized
  .............
}



So that's it for now! Have a good day! CHEERS
Posted in: #Coffee Room
Ankita Katdare
Ankita Katdare 路 Aug 28, 2012
Rank A1 - PRO
Vishal0203 That is a really useful post. I have pinned it. Keep writing useful articles and posts as tutorials for CEans.
Vishal Sharma
Vishal Sharma 路 Aug 28, 2012
Rank B3 - LEADER
AbraKaDabra
Thanks a lot! 馃榾 I'll update this post if I get along some new things to make the program efficient!
Priya Nadkarni
Priya Nadkarni 路 Aug 29, 2012
Rank D3 - MASTER
Thank You. That was really useful.馃榾
Vishal Sharma
Vishal Sharma 路 Aug 29, 2012
Rank B3 - LEADER
PriyaJ
good to know that! 馃榾
monujatt
monujatt 路 Aug 30, 2012
Rank D2 - MASTER
With reference to this -> Small tips for programmers::
Your code should have proper comments ...sometimes programmer itself not able to understand what an API does(even that is written by him).... 馃榾
Vishal Sharma
Vishal Sharma 路 Aug 30, 2012
Rank B3 - LEADER
monujatt
Yeah! I've seen such people.. 馃槰
sulochana anand
sulochana anand 路 Aug 31, 2012
Rank D1 - MASTER
good knowledge given by u.thanx
Vishal Sharma
Vishal Sharma 路 Aug 31, 2012
Rank B3 - LEADER
sulochana anand
thanks .. 馃榾
gaurav91
gaurav91 路 Sep 1, 2012
Rank D3 - MASTER
i too had worked with c++!! but i didn't knew most of it ..true knowledge ..
Vishal Sharma
Vishal Sharma 路 Sep 1, 2012
Rank B3 - LEADER
gaurav91
There are many more things.
Like the use of iomanip.h
In C++, it is another header which contains the functions, which can make your presentation on output screen better!! 馃榾
Vishal Sharma
Vishal Sharma 路 Sep 1, 2012
Rank B3 - LEADER
ONE MORE THING I FORGOT TO ADD IN THE POST!

Avoid using conio.h header

conio.h is not considered as the standard library. All the compilers may not support this header. Hence we avoid using it.

So, the functions like clrscr(); and getch(); are also considered as the non standard functions. As, I've earlier mentioned, the alternative for clrscr(); is system("cls");
Similarly, for getch(); the alternative is getchar(); which is in stdio.h

Do not forget to flush the input stream before using getchar(); and its equivalent in C++ cin.get();
gaurav91
gaurav91 路 Sep 1, 2012
Rank D3 - MASTER
hey since when r u working in c++?
Vishal Sharma
Vishal Sharma 路 Sep 1, 2012
Rank B3 - LEADER
gaurav91
hey since when r u working in c++?
I'm a second year student, so, Just 3 months.. 馃榾
gaurav91
gaurav91 路 Sep 1, 2012
Rank D3 - MASTER
wow awesome buddy! so where r u studying c++ from i mean which books do u refer?
Vishal Sharma
Vishal Sharma 路 Sep 1, 2012
Rank B3 - LEADER
gaurav91
wow awesome buddy! so where r u studying c++ from i mean which books do u refer?
The books doesn't have anything related to all this. The books just teach us the basics. To make your skills advanced, do a research. 馃榾
The best thing, search the internet, join useful forums! There are many intellects across the world who are keen to help you out! And they even help making your programs more efficient!
These tips are used for efficient programming!
gaurav91
gaurav91 路 Sep 1, 2012
Rank D3 - MASTER
that's y asked becoz ..i didn't find all these tips or anything related anywhere in the books .. but all these tips r one of the unknown.. and u r sharing ur knowledge that's good.. for people like me also 馃榾
Vishal Sharma
Vishal Sharma 路 Sep 1, 2012
Rank B3 - LEADER
馃榾 I'm glad it's helpful.
majority of people in India are unaware of these things as we are never taught about all this in our college. So, the forums like CE create good platform of learning!
David Emperor
David Emperor 路 Sep 2, 2012
Rank E1 - BEGINNER
Hey dude 馃榾 add for people
that scanf() and printf () are working fast than cin and cout
the view of scanf is scanf ("%/*const char * format*/", variable);
formats are d- decimal integer , e,E,f,g,G - float types, u - unsigned int, h, H- hexidemical number, s - string, c-char , o - octal integer
for example scanf ("%f", &n); printf ("%f", n);
for newline type printf ("\n"); for tab printf ("\tab");
馃槈
when you use functions the value that you gave to function is being copied in parameters, so if you want make changes in the given variables you must use
void f(int& k, int& s)
{...}
int main()
{...
    f(n,m);
...}
instead of
void f(int k, int s)
{...}
int main()
{...
    f(n,m);
...}
for calculating 2^k fast use
int s=1;
s=s<for swaping values of variables use
a=a^b;
b=a^b;
a=b^a;
I think that's enough 馃榾
sorry for weak english 馃槚
Vishal Sharma
Vishal Sharma 路 Sep 2, 2012
Rank B3 - LEADER
that scanf() and printf () are working fast than cin and cout
you are right! 馃榾
But, these are the tips for using c++. scanf and printf functions belong to C
Hence, you cannot use them when you are asked to program in c++.

Remaining things are all fine.. 馃榾
David Emperor
David Emperor 路 Sep 2, 2012
Rank E1 - BEGINNER
bro you can use scanf and printf in c++ 馃槈
you only need to include cstdio and it will work
as well you can use fscanf and fprintf and every input/output comands that belong to C 馃槈
Vishal Sharma
Vishal Sharma 路 Sep 2, 2012
Rank B3 - LEADER
cstdio is just a new name given to stdio.h
Just because the C and C++ programs are performed in integrated environments, doesn't mean that you can flip flop the libraries.
just a difference in name, doesn't create difference in functions. If you use printf and scanf() then there will be no much difference in C and C++. As C++ is advanced, we use cin and cout instead of scanf and printf as cin and cout allows us to utilize the special features like "cascading" which cannot be done using printf().

Even the iomanip.h header functions like setw(), setprecision() which play an important role in clear presentation of output, can be easily operated using cascading of cin and cout.

for more info, see this.
https://stackoverflow.com/questions/9650812/use-of-printf-in-c




...
malaka
malaka 路 Sep 6, 2012
Rank E1 - BEGINNER
Thanks for share given good knowledge.
sulochana anand
sulochana anand 路 Sep 6, 2012
Rank D1 - MASTER
vishal really u r having vishal knowledge.good
sulochana anand
sulochana anand 路 Sep 6, 2012
Rank D1 - MASTER
David Emperor
Hey dude 馃榾 add for people
that scanf() and printf () are working fast than cin and cout
the view of scanf is scanf ("%/*const char * format*/", variable);
formats are d- decimal integer , e,E,f,g,G - float types, u - unsigned int, h, H- hexidemical number, s - string, c-char , o - octal integer
for example scanf ("%f", &n); printf ("%f", n);
for newline type printf ("\n"); for tab printf ("\tab");
馃槈
when you use functions the value that you gave to function is being copied in parameters, so if you want make changes in the given variables you must use
void f(int& k, int& s)
{...}
int main()
{...
    f(n,m);
...}
instead of
void f(int k, int s)
{...}
int main()
{...
    f(n,m);
...}
for calculating 2^k fast use
int s=1;
s=s<for swaping values of variables use
a=a^b;
b=a^b;
a=b^a;
I think that's enough 馃榾
sorry for weak english 馃槚
thanx its working.till now i was having two methods of swaping but now three.
Vishal Sharma
Vishal Sharma 路 Sep 6, 2012
Rank B3 - LEADER
sulochana anand
thanx its working.till now i was having two methods of swaping but now three.
Thanks!!
And It's just XOR method of swapping! I thought its easy so didn't share it! sorry! 馃槢
Vishal Sharma
Vishal Sharma 路 Sep 8, 2012
Rank B3 - LEADER
UPDATE:

I've seen many people using cout or (printf in C) to print just one character.
cout<< or printf is very expensive way of printing a character.
the reason for that are:

1> gets the format string
2> parse and analyze the string
3> find out that there's a CHAR format specifier
4> call the char output sub function
5> gets the character parameter
6> output the character to the screen

These are the operations performed by cout<< or printf() to print just a character. These things happen just in a fraction of seconds but as this thread is about "efficient" programming, I'll suggest you to STOP using cout<< or printf() to print one character.

You have the facility of cout.put() or (putchar() in C, both are equivalent) to print one character. It just reads the character and prints it without following so many steps like cout<< .

NOTE:: putchar() must be used only in C! Using it in C++ is not a good way to perform c++ programming. putchar() and cout.put() are equivalent but they must not be flip flopped
Shivani.bhupal
Shivani.bhupal 路 Sep 22, 2012
Rank D3 - MASTER
Vishal0203
I'm a second year student, so, Just 3 months.. 馃榾
SERIOUSLY.....udont seem like 2nd year student...plz give me tips on how you study
Vishal Sharma
Vishal Sharma 路 Sep 22, 2012
Rank B3 - LEADER
Shivani.bhupal
SERIOUSLY.....udont seem like 2nd year student...plz give me tips on how you study
Nothing special! I spend more time in playing 馃槢
I just implement what teachers teach in college, in a different way! Like making software, combining all the topics n all! Which gives you a real time experience. I got these ideas while making a software. These tips are important for efficient programming.
And another thing, pay attention in class. That's it! 馃榾
Shivani.bhupal
Shivani.bhupal 路 Sep 24, 2012
Rank D3 - MASTER
Making software..??? Can u plz give me a clue about what kind of softwares have u made and how you proceded towards it.I ll be grateful
Shivani.bhupal
Shivani.bhupal 路 Sep 24, 2012
Rank D3 - MASTER
Vishal0203
Nothing special! I spend more time in playing 馃槢
I just implement what teachers teach in college, in a different way! Like making software, combining all the topics n all! Which gives you a real time experience. I got these ideas while making a software. These tips are important for efficient programming.
And another thing, pay attention in class. That's it! 馃榾
Making software..??? Can u plz give me a clue about what kind of softwares have u made and how you proceded towards it.I ll be grateful
David Emperor
David Emperor 路 Sep 24, 2012
Rank E1 - BEGINNER
Like this 馃榾
grsalvi
grsalvi 路 Sep 28, 2012
Rank C2 - EXPERT
Why line B is getting executed before line A in following cpp program ?
馃槙
As after running the line A displays after you press EnteR.
but getchar comes after line A.
????

include
#include
#include

int main()
{

char ch;
cout<<"Enter a character: ";
cin>>ch;


for(int i=65;i<=122;i++)
{
if(ch==i)
{
cout<<"The ASCII value of "<[line A]
cout<<"Press Enter to Exit";
getchar(); [line B]

exit(0);
}
}


return 0;
}
cranberrypie
cranberrypie 路 Sep 29, 2012
Rank E1 - BEGINNER
ohhh !!!馃槖
i dont think the first three rules which you described is also known to our professors!!馃榿
anyways thanks for the info!! 馃榾
Vishal Sharma
Vishal Sharma 路 Oct 2, 2012
Rank B3 - LEADER
Shivani.bhupal
Making software..??? Can u plz give me a clue about what kind of softwares have u made and how you proceded towards it.I ll be grateful
I mostly make educational software's for my juniors and sometimes if teachers ask me to make some software then i make it, like for library records, for sports person details (branch, attendance etc.) and the best thing, I don't give these for free 馃榿 I charge them 馃榿 馃槈
Vishal Sharma
Vishal Sharma 路 Oct 2, 2012
Rank B3 - LEADER
cranberrypie
ohhh !!!馃槖
i dont think the first three rules which you described is also known to our professors!!馃榿
anyways thanks for the info!! 馃榾
yeah! Even my professors didn't know about it.. 馃槢
These things are not taught in colleges. It's good to know that its helpful 馃榾
Vishal Sharma
Vishal Sharma 路 Oct 2, 2012
Rank B3 - LEADER
grsalvi
Why line B is getting executed before line A in following cpp program ?
馃槙
As after running the line A displays after you press EnteR.
but getchar comes after line A.
????

include
#include
#include

int main()
{

char ch;
cout<<"Enter a character: ";
cin>>ch;


for(int i=65;i<=122;i++)
{
if(ch==i)
{
cout<<"The ASCII value of "<[line A]
cout<<"Press Enter to Exit";
getchar(); [line B]

exit(0);
}
}


return 0;
}
I'll have a look at it soon! BTW i didn't understand your question, can you be more clear??????
simplycoder
simplycoder 路 Oct 2, 2012
Rank B3 - LEADER
grsalvi
Why line B is getting executed before line A in following cpp program ?
馃槙
As after running the line A displays after you press EnteR.
but getchar comes after line A.
????

include
#include
#include

int main()
{

char ch;
cout<<"Enter a character: ";
cin>>ch;


for(int i=65;i<=122;i++)
{
if(ch==i)
{
cout<<"The ASCII value of "<[line A]
cout<<"Press Enter to Exit";
getchar(); [line B]

exit(0);
}
}


return 0;
}
A query from my side, I think you are following this approach.
1)Input a character.
2)Press Enter.
3)The result is shown in cmd prompt.
Now according to you, line B is executing first.
If this is your question, then the program is following its routine.
Till and unless you convey your program that the input is finished from your side, it will not process any further.
so Enter is a key reserved to break an input.
(While entering an array, space or enter is treated as the next element).
So after you press enter, you conveyed the program that you have finished with whatever you input you wanted,and now its free to process it.

If you are still in doubt, hit a cout with some text before line B.
Vishal Sharma
Vishal Sharma 路 Oct 2, 2012
Rank B3 - LEADER
grsalvi
I totally agree with simplycoder. If your question is same as what simplycoder guessed, then you just need to add cin.ignore(); before getchar();
As simplycoder said, getchar() reads the input enter key you pressed while entering the character. So, your program doesn't wait for pressing enter.
grsalvi
grsalvi 路 Oct 2, 2012
Rank C2 - EXPERT
actually the problem is, on running the code :

A character is asked .(user types character and presses enter)

Then the ASCII value gets printed after you press 'Enter key'.

This means line B is causing me to press enter and then line A is displaying ASCII value.
Why an additional enter is required ?

However i added 'endl' keyword now its working fine .Why?
New properly working code :

#include 
#include
#include



int main()
{

    char ch;
    cout<<"Enter a character: ";
    cin>>ch;
    

    for(int i=65;i<=122;i++)
    {
        if(ch==i)
        {
            cout<<"The ASCII value of "<
    
simplycoder
simplycoder 路 Oct 2, 2012
Rank B3 - LEADER
grsalvi
actually the problem is, on running the code :

A character is asked .(user types character and presses enter)

Then the ASCII value gets printed after you press 'Enter key'.

This means line B is causing me to press enter and then line A is displaying ASCII value.
Why an additional enter is required ?

However i added 'endl' keyword now its working fine .Why?
New properly working code :

#include 
#include
#include
 
 
 
int main()
{
 
    char ch;
    cout<<"Enter a character: ";
    cin>>ch;
 
 
    for(int i=65;i<=122;i++)
    {
        if(ch==i)
        {
            cout<<"The ASCII value of "<
endl is nothing but END-LINE, that means the next thing that is supposed to be displayed would be on the next line of the console.
Its not the endl, rather fflush(stdin) which does the trick.

In the previous version, you didn't write anything that would interfere after the character was entered. whereas here, it is fflush(stdin).
Vishal Sharma
Vishal Sharma 路 Oct 2, 2012
Rank B3 - LEADER
grsalvi
actually the problem is, on running the code :

A character is asked .(user types character and presses enter)

Then the ASCII value gets printed after you press 'Enter key'.

This means line B is causing me to press enter and then line A is displaying ASCII value.
Why an additional enter is required ?

However i added 'endl' keyword now its working fine .Why?
New properly working code :

#include 
#include
#include
 
 
 
int main()
{
 
    char ch;
    cout<<"Enter a character: ";
    cin>>ch;
   
 
    for(int i=65;i<=122;i++)
    {
        if(ch==i)
        {
            cout<<"The ASCII value of "<
No no no... You're getting it wrong!!
its not the getchar() being executed before displaying result.
the enter key you are pressing after typing the character is to tell the compiler that the "cin statement is finished, go to next step" viz. same as scanf(); in C.
getchar() is just to hold your output on screen it is nowhere related to the execution of program.
anymore queries?? go ahead! 馃榾
Vishal Sharma
Vishal Sharma 路 Oct 2, 2012
Rank B3 - LEADER
I mean getchar() is to get character from the keyboard. (in your program, it holds output on screen until you press a key)
grsalvi
grsalvi 路 Oct 2, 2012
Rank C2 - EXPERT
simplycoder
endl is nothing but END-LINE, that means the next thing that is supposed to be displayed would be on the next line of the console.
Its not the endl, rather fflush(stdin) which does the trick.

In the previous version, you didn't write anything that would interfere after the character was entered. whereas here, it is fflush(stdin).
No dude earlier i had added only fflush() still the problem didn't get solved.Try the code your self .Its acting weird 馃槈
grsalvi
grsalvi 路 Oct 2, 2012
Rank C2 - EXPERT
Vishal0203
I mean getchar() is to get character from the keyboard. (in your program, it holds output on screen until you press a key)
case 1:

#include
#include
#include



int main()
{

char ch;
cout<<"Enter a character: ";
cin>>ch;


for(int i=65;i<=122;i++)
{
if(ch==i)
{
cout<<"The ASCII value of "<
cout<<"Press Enter to Exit";

fflush(stdin);

getchar();

exit(0);
}
}


return 0;
}


out put : (A is typed and Enter pressed )

:Enter a character: A

output : (one more enter presssed)

Enter a character: A

The ASCII value of Ais : 65Press Enter to ExitPress any key to continue[/code]
Vishal Sharma
Vishal Sharma 路 Oct 2, 2012
Rank B3 - LEADER
grsalvi
No dude earlier i had added only fflush() still the problem didn't get solved.Try the code your self .Its acting weird 馃槈
you're right! If you are using endl, you need not flush the stream, as endl itself is stream manipulator which first prints the character '\n' and then flushes the stream itself.
If you are not using endl, you can use fflush() or to be more efficient, you can use cin.ignore()
grsalvi
grsalvi 路 Oct 2, 2012
Rank C2 - EXPERT
case 2: endl added to line B

#include
#include
#include



int main()
{

char ch;
cout<<"Enter a character: ";
cin>>ch;


for(int i=65;i<=122;i++)
{
if(ch==i)
{
cout<<"The ASCII value of "<
cout<<"Press Enter to Exit"< [line B]

fflush(stdin);

getchar();

exit(0);
}
}


return 0;
}


output: (A typed & enter pressed)

Enter a character: A
The ASCII value of Ais : 65Press Enter to Exit

Note: only one enter was sufficient to show answer .This means in case 1 : getchar() is some how making user to type one more enter before displaying answer .
grsalvi
grsalvi 路 Oct 2, 2012
Rank C2 - EXPERT
Vishal0203
you're right! If you are using endl, you need not flush the stream, as endl itself is stream manipulator which first prints the character '\n' and then flushes the stream itself.
If you are not using endl, you can use fflush() or to be more efficient, you can use cin.ignore()
Buddy even cin.ignore does not work.It is only 'endl' in line B which helps.Is this a bug in cpp?
Vishal Sharma
Vishal Sharma 路 Oct 2, 2012
Rank B3 - LEADER
grsalvi
Buddy even cin.ignore does not work.It is only 'endl' in line B which helps.Is this a bug in cpp?
umm.. did you try tracing the program??
grsalvi
grsalvi 路 Oct 2, 2012
Rank C2 - EXPERT
simplycoder Vishal0203 : see the two cases
simplycoder
simplycoder 路 Oct 2, 2012
Rank B3 - LEADER
grsalvi
case 2: endl added to line B

#include
#include
#include



int main()
{

char ch;
cout<<"Enter a character: ";
cin>>ch;


for(int i=65;i<=122;i++)
{
if(ch==i)
{
cout<<"The ASCII value of "<
cout<<"Press Enter to Exit"<[line B]

fflush(stdin);

getchar();

exit(0);
}
}


return 0;
}


output: (A typed & enter pressed)

Enter a character: A
The ASCII value of Ais : 65Press Enter to Exit

Note: only one enter was sufficient to show answer .This means in case 1 : getchar() is some how making user to type one more enter before displaying answer .
Oh, I added endl to line A.
I agree with Vishal,
cout<
Just my 2 cents.
grsalvi
grsalvi 路 Oct 2, 2012
Rank C2 - EXPERT
Vishal0203
umm.. did you try tracing the program??
Ya all instructions are executed sequentially.
But output is displayed after you respond to getchar() ,by pressing enter .
Vishal Sharma
Vishal Sharma 路 Oct 2, 2012
Rank B3 - LEADER
grsalvi
simplycoder Vishal0203 : see the two cases
I've seen the cases. Actually, i dont have any programming environment in my lappy now, so i'll suggest you to tracethe program once and see hoe it actually runs!
Vishal Sharma
Vishal Sharma 路 Oct 2, 2012
Rank B3 - LEADER
grsalvi
Ya all instructions are executed sequentially.
But output is displayed after you respond to getchar() ,by pressing enter .
oh crap!! i don't see any reason behind it..
grsalvi
grsalvi 路 Oct 2, 2012
Rank C2 - EXPERT
Guys problem solved
Ran the same code( just getchar(),no endl ,no fflush ) in turbo cpp .It worked normal馃榾.

The problem is with visual c++.馃槨
Vishal Sharma
Vishal Sharma 路 Oct 2, 2012
Rank B3 - LEADER
grsalvi
Guys problem solved
Ran the same code( just getchar(),no endl ,no fflush ) in turbo cpp .It worked normal馃榾.

The problem is with visual c++.馃槨
turbo cpp is waste!! It'll execute all unefficient things.. the true success is when it executes properly in visual C++ 馃槚
simplycoder
simplycoder 路 Oct 2, 2012
Rank B3 - LEADER
I agree with Vishal, if you dnt have Visual C++ (personally I dont suggest this as well). use DevC++.

Turbo C++ will lead you to wrong usage of C++ as language aswell as standard library functions.
grsalvi
grsalvi 路 Oct 2, 2012
Rank C2 - EXPERT
Vishal0203
turbo cpp is waste!! It'll execute all unefficient things.. the true success is when it executes properly in visual C++ 馃槚
But it visual c++ ,i told you what problem is happening.
Vishal Sharma
Vishal Sharma 路 Oct 3, 2012
Rank B3 - LEADER
grsalvi
But it visual c++ ,i told you what problem is happening.
I use DEV C++
I compiled the program and executed it too. I don't face any problems like you are facing. its just the same (only one press of enter key).
Jeffrey Arulraj
Jeffrey Arulraj 路 Oct 3, 2012
Rank A2 - PRO
Thanks pal the namespace tag can you please send some links to understand it more
Vishal Sharma
Vishal Sharma 路 Oct 4, 2012
Rank B3 - LEADER
jeffrey samuel
Thanks pal the namespace tag can you please send some links to understand it more
C++ is the introduction to the namespaces. If have a look on any higher programming language like C#, you'll see an abundant use of namespaces.

there you go... this is the best documentation about the namespaces 馃榾

https://www.cplusplus.com/doc/tutorial/namespaces/
bossbs
bossbs 路 Oct 4, 2012
Rank E1 - BEGINNER
first know about basic of c++ you can read the diploma book it will help you more
Jeffrey Arulraj
Jeffrey Arulraj 路 Oct 4, 2012
Rank A2 - PRO
bossbs
first know about basic of c++ you can read the diploma book it will help you more
Thanks for the advice pal but I prefer knowing Data structures as they help me more in applying the basics
Jeffrey Arulraj
Jeffrey Arulraj 路 Oct 4, 2012
Rank A2 - PRO
Vishal0203
C++ is the introduction to the namespaces. If have a look on any higher programming language like C#, you'll see an abundant use of namespaces.

there you go... this is the best documentation about the namespaces 馃榾

https://www.cplusplus.com/doc/tutorial/namespaces/
thanks again for the link love it
Vishal Sharma
Vishal Sharma 路 Oct 4, 2012
Rank B3 - LEADER
jeffrey samuel
Thanks for the advice pal but I prefer knowing Data structures as they help me more in applying the basics
Data structures will surely help you learn new things, but you need to know the basics like templates, overloading etc. Since we don't like the compiler catching hundreds of errors in our program. 馃槈
Jeffrey Arulraj
Jeffrey Arulraj 路 Oct 4, 2012
Rank A2 - PRO
Yeah true I learnt the basics real well but here and there I have some difficulties in understanding algorithms

In case of OOPS I am confident of them a little cos of frequent helping of other class mates I got more acquainted with it
shanmugapriya kr
shanmugapriya kr 路 Oct 21, 2012
Rank E1 - BEGINNER
thanks a lot ............. this would really help me as i am in the initial stage of engineering馃榿
Vishal Sharma
Vishal Sharma 路 Oct 22, 2012
Rank B3 - LEADER
shanmugapriya kr
thanks a lot ............. this would really help me as i am in the initial stage of engineering馃榿
I hope it does!
shanmugapriya kr
shanmugapriya kr 路 Oct 26, 2012
Rank E1 - BEGINNER
thanks a lot vishal 馃槢
Vishal0203
I hope it does!
Vishal Sharma
Vishal Sharma 路 Oct 28, 2012
Rank B3 - LEADER
UPDATE::

Use of getline() to get the input of a string or a line (common for both).

the getline function present in plays an important role to get the input of a string, as well as a line. The getline function is common for both the situations, if you just know how to use it!

We can take the input of a string through cin. But what about a line??
cin takes a continuous string and as soon as it encounters an empty space, it terminates the string.

But, by using getline() , a delimiter can be added! which means, we can tell the compiler when to terminate the string.

SYNTAX

getline(istream *, str, 'delim');

istream * : it is an input stream pointer (cin).
str : it is the declared string by the user.
'delim' : it is the delimiter. represented in single quotes.

Implementation

Let me show you a small example!

#include 
#include 
using namespace std;
int main()
{
  string a;
  cout<<"Enter the full name: "<In the above example, the delimiter is set as '\n' (you can even take it as '\r'). Hence, the input will be taken as long as you don't hit enter key.

If I take Input as: Barak Obama
then the output will be: Barak Obama

now, if I change the delimiter as

getline(cin , a , ' ');  /* delimiter set as \n */
now we see that the delimiter is set to "space". Hence the input will be taken only up to the first space. That means, the string terminates when it encounters a space.

If I take input as: Barak Obama
then the output will be: Barak

So, We see that everything is dependent on the delimiter! 馃榾
doubts can be asked comments!
grsalvi
grsalvi 路 Oct 30, 2012
Rank C2 - EXPERT
Vishal0203
UPDATE::

Use of getline() to get the input of a string or a line (common for both).

the getline function present in plays an important role to get the input of a string, as well as a line. The getline function is common for both the situations, if you just know how to use it!

We can take the input of a string through cin. But what about a line??
cin takes a continuous string and as soon as it encounters an empty space, it terminates the string.

But, by using getline() , a delimiter can be added! which means, we can tell the compiler when to terminate the string.

SYNTAX

getline(istream *, str, 'delim');

istream * : it is an input stream pointer (cin).
str : it is the declared string by the user.
'delim' : it is the delimiter. represented in single quotes.

Implementation

Let me show you a small example!

#include 
#include 
using namespace std;
int main()
{
  string a;
  cout<<"Enter the full name: "<In the above example, the delimiter is set as '\n' (you can even take it as '\r'). Hence, the input will be taken as long as you don't hit enter key.

If I take Input as: Barak Obama
then the output will be: Barak Obama

now, if I change the delimiter as

getline(cin , a , ' ');  /* delimiter set as \n */
now we see that the delimiter is set to "space". Hence the input will be taken only up to the first space. That means, the string terminates when it encounters a space.

If I take input as: Barak Obama
then the output will be: Barak

So, We see that everything is dependent on the delimiter! 馃榾
doubts can be asked comments!
The Function cin.getline() , does the same. Right ??
grsalvi
grsalvi 路 Oct 30, 2012
Rank C2 - EXPERT
One Of the added feature of ANSI c++ is Standard template library.
The containers like MAP are very useful.Hence i am using the STL Map.
I wanted to know if we can store a map (with keys and values we have entered in it) to a file and also read back from it.
I checked web but found nothing proper.
If possible post code for both read and write.
Vishal Sharma
Vishal Sharma 路 Oct 30, 2012
Rank B3 - LEADER
grsalvi
The Function cin.getline() , does the same. Right ??
there is a difference!
cin.getline() belongs to whereas getline() belongs to
to use cin.getline() you need to specify the size of string like we do in C programming (char a[10]).
but, in getline you don't need to specify a size because it grows dynamically (declared as string a).
Vishal Sharma
Vishal Sharma 路 Oct 30, 2012
Rank B3 - LEADER
grsalvi
One Of the added feature of ANSI c++ is Standard template library.
The containers like MAP are very useful.Hence i am using the STL Map.
I wanted to know if we can store a map (with keys and values we have entered in it) to a file and also read back from it.
I checked web but found nothing proper.
If possible post code for both read and write.
it is little advanced for me!! [Prototype]
might know the answer for this!
I've tagged him here.. hope he helps you 馃榾
grsalvi
grsalvi 路 Oct 30, 2012
Rank C2 - EXPERT
grsalvi
The Function cin.getline() , does the same. Right ??
okay so getline is used for string objects.
I was wondering that the string objects also follow the rule of '\0' at end of characters as in character arrays ??
Vishal Sharma
Vishal Sharma 路 Oct 30, 2012
Rank B3 - LEADER
grsalvi
okay so getline is used for string objects.
I was wondering that the string objects also follow the rule of '\0' at end of characters as in character arrays ??
getline() takes a delimiter. As I've mentioned in the post. '\0' is inbuilt for this. As soon as the compiler encounters the delimiter it places a string terminator at the end.
grsalvi
grsalvi 路 Oct 31, 2012
Rank C2 - EXPERT
Check out following code.

Case 1:
#include
fstream f;
f.open("d:\\test.tsv",ios::out|ios::in);
For the above code.
The program creates file test.tsv and gives no error for opening that is { f.fail() returns 0}.

Case : 2

#include
using namespace std;
 
fstream f;    f.open("d:\\test.tsv",ios::out|ios::in);
In 2nd case also code is same but library has been changed to ANSI one.
Now problem is ,on running program ,file "test.tsv" is not created and f.fail() returns true.



Why its like that ? And how can i get functioning like that of case 1,using ANSI fstream library file.
Vishal Sharma
Vishal Sharma 路 Oct 31, 2012
Rank B3 - LEADER
grsalvi
which programming environment do you use??
grsalvi
grsalvi 路 Oct 31, 2012
Rank C2 - EXPERT
Vishal0203
grsalvi
which programming environment do you use??
IDE is visual c++ 6.0 .
Vishal Sharma
Vishal Sharma 路 Oct 31, 2012
Rank B3 - LEADER
grsalvi
IDE is visual c++ 6.0 .
Okay!
the first code snippet is inefficient and wrong. you cannot declare both ios::in and ios:: out together and it doesn't follow any ANSI standard. Since you are using Visual C++, all the codes following any standard of the turbo C or the DEV C or any other environment,works fine. But, If you try to execute the first snippet in DEV, you'll get an error as the .h formats are not for C++.

Your second snippet is completely fine, but it doesn't produce the file because you've confused the compiler! The file is unavailable in the system at the start. So to create the file, you must either use ios::in or ios:: out (not together).

still if you want to use both together, add the append mode too.

f.open("d:\\test.tsv",ios::out|ios::app|ios::in);
you can try seeing any code snippets across the web. You'll never see ios::in and ios:: out together

Hope you understood! awaiting for your reply!
grsalvi
grsalvi 路 Oct 31, 2012
Rank C2 - EXPERT
Vishal0203
Okay!
the first code snippet is inefficient and wrong. you cannot declare both ios::in and ios:: out together and it doesn't follow any ANSI standard. Since you are using Visual C++, all the codes following any standard of the turbo C or the DEV C or any other environment,works fine. But, If you try to execute the first snippet in DEV, you'll get an error as the .h formats are not for C++.

Your second snippet is completely fine, but it doesn't produce the file because you've confused the compiler! The file is unavailable in the system at the start. So to create the file, you must either use ios::in or ios:: out (not together).

still if you want to use both together, add the append mode too.

f.open("d:\\test.tsv",ios::out|ios::app|ios::in);
you can try seeing any code snippets across the web. You'll never see ios::in and ios:: out together

Hope you understood! awaiting for your reply!
Thanl you, i got the point.
The use of ios::in and ios:: out is for the purpose that if you want a single file stream to be used for
writing and reading a file.
This common mode is used many times in text book of cpp but with file.
Anyways issue resolved.
Vishal Sharma
Vishal Sharma 路 Oct 31, 2012
Rank B3 - LEADER
grsalvi
Thanl you, i got the point.
The use of ios::in and ios:: out is for the purpose that if you want a single file stream to be used for
writing and reading a file.
This common mode is used many times in text book of cpp but with file.
Anyways issue resolved.
when programming in C++, using .h format is of no use.. C++ is the next step of C then why not moving on to next step instead of following the ancestor ways.. That's the reason I preffer programming in DEV, as it doesn't allow such old methods to work! (shows an error even if you use conio.h header)

I prefer these books.. These are very helpful..
The Complete reference C++
How to program C++ -> Dietel
safiajen0055
safiajen0055 路 Nov 15, 2012
Rank D1 - MASTER
Vishal0203

Hiiiiii,
Thanks for sharing the tips For C++ Programmers. This post is very useful for me.
Please keep sharing this type of useful information with us...
Vishal Sharma
Vishal Sharma 路 Nov 15, 2012
Rank B3 - LEADER
safiajen0055
Good to know that it was useful to you. There are other useful posts pinned in the discussion area. They may help you learn more! 馃榾
safiajen0055
safiajen0055 路 Nov 20, 2012
Rank D1 - MASTER
Vishal0203

Thanks for telling me this .
I want to ask you one more thing can you help about the Java programming.
Because i have so many problems with Java Programming.
Please Reply me.. I am waiting for you Reply
fahad baig
fahad baig 路 Nov 20, 2012
Rank E2 - BEGINNER
this is very useful post for us.keep posting these kind of posts which helps us in programming ic various languages.
simplycoder
simplycoder 路 Nov 20, 2012
Rank B3 - LEADER
safiajen0055
Vishal0203

Thanks for telling me this .
I want to ask you one more thing can you help about the Java programming.
Because i have so many problems with Java Programming.
Please Reply me.. I am waiting for you Reply
I think it would be better if you state your problem without waiting for a reply (preferably in a new thread) on this forum. In case if Vishal0203 is not available, someone else might help you out. Or there can be multiple solutions etc..etc
Vishal Sharma
Vishal Sharma 路 Nov 20, 2012
Rank B3 - LEADER
safiajen0055
Vishal0203

Thanks for telling me this .
I want to ask you one more thing can you help about the Java programming.
Because i have so many problems with Java Programming.
Please Reply me.. I am waiting for you Reply
I think simply coder is right! I'm not familiar to java yet 馃槖 The subject is in next semester..
So it would be better if you create a new thread so that you'll get your problems solved soon.
safiajen0055
safiajen0055 路 Nov 23, 2012
Rank D1 - MASTER
Vishal0203
I think simply coder is right! I'm not familiar to java yet 馃槖 The subject is in next semester..
So it would be better if you create a new thread so that you'll get your problems solved soon.

Hiiiii ,

Thanks for Advise me. I will create new thread related to Java Programming problems ..

馃榾 馃榾
Chinu1
Chinu1 路 Nov 27, 2012
Rank D1 - MASTER
Hello... You give me best knowledge about system("cls");. Before reading your sharing, always i was use clrscr();.
Vishal Sharma
Vishal Sharma 路 Nov 27, 2012
Rank B3 - LEADER
Chinu1
Hello... You give me best knowledge about system("cls");. Before reading your sharing, always i was use clrscr();.
Yeah. I think almost everyone starts with that. But if you notice at the headers of clrscr(); and system("cls"); then, you see that clrscr(); belongs to conio.h (console input output) whereas, system("cls"); belongs to stdlib.h (standard library). Since, it is STANDARD, we use system("cls");
UGINIRAJ
UGINIRAJ 路 Dec 1, 2012
Rank E2 - BEGINNER
Vishal0203
Small tips for programmers::

NOTE-> THESE TIPS ARE SUPPORTED IN DEV-C++ WHICH USES A GCC COMPILER. I SUGGEST EVERYONE TO USE DEV or VISUAL C++ BECAUSE IT WILL SURELY ENHANCE YOUR PROGRAMMING SKILLS AND INTELLIGENCE.

1) Most of us use clrscr( ); to clean up the output screen. Well, this function is not a standard function to be used in your program. Use system("cls"); which is included in stdlib.h this function is more preferable than clrscr();


2) While programming for C++, instead of
#include 


use

#include 
 
using namespace std;
as the programming environments have been developed and none of the people use the ancestor blue screen programming environment. Similarly, the header files ctype.h, assert.h etc can be directly writen as cctype, cassert (without .h extension)

3) In C++, using getch( ) to hold the output on screen is a bad practice. Which is not a standard function provided by ANSI C. Use
fflush (stdin);  // cstdio header
 
std::cin.get();  // iostream header

4) DO NOT USE void main( ) since it returns a garbage value to the main function, which makes it impossible to be called explicitly in some other program.
use int main( ) so that you know what value it returns.

5) And, at the end I'd like to tell something about presentation. Presentation of code is very important in all aspects. The code must be understandable not only by you but also to the other person. It doesn't matter to whom you are presenting the code. I've seen people (mostly my classmates) writing the code in very clumsy way, and when they ask me any doubt, I'm unable to understand their code, just because their way of presentation is too bad.
The same thing must be kept in mind while asking the doubts on the forums. Your codes must be understandable by the person who is seeing it, or else they loose interest in helping you out..

> Writing comments is a very good habit. Write a comment about the action of the statement in your code. It must be done for all the loops you're using. If you're able to determine where you are getting an error, write a comment indicating the error.

> Make your code more expanded and clear. Use the white spaces (space bar or tab keys ). They don't effect your program in anyway as the compiler skips all the white spaces used. But, it helps the user to understand the code easily.
ex:

int main() {printf("HELLO CEANS"); return 0;}
the compiler faces no problem in faces no problem in executing the above code. But is it presented well?? Well, everyone knows NO! We can instead write it in different way as

int main()
{
  printf ("HELLO CEANS");
  return 0;
}
The same things can be done while using the loop conditions or conditional statements

// make your code look clear...
 
for(i = 0 ; i < condition ; i++)  // spaces between the variables and operators
{                    // opening and closing braces are in same line
  .............        // loop statements are well organized
  .............
}



So that's it for now! Have a good day! CHEERS
this is very very useful foe us keep it going for better growth....
dhruba sunuwar
dhruba sunuwar 路 Jan 20, 2013
Rank D1 - MASTER
hey can you suggest me some c++ projects for high school!!!
Jeffrey Arulraj
Jeffrey Arulraj 路 Jan 20, 2013
Rank A2 - PRO
dhruba sunuwar
hey can you suggest me some c++ projects for high school!!!
Check our project section Buddy. Don't misuse other threads here PLS
dhruba sunuwar
dhruba sunuwar 路 Jan 20, 2013
Rank D1 - MASTER
Conqueror
Check our project section Buddy. Don't misuse other threads here PLS
i am really sorry but i am new to this site.so, don't know much about it..but where is this "project section"???
Neeraj Sharma
Neeraj Sharma 路 Jan 21, 2013
Rank B2 - LEADER
dhruba sunuwar
i am really sorry but i am new to this site.so, don't know much about it..but where is this "project section"???
Here it is

https://www.crazyengineers.com/community/forums/project-ideas-seminar-topics.56/

Please explore the site as you will be able to find lot of god things here
Min1a1k1shi
Min1a1k1shi 路 Feb 9, 2013
Rank E1 - BEGINNER
Thanks a lot for sharing this awesome information 馃槈
shibi raj
shibi raj 路 Feb 12, 2013
Rank D3 - MASTER
It's so nice..
Thanks very much for posting this..
Vishal Sharma
Vishal Sharma 路 Feb 16, 2013
Rank B3 - LEADER
Many people ask suggestion for C++ projects.
Its not me or someone else here, will give you the ideas. It has to be generated in your brain..
If you come up with an idea, just follow the following steps to check whether you are making it right

==> BEFORE CODING
only a few points to be remembered before typing the code.
> Know about the needs of the client (generally a question for students).
> Construct a basic idea of the concepts to be used to meet the needs of client.
> Always program, keeping the idea about the future needs of the client. Predict the mind of client and plan up for further demands of client.
> The needs of the client may change! So always add comments to your code for further references.

==> IMPORTANT

Make sure you make the program user friendly. Program in such a way that the users who do not know anything about programming can also understand what they have to do.

Your Program must always be ready to handle the exceptions to avoid application crash. There are users who enter some unknown or out range data.. at this situation application must,
1> handle the exception
2> Generate error message
3> rollback
For example, in a calculator if you enter a number 100000000000000000000000000000 the calculator may generate a message "out of range". Same Idea must be implemented.

to know more about exception handling in C++/C refer to this
Feel Free to ask doubts 馃榾
shravan kumar.TV
shravan kumar.TV 路 Feb 17, 2013
Rank E1 - BEGINNER
Its a valuable information and useful to beginners...Thanks for that information..
vikaskumar11233
vikaskumar11233 路 Feb 25, 2013
Rank C2 - EXPERT
Hey, Every thing is clear to me, but I have a little doubt in void main() function. I learn that void returns nothing, then why do you say that it returns a garbage value. Please made this concept clear to me.
Vishal Sharma
Vishal Sharma 路 Feb 25, 2013
Rank B3 - LEADER
vikaskumar11233
Hey, Every thing is clear to me, but I have a little doubt in void main() function. I learn that void returns nothing, then why do you say that it returns a garbage value. Please made this concept clear to me.
Declaring main() as void doesn't mean that the program won't return anything. It means that the program may not return a fixed value (garbage value), i.e. it may not return 0 like in int main() (we return 0 or EXIT_SUCCESS at the end if we declare it as int).

Every c/c++ program must return 0 to indicate its success. And, other reason is C/C++ programming standards. They say that int main() is the best way. (the reason i've explained you above)
Indo European
Indo European 路 Feb 27, 2013
Rank E2 - BEGINNER
The key to superior software engineering is to hub away from developing monolithic applications that do only one job, and focus on budding libraries. One way to think of libraries is as a program with many entry points. Every record you write becomes an inheritance that you can pass on to other developers. Just like in mathematics you expand little theorems and use the little theorems to hide the complexity in proving bigger theorems, in software engineering you develop libraries to take care of low-level details once and for all so that they are out of the way every time you make a deferent implementation for a variation of the problem.
safiajen0055
safiajen0055 路 Apr 10, 2013
Rank D1 - MASTER
Nice In
Vishal0203
Small tips for programmers::

NOTE-> THESE TIPS ARE SUPPORTED IN DEV-C++ WHICH USES A GCC COMPILER. I SUGGEST EVERYONE TO USE DEV or VISUAL C++ BECAUSE IT WILL SURELY ENHANCE YOUR PROGRAMMING SKILLS AND INTELLIGENCE.

1) Most of us use clrscr( ); to clean up the output screen. Well, this function is not a standard function to be used in your program. Use system("cls"); which is included in stdlib.h this function is more preferable than clrscr();


2) While programming for C++, instead of
#include 


use

#include 
 
using namespace std;
as the programming environments have been developed and none of the people use the ancestor blue screen programming environment. Similarly, the header files ctype.h, assert.h etc can be directly writen as cctype, cassert (without .h extension)

3) In C++, using getch( ) to hold the output on screen is a bad practice. Which is not a standard function provided by ANSI C. Use
fflush (stdin);  // cstdio header
 
std::cin.get();  // iostream header

4) DO NOT USE void main( ) since it returns a garbage value to the main function, which makes it impossible to be called explicitly in some other program.
use int main( ) so that you know what value it returns.

5) And, at the end I'd like to tell something about presentation. Presentation of code is very important in all aspects. The code must be understandable not only by you but also to the other person. It doesn't matter to whom you are presenting the code. I've seen people (mostly my classmates) writing the code in very clumsy way, and when they ask me any doubt, I'm unable to understand their code, just because their way of presentation is too bad.
The same thing must be kept in mind while asking the doubts on the forums. Your codes must be understandable by the person who is seeing it, or else they loose interest in helping you out..

> Writing comments is a very good habit. Write a comment about the action of the statement in your code. It must be done for all the loops you're using. If you're able to determine where you are getting an error, write a comment indicating the error.

> Make your code more expanded and clear. Use the white spaces (space bar or tab keys ). They don't effect your program in anyway as the compiler skips all the white spaces used. But, it helps the user to understand the code easily.
ex:

int main() {printf("HELLO CEANS"); return 0;}
the compiler faces no problem in faces no problem in executing the above code. But is it presented well?? Well, everyone knows NO! We can instead write it in different way as

int main()
{
  printf ("HELLO CEANS");
  return 0;
}
The same things can be done while using the loop conditions or conditional statements

// make your code look clear...
 
for(i = 0 ; i < condition ; i++)  // spaces between the variables and operators
{                    // opening and closing braces are in same line
  .............        // loop statements are well organized
  .............
}



So that's it for now! Have a good day! CHEERS

Nice Information 馃榾
Thanks for sharing this Information With us 馃榾
Vishal Sharma
Vishal Sharma 路 Apr 10, 2013
Rank B3 - LEADER
safiajen0055
Nice In


Nice Information 馃榾
Thanks for sharing this Information With us 馃榾
and yeah, instead of fflush(stdin) before cin.get(), you can even use cin.sync()
it avoids inclusion of another header file cstdio 馃榾
Ganesh MSD
Ganesh MSD 路 Apr 12, 2013
Rank D2 - MASTER
Vishal0203
Small tips for programmers::

NOTE-> THESE TIPS ARE SUPPORTED IN DEV-C++ WHICH USES A GCC COMPILER. I SUGGEST EVERYONE TO USE DEV or VISUAL C++ BECAUSE IT WILL SURELY ENHANCE YOUR PROGRAMMING SKILLS AND INTELLIGENCE.

1) Most of us use clrscr( ); to clean up the output screen. Well, this function is not a standard function to be used in your program. Use system("cls"); which is included in stdlib.h this function is more preferable than clrscr();


2) While programming for C++, instead of
#include 


use

#include 
 
using namespace std;
as the programming environments have been developed and none of the people use the ancestor blue screen programming environment. Similarly, the header files ctype.h, assert.h etc can be directly writen as cctype, cassert (without .h extension)

3) In C++, using getch( ) to hold the output on screen is a bad practice. Which is not a standard function provided by ANSI C. Use
fflush (stdin);  // cstdio header
 
std::cin.get();  // iostream header

4) DO NOT USE void main( ) since it returns a garbage value to the main function, which makes it impossible to be called explicitly in some other program.
use int main( ) so that you know what value it returns.

5) And, at the end I'd like to tell something about presentation. Presentation of code is very important in all aspects. The code must be understandable not only by you but also to the other person. It doesn't matter to whom you are presenting the code. I've seen people (mostly my classmates) writing the code in very clumsy way, and when they ask me any doubt, I'm unable to understand their code, just because their way of presentation is too bad.
The same thing must be kept in mind while asking the doubts on the forums. Your codes must be understandable by the person who is seeing it, or else they loose interest in helping you out..

> Writing comments is a very good habit. Write a comment about the action of the statement in your code. It must be done for all the loops you're using. If you're able to determine where you are getting an error, write a comment indicating the error.

> Make your code more expanded and clear. Use the white spaces (space bar or tab keys ). They don't effect your program in anyway as the compiler skips all the white spaces used. But, it helps the user to understand the code easily.
ex:

int main() {printf("HELLO CEANS"); return 0;}
the compiler faces no problem in faces no problem in executing the above code. But is it presented well?? Well, everyone knows NO! We can instead write it in different way as

int main()
{
  printf ("HELLO CEANS");
  return 0;
}
The same things can be done while using the loop conditions or conditional statements

// make your code look clear...
 
for(i = 0 ; i < condition ; i++)  // spaces between the variables and operators
{                    // opening and closing braces are in same line
  .............        // loop statements are well organized
  .............
}



So that's it for now! Have a good day! CHEERS
Nice post.! It is so useful..!!
Min1a1k1shi
Min1a1k1shi 路 Apr 21, 2013
Rank E1 - BEGINNER
The tips herein do not only explain how to write better code, but rather, they present the rationale behind the new language rules. Obviously, there are many other perennial good tips that C++ programmers can benefit from. However, I'm sure that this collection will provide you with many insights and guidelines for professional, efficient, and bug-free C++while teaching you some new coding and design techniques
Badbade Abhi
Badbade Abhi 路 Apr 26, 2013
Rank E1 - BEGINNER
Vishal0203
Small tips for programmers::

NOTE-> THESE TIPS ARE SUPPORTED IN DEV-C++ WHICH USES A GCC COMPILER. I SUGGEST EVERYONE TO USE DEV or VISUAL C++ BECAUSE IT WILL SURELY ENHANCE YOUR PROGRAMMING SKILLS AND INTELLIGENCE.

1) Most of us use clrscr( ); to clean up the output screen. Well, this function is not a standard function to be used in your program. Use system("cls"); which is included in stdlib.h this function is more preferable than clrscr();


2) While programming for C++, instead of
#include 


use

#include 
 
using namespace std;
as the programming environments have been developed and none of the people use the ancestor blue screen programming environment. Similarly, the header files ctype.h, assert.h etc can be directly writen as cctype, cassert (without .h extension)

3) In C++, using getch( ) to hold the output on screen is a bad practice. Which is not a standard function provided by ANSI C. Use
fflush (stdin);  // cstdio header
 
std::cin.get();  // iostream header

4) DO NOT USE void main( ) since it returns a garbage value to the main function, which makes it impossible to be called explicitly in some other program.
use int main( ) so that you know what value it returns.

5) And, at the end I'd like to tell something about presentation. Presentation of code is very important in all aspects. The code must be understandable not only by you but also to the other person. It doesn't matter to whom you are presenting the code. I've seen people (mostly my classmates) writing the code in very clumsy way, and when they ask me any doubt, I'm unable to understand their code, just because their way of presentation is too bad.
The same thing must be kept in mind while asking the doubts on the forums. Your codes must be understandable by the person who is seeing it, or else they loose interest in helping you out..

> Writing comments is a very good habit. Write a comment about the action of the statement in your code. It must be done for all the loops you're using. If you're able to determine where you are getting an error, write a comment indicating the error.

> Make your code more expanded and clear. Use the white spaces (space bar or tab keys ). They don't effect your program in anyway as the compiler skips all the white spaces used. But, it helps the user to understand the code easily.
ex:

int main() {printf("HELLO CEANS"); return 0;}
the compiler faces no problem in faces no problem in executing the above code. But is it presented well?? Well, everyone knows NO! We can instead write it in different way as

int main()
{
  printf ("HELLO CEANS");
  return 0;
}
The same things can be done while using the loop conditions or conditional statements

// make your code look clear...
 
for(i = 0 ; i < condition ; i++)  // spaces between the variables and operators
{                    // opening and closing braces are in same line
  .............        // loop statements are well organized
  .............
}



So that's it for now! Have a good day! CHEERS
Thank you Vishal sir.. In future further programming i will definitely follow your instruction...Hattsss of your knowledge sir...
with regards,
Abhi
Vishal Sharma
Vishal Sharma 路 Apr 26, 2013
Rank B3 - LEADER
Badbade Abhi
Thank you Vishal sir.. In future further programming i will definitely follow your instruction...Hattsss of your knowledge sir...
with regards,
Abhi
I hope you do! because most people say that they'll program in this way but no one actually does!
Sanket0731
Sanket0731 路 Jun 29, 2013
Rank E1 - BEGINNER
what is the meaning of the line 'using namespace std'?
what are namespaces?
simplycoder
simplycoder 路 Jun 29, 2013
Rank B3 - LEADER
Sanket0731
what is the meaning of the line 'using namespace std'?
what are namespaces?

Try to google up. Google is your best friend in most of the cases.
zeeshanaayan07
zeeshanaayan07 路 Jun 30, 2013
Rank E1 - BEGINNER
C++ is the mother of language without c++ we can service coding in all web developer software. Thanks for sharing a great post. I appreciate your thinking.
vikaskumar11233
vikaskumar11233 路 Jul 4, 2013
Rank C2 - EXPERT
Okay now i got it Vishal, thank you for solving my problem void main() declaration. I want to know more about the standard libraries and their declaration. Like I was making a program of AND and NAND gates but it was showing error in Library file declaration. Is there any need of some other standard libraries declaration which I was missing in this program ?
Neeraj Sharma
Neeraj Sharma 路 Jul 5, 2013
Rank B2 - LEADER
vikaskumar11233
Okay now i got it Vishal, thank you for solving my problem void main() declaration. I want to know more about the standard libraries and their declaration. Like I was making a program of AND and NAND gates but it was showing error in Library file declaration. Is there any need of some other standard libraries declaration which I was missing in this program ?
Can you share a code snippet of where exactly you were facing the problem?
Vishal Sharma
Vishal Sharma 路 Jul 5, 2013
Rank B3 - LEADER
vikaskumar11233
Okay now i got it Vishal, thank you for solving my problem void main() declaration. I want to know more about the standard libraries and their declaration. Like I was making a program of AND and NAND gates but it was showing error in Library file declaration. Is there any need of some other standard libraries declaration which I was missing in this program ?
I'm unable to visualize need a snippet. If not the exact code post an example similar to it!
zeeshanaayan07
zeeshanaayan07 路 Jul 6, 2013
Rank E1 - BEGINNER
I think this the c++ coding
Neeraj Sharma
Neeraj Sharma 路 Jul 7, 2013
Rank B2 - LEADER
zeeshanaayan07
I think this the c++ coding
I know its C++ coding. I want him to post the part of the code he is facing problems with.
zeeshanaayan07
zeeshanaayan07 路 Jul 7, 2013
Rank E1 - BEGINNER
Learn C++ Coding because this is the basic thing which the base of all coding like html, css, php, csharp etc. if you want to learn c++ tutorial click there are the brilliant tutorial
Neeraj Sharma
Neeraj Sharma 路 Jul 7, 2013
Rank B2 - LEADER
zeeshanaayan07
Learn C++ Coding because this is the basic thing which the base of all coding like html, css, php, csharp etc. if you want to learn c++ tutorial click there are the brilliant tutorial
I guess you don't understand what I am asking him. I know C++ since a lot of years.
I would recommend you to read my posts and understand it before giving your advice.
Indo European
Indo European 路 Jul 11, 2013
Rank E2 - BEGINNER
Suppose you declare something like this in your code:
const int* ptr=new int;
To who is constantans is bided? Pointer or Pointed object? The ans is pointed object. So the following is legal:
int u;
int v;
const int* ptr=&u;
ptr=&v;//OK, pointer can be changed
But not the following:
int u=3;
const int* ptr=&u;
*ptr=4;//error, pointer is declared to address const int
As pointer is being declared to point to const object and not itself being const, it can be declared without being initialized:
const int* ptr;//Ok, no initialization of ptr needed.
Now, suppose you want to declare pointer itself to be constant, so that the pointer cannot change its value and hence, will point only one object during its existence. Most of us will declare following when asked to do this for the first time in our life:
int const* ptr;

But this means the same as above, ptr happens to address a constant object. To declare pointer itself to be constant, use the following segment:
int* const ptr=&someint;

Now you can't make ptr to point to something else, but you can change the value of the object being pointed by it. To declare a const pointer which points to an const object any of the two segment is ok:
int x=2;
const int* const ptr1=&x;
int const* const ptr2=&x;

Passing one or more argument to a function as const: When you pass an argument to a function as const, it doesn't mean that you can't pass non-const object as an argument to that object. Only thing you are telling the compiler is that this variable has to retain its value during the execution of that function. So, if by mistake you try to change its value inside the function code, compiler will flag error.
void somefunc(const int num){
//...
++num;//OOPs, by mistake, But compiler will show it as an error
//saving your precious time from hunting for the possible bug here
//..
}
simplycoder
simplycoder 路 Aug 6, 2013
Rank B3 - LEADER
jimm
hey guys hello !! and i am trying to do concatenate two words using while loop and in c++ and i am getting error so which values should i take with int x,y,concatenate or what ?
please help me day after tommorow is my exam of c++.

thank you in advance.
I am not sure what you mean by concatenation using while loop?
a loop would be used only for iterative process.

Anywas, show your work, then we would be able to help you.
Vishal Sharma
Vishal Sharma 路 Aug 6, 2013
Rank B3 - LEADER
jimm
hey guys hello !! and i am trying to do concatenate two words using while loop and in c++ and i am getting error so which values should i take with int x,y,concatenate or what ?
please help me day after tommorow is my exam of c++.

thank you in advance.

If you show your approach we'll try to correct you.
Direct codes are not given!
Jeffrey Arulraj
Jeffrey Arulraj 路 Aug 6, 2013
Rank A2 - PRO
Well just use the logic till word end that is Null character the first word has to be stored in the array

When null is encountered using if clause call the second word and do the rest of the assignment you are done with your programming

About x,y Not sure what you are asking here mate
jimm
hey guys hello !! and i am trying to do concatenate two words using while loop and in c++ and i am getting error so which values should i take with int x,y,concatenate or what ?
please help me day after tommorow is my exam of c++.

thank you in advance.
Vishal Sharma
Vishal Sharma 路 Aug 9, 2013
Rank B3 - LEADER
A Better way to code In C++

As, C++ gives you feature of class, you can use it to make separate modules or headers and just LINK it to your program. This reduces the size of code and each module becomes self explanatory.

You just need to invoke the object of the class you have in your header in to your main file. Remember, that's how cin and cout work. They are objects not functions like printf() and scanf().

I'm not giving any code here, just an example
suppose you have to conduct an exam, and you have to write a code for the interface, then the questions are generated in separate module, solutions are checked in a separate module and your marks are evaluated in separate module. Your interface just uses the objects of those modules to do its task.
Then, instead of writing a long code, you can do something like this..
#include "iostream"
#include "questions.h"
#include "solutions.h"
#include "marks.h"
using namespace std;
int main() {
  ...............
  ...............
}
your questions are generated in questions.h
solution is checked in solution.h
marks are evaluated in marks.h
Finally your result is displayed through your main.cpp file
sneha thorat
sneha thorat 路 Aug 11, 2013
Rank E1 - BEGINNER
i agree with vishal printf and scanf is used in the c.if there is program in c++ cin and cout is used instead of printf and scanf.
simplycoder
simplycoder 路 Aug 11, 2013
Rank B3 - LEADER
sneha thorat
i agree with vishal printf and scanf is used in the c.if there is program in c++ cin and cout is used instead of printf and scanf.

cin and cout are preferred in C++ program.
However, scanf and printf would work faster than cin and cout.

Same way a C program can also be programmed in the way Vishal0203 has written.

Am I missing anything here?
Vishal Sharma
Vishal Sharma 路 Aug 11, 2013
Rank B3 - LEADER
simplycoder
cin and cout are preferred in C++ program.
However, scanf and printf would work faster than cin and cout.

Same way a C program can also be programmed in the way Vishal0203 has written.

Am I missing anything here?

Yeah, printf and scanf provide fast access.. can't deny that! But standards and cascading! 馃榾

and yeah, even a C program can be made that way, I didn't say that it cant be done in C. I'm just illustrating the general concept used in C++
Almost all the inbuilt header files (after c++) have classes and require object to use its functionality.. So, my post (i think) makes it easier to understand if we just try to compare the already existing things to this post!
sneha thorat
sneha thorat 路 Sep 1, 2013
Rank E1 - BEGINNER
k.i got it.........
sneha thorat
sneha thorat 路 Sep 1, 2013
Rank E1 - BEGINNER
simplycoder
cin and cout are preferred in C++ program.
However, scanf and printf would work faster than cin and cout.

Same way a C program can also be programmed in the way Vishal0203 has written.

Am I missing anything here?
you are right........m just saying that on the basis of c++ program.
Vishal Sharma
Vishal Sharma 路 Sep 2, 2013
Rank B3 - LEADER
sneha thorat
you are right........m just saying that on the basis of c++ program.
Yeah! actually it doesn't make much difference...
cout

this is for cout!

pf
This is for printf()
sneha thorat
sneha thorat 路 Sep 2, 2013
Rank E1 - BEGINNER
Vishal0203
Yeah! actually it doesn't make much difference...
cout

this is for cout!

pf
This is for printf()
yaah i understood.thanks for the extra knowledge.
Vishal Sharma
Vishal Sharma 路 Oct 1, 2013
Rank B3 - LEADER
UPDATE

Now This Post goes for C programming (can be used in c++ too but i prefer getline() for this in c++ as it matches the standard)

While taking the input of a string, many of us use scanf("%s",a); etc.
this way is correct but it cannot handle the exception of exceeding the character array size!

As we all are programmers, we have an intention that the user must not face any kind of problems with the software and its obvious that user is un-aware of the size of string we have prescribed!
Lets take an example here....

#include
int main() {
  char a[10];
  scanf("%s",a);
  printf("%s",a);
  return 0;
}
the size of array is 10 but users are unaware of it and they may enter a string passing beyond the size like in this case!

b

Hence there is a problem of memory out source and ultimately, the application collapse showing this error report!

a

and as we all know we never want our application to crash, so we can take some necessary measures to avoid this problem

so, if i replace my above code with the following code, it handles this exception and it ignores whatever is entered beyond the array size.. the two functions responsible for this are fgets() and fputs()

#include
int main() {
   char a[10];
   fgets(a,sizeof(a),stdin);
   fputs(a,stdout);
   return 0;
}
The output will be something like this, showing only the first 10 digits entered! In this way this problem is handled and the application doesn't crash!

anishrushi
anishrushi 路 Dec 13, 2013
Rank E2 - BEGINNER
For more questions and answers on c++ hit upon https://www.crazyengineers.com
rahulkhan
rahulkhan 路 Jan 23, 2014
Rank E2 - BEGINNER
vaibhav bhanawat
vaibhav bhanawat 路 Mar 4, 2014
Rank D3 - MASTER
hello friends what should i do to convert my c++ code into an software which can be installed into different computer...........i am unable to open the exe file.
..
Vishal Sharma
Vishal Sharma 路 Mar 5, 2014
Rank B3 - LEADER
vaibhav bhanawat
hello friends what should i do to convert my c++ code into an software which can be installed into different computer...........i am unable to open the exe file.
..
I think its the system architecture which is not allowing you to run the exe on other PC's. Make sure that the architecture of your system and the other system are same. If you don't want this kind of trouble, create two setups (separate for both x86 and x64). I was facing the same problem with my app. I had to follow this step.
kiran devaguptapu
kiran devaguptapu 路 Dec 6, 2014
Rank E2 - BEGINNER
thanks......very useful.
rahul69
rahul69 路 Jan 11, 2015
Rank B2 - LEADER
One good practice is to avoid hard-coding the array size(in case static array is necessary), for example:
instead of creating :
int array [50];
better way is :
#define max 50
.
.
int array [max];
so if we need to increase array size, in complex program, we can be sure no array is left, otherwise it will be a havoc.
sneha thorat
sneha thorat 路 Jan 17, 2015
Rank E1 - BEGINNER
thanks fr d useful post........
Kiran.laxman.hiwane
Kiran.laxman.hiwane 路 Feb 6, 2015
Rank D3 - MASTER
how to initialise and declare a funtion inside and outside the class
oumar
oumar 路 Feb 24, 2015
Rank E1 - BEGINNER
thanks guys for your contributions
Sirajul islam Shawon
Sirajul islam Shawon 路 Oct 21, 2015
Rank E2 - BEGINNER
Vishal0203
Small tips for programmers::

NOTE-> THESE TIPS ARE SUPPORTED IN DEV-C++ WHICH USES A GCC COMPILER. I SUGGEST EVERYONE TO USE DEV or VISUAL C++ BECAUSE IT WILL SURELY ENHANCE YOUR PROGRAMMING SKILLS AND INTELLIGENCE.

1) Most of us use clrscr( ); to clean up the output screen. Well, this function is not a standard function to be used in your program. Use system("cls"); which is included in stdlib.h this function is more preferable than clrscr();



2) While programming for C++, instead of
#include 


use

#include 

using namespace std;
as the programming environments have been developed and none of the people use the ancestor blue screen programming environment. Similarly, the header files ctype.h, assert.h etc can be directly writen as cctype, cassert (without .h extension)

3) In C++, using getch( ) to hold the output on screen is a bad practice. Which is not a standard function provided by ANSI C. Use
fflush (stdin);  // cstdio header

std::cin.get();  // iostream header

4) DO NOT USE void main( ) since it returns a garbage value to the main function, which makes it impossible to be called explicitly in some other program.
use int main( ) so that you know what value it returns.

5) And, at the end I'd like to tell something about presentation. Presentation of code is very important in all aspects. The code must be understandable not only by you but also to the other person. It doesn't matter to whom you are presenting the code. I've seen people (mostly my classmates) writing the code in very clumsy way, and when they ask me any doubt, I'm unable to understand their code, just because their way of presentation is too bad.
The same thing must be kept in mind while asking the doubts on the forums. Your codes must be understandable by the person who is seeing it, or else they loose interest in helping you out..

> Writing comments is a very good habit. Write a comment about the action of the statement in your code. It must be done for all the loops you're using. If you're able to determine where you are getting an error, write a comment indicating the error.

> Make your code more expanded and clear. Use the white spaces (space bar or tab keys ). They don't effect your program in anyway as the compiler skips all the white spaces used. But, it helps the user to understand the code easily.
ex:

int main() {printf("HELLO CEANS"); return 0;}
the compiler faces no problem in faces no problem in executing the above code. But is it presented well?? Well, everyone knows NO! We can instead write it in different way as

int main()
{
  printf ("HELLO CEANS");
  return 0;
}

The same things can be done while using the loop conditions or conditional statements

// make your code look clear...

for(i = 0 ; i < condition ; i++)  // spaces between the variables and operators
{                     // opening and closing braces are in same line
  .............        // loop statements are well organized
  .............
}



So that's it for now! Have a good day! CHEERS
Md shahin hawlada
Md shahin hawlada 路 Nov 30, 2015
Rank E2 - BEGINNER
Really useful that topic a lot's of thanks
Gnanam Ramalingam
Gnanam Ramalingam 路 Dec 24, 2015
Rank E1 - BEGINNER
hey dude its really useful keep updating
ankush0809
ankush0809 路 Dec 25, 2015
Rank D3 - MASTER
is it necessary to learn c++ or html for PHP or if i want to learn wordpress only then, can i learn direct wordpress ?
Vishal Sharma
Vishal Sharma 路 Dec 25, 2015
Rank B3 - LEADER
ankush0809
is it necessary to learn c++ or html for PHP or if i want to learn wordpress only then, can i learn direct wordpress ?
You eat the entire cake, not just the icing 馃槈
MANU BHARADWAJ
MANU BHARADWAJ 路 Jan 4, 2016
Rank D3 - MASTER
I am currently using turbo c++ compiler should I change to any other ?
Vishal Sharma
Vishal Sharma 路 Jan 4, 2016
Rank B3 - LEADER
manu bharadwaj
I am currently using turbo c++ compiler should I change to any other ?
Yeah, use dev c++ if yu are a starter or else use visual studio.
MANU BHARADWAJ
MANU BHARADWAJ 路 Jan 4, 2016
Rank D3 - MASTER
Vishal0203
Yeah, use dev c++ if yu are a starter or else use visual studio.
Thank u vishal where can I download dev c++
Vishal Sharma
Vishal Sharma 路 Jan 4, 2016
Rank B3 - LEADER
manu bharadwaj
Thank u vishal where can I download dev c++
Google it buddy.
Edward Kagimu
Edward Kagimu 路 Aug 29, 2016
Rank E1 - BEGINNER
dude pliz would u hel[p and elaborate to me how to doiwnload and install the gcc compilers on window seven operating sytem?
Vishal Sharma
Vishal Sharma 路 Aug 29, 2016
Rank B3 - LEADER
kagimu Edward
dude pliz would u hel[p and elaborate to me how to doiwnload and install the gcc compilers on window seven operating sytem?
You can download Dev C++, gcc compilers are packaged with it. Just add the bin path in your environment variables and you are good to go
Vishal Sharma
Vishal Sharma 路 Sep 23, 2016
Rank B3 - LEADER
Redfredg
This is very indepth and informative tips. I have been learning C++ for 5 months and I do not know if I understand if I know the language like, I am suppose to. How do you know if you are okay with C++? I am right now learning polymorphism and I Semi understand it right now.
The way I understood C++ and oops concepts was by writing all my data structure code in C++. Also, by comparing the methods of C with C++.
Vishal Sharma
Vishal Sharma 路 Sep 22, 2017
Rank B3 - LEADER
Betty Maria
Good tips but why did you use printf(); in C++?
printf ("HELLO CEANS");

I think you should use:
cout << "HELLO CEANS";
Agreed. I mentioned that somewhere in this long thread, as I was not able to update the post.

You must log-in or sign-up to reply to this post.

Click to Log-In or Sign-Up