Few Tips for C++/C
Here are a few tips that i feel that may be useful for new programmers in C++.....
1.Opening a file at a location :
This is a very big shortfall that I felt while studying in +2.Opening a file at other location other than at the current folder was "out of syllabus" in +2.
The problem can be solved by using the full pathname while opening the file using "//" instead of "/" which is usually used in pathnames.
i.open("D:\\file.cpp");
2.Reading a substring into another string
The usual mistake done while doing this type of code is to forget to end the new string with a null character('\0') which marks the end of a string.
3.A more efficient and "beautiful" switch statement
While using switch case statements,instead of using a separate variable for user choice,the getch() function from conio.h may be used,thus saving a memory location as well as a press of return key while execution of the code as the switch gets executed the moment the choice is pressed.The drawbacks in using this method is that no multi -character cases can be defined.(and the choice will be read as a character,which is not a drawback).Here is a demo..
#include<ctype.h>
#include<iostream.h>
#include<conio.h>
main()
{
.
.
.
.
switch(tolower(getch())
{
case 'a':cout<<"\n The choice is a ";
break;
1.Opening a file at a location :
This is a very big shortfall that I felt while studying in +2.Opening a file at other location other than at the current folder was "out of syllabus" in +2.
The problem can be solved by using the full pathname while opening the file using "//" instead of "/" which is usually used in pathnames.
i.open("D:\\file.cpp");
2.Reading a substring into another string
The usual mistake done while doing this type of code is to forget to end the new string with a null character('\0') which marks the end of a string.
3.A more efficient and "beautiful" switch statement
While using switch case statements,instead of using a separate variable for user choice,the getch() function from conio.h may be used,thus saving a memory location as well as a press of return key while execution of the code as the switch gets executed the moment the choice is pressed.The drawbacks in using this method is that no multi -character cases can be defined.(and the choice will be read as a character,which is not a drawback).Here is a demo..
#include<ctype.h>
#include<iostream.h>
#include<conio.h>
main()
{
.
.
.
.
switch(tolower(getch())
{
case 'a':cout<<"\n The choice is a ";
break;
case 'b':cout<<"\n The choice is b ";
break;
case 'c':cout<<"\n The choice is c ";
break;
}
}
Thats all is what I have now.......Hope this post will grow in due time..........