-
#-Link-Snipped-# That is a really useful post. I have pinned it. Keep writing useful articles and posts as tutorials for CEans.
Are you sure? This action cannot be undone.
-
#-Link-Snipped-#
Thanks a lot! 😀 I'll update this post if I get along some new things to make the program efficient!
Are you sure? This action cannot be undone.
-
Thank You. That was really useful.😀
Are you sure? This action cannot be undone.
-
#-Link-Snipped-#
good to know that! 😀
Are you sure? This action cannot be undone.
-
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).... 😀
Are you sure? This action cannot be undone.
-
#-Link-Snipped-#
Yeah! I've seen such people.. 😨
Are you sure? This action cannot be undone.
-
good knowledge given by u.thanx
Are you sure? This action cannot be undone.
-
#-Link-Snipped-#
thanks .. 😀
Are you sure? This action cannot be undone.
-
i too had worked with c++!! but i didn't knew most of it ..true knowledge ..
Are you sure? This action cannot be undone.
-
#-Link-Snipped-#
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!! 😀
Are you sure? This action cannot be undone.
-
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();
Are you sure? This action cannot be undone.
-
hey since when r u working in c++?
Are you sure? This action cannot be undone.
-
gaurav91
hey since when r u working in c++?
I'm a second year student, so, Just 3 months.. 😀
Are you sure? This action cannot be undone.
-
wow awesome buddy! so where r u studying c++ from i mean which books do u refer?
Are you sure? This action cannot be undone.
-
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!
Are you sure? This action cannot be undone.
-
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 😀
Are you sure? This action cannot be undone.
-
😀 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!
Are you sure? This action cannot be undone.
-
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<<k;
for swaping values of variables use
a=a^b;
b=a^b;
a=b^a;
I think that's enough 😀
sorry for weak english 😖
Are you sure? This action cannot be undone.
-
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.. 😀
Are you sure? This action cannot be undone.
-
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 😉
Are you sure? This action cannot be undone.
-
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.
#-Link-Snipped-#
Are you sure? This action cannot be undone.
-
Thanks for share given good knowledge.
Are you sure? This action cannot be undone.
-
vishal really u r having vishal knowledge.good
Are you sure? This action cannot be undone.
-
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<<k;
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.
Are you sure? This action cannot be undone.
-
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! 😛
Are you sure? This action cannot be undone.
-
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
Are you sure? This action cannot be undone.
-
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
Are you sure? This action cannot be undone.
-
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! 😀
Are you sure? This action cannot be undone.
-
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
Are you sure? This action cannot be undone.
-
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
Are you sure? This action cannot be undone.
-
Like this 😀
Are you sure? This action cannot be undone.
-
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 <iostream.h>
#include<stdio.h>
#include<stdlib.h>
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 "<<ch<<"is : "<<i; [line A]
cout<<"Press Enter to Exit";
getchar(); [line B]
exit(0);
}
}
return 0;
}
Are you sure? This action cannot be undone.
-
ohhh !!!😒
i dont think the first three rules which you described is also known to our professors!!😁
anyways thanks for the info!! 😀
Are you sure? This action cannot be undone.
-
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 😁 😉
Are you sure? This action cannot be undone.
-
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 😀
Are you sure? This action cannot be undone.
-
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 <iostream.h>
#include<stdio.h>
#include<stdlib.h>
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 "<<ch<<"is : "<<i; [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??????
Are you sure? This action cannot be undone.
-
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 <iostream.h>
#include<stdio.h>
#include<stdlib.h>
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 "<<ch<<"is : "<<i; [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.
Are you sure? This action cannot be undone.
-
#-Link-Snipped-#
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.
Are you sure? This action cannot be undone.
-
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 <iostream.h>
#include<stdio.h>
#include<stdlib.h>
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 "<<ch<<"is : "<<i<<endl;
cout<<"Press Enter to Exit"<<endl;
fflush(stdin);
getchar();
exit(0);
}
}
return 0;
}
[code\n]
Are you sure? This action cannot be undone.
-
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 <iostream.h>
#include<stdio.h>
#include<stdlib.h>
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 "<<ch<<"is : "<<i<<endl;
cout<<"Press Enter to Exit"<<endl;
fflush(stdin);
getchar();
exit(0);
}
}
return 0;
}
[code\n]
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).
Are you sure? This action cannot be undone.
-
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 <iostream.h>
#include<stdio.h>
#include<stdlib.h>
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 "<<ch<<"is : "<<i<<endl;
cout<<"Press Enter to Exit"<<endl;
fflush(stdin);
getchar();
exit(0);
}
}
return 0;
}
[code\n]
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! 😀
Are you sure? This action cannot be undone.
-
I mean getchar() is to get character from the keyboard. (in your program, it holds output on screen until you press a key)
Are you sure? This action cannot be undone.
-
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 😉
Are you sure? This action cannot be undone.
-
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 <iostream.h>
#include<stdio.h>
#include<stdlib.h>
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 "<<ch<<"is : "<<i;
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]
Are you sure? This action cannot be undone.
-
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()
Are you sure? This action cannot be undone.
-
case 2: endl added to line B
#include <iostream.h>
#include<stdio.h>
#include<stdlib.h>
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 "<<ch<<"is : "<<i;
cout<<"Press Enter to Exit"<<endl; [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 .
Are you sure? This action cannot be undone.
-
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?
Are you sure? This action cannot be undone.
-
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??
Are you sure? This action cannot be undone.
-
#-Link-Snipped-# #-Link-Snipped-# : see the two cases
Are you sure? This action cannot be undone.
-
grsalvi
case 2: endl added to line B
#include <iostream.h>
#include<stdio.h>
#include<stdlib.h>
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 "<<ch<<"is : "<<i;
cout<<"Press Enter to Exit"<<endl; [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<<endl is equivalent to std::cout<<"\n"<<std::flush;
Just my 2 cents.
Are you sure? This action cannot be undone.
-
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 .
Are you sure? This action cannot be undone.
-
grsalvi
#-Link-Snipped-# #-Link-Snipped-# : 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!
Are you sure? This action cannot be undone.
-
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..
Are you sure? This action cannot be undone.
-
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++.😡
Are you sure? This action cannot be undone.
-
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++ 😖
Are you sure? This action cannot be undone.
-
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.
Are you sure? This action cannot be undone.
-
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.
Are you sure? This action cannot be undone.
-
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).
Are you sure? This action cannot be undone.
-
Thanks pal the namespace tag can you please send some links to understand it more
Are you sure? This action cannot be undone.
-
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 😀
#-Link-Snipped-#
Are you sure? This action cannot be undone.
-
first know about basic of c++ you can read the diploma book it will help you more
Are you sure? This action cannot be undone.
-
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
Are you sure? This action cannot be undone.
-
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 😀
#-Link-Snipped-#
thanks again for the link love it
Are you sure? This action cannot be undone.
-
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. 😉
Are you sure? This action cannot be undone.
-
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
Are you sure? This action cannot be undone.
-
thanks a lot ............. this would really help me as i am in the initial stage of engineering😁
Are you sure? This action cannot be undone.
-
shanmugapriya kr
thanks a lot ............. this would really help me as i am in the initial stage of engineering😁
I hope it does!
Are you sure? This action cannot be undone.
-
thanks a lot vishal 😛
Vishal0203
I hope it does!
Are you sure? This action cannot be undone.
-
UPDATE::
Use of getline() to get the input of a string or a line (common for both).
the getline function present in <string> 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 <iostream>
#include <string>
using namespace std;
int main()
{
string a;
cout<<"Enter the full name: "<<endl;
getline(cin , a , '\n'); /* delimiter set as \n */
cout<<endl
<<"Thank You! Name registered as: "
<<a<<endl;
cin.sync();
cin.get();
return 0;
}
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!
Are you sure? This action cannot be undone.
-
Vishal0203
UPDATE::
Use of getline() to get the input of a string or a line (common for both).
the getline function present in <string> 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 <iostream>
#include <string>
using namespace std;
int main()
{
string a;
cout<<"Enter the full name: "<<endl;
getline(cin , a , '\n'); /* delimiter set as \n */
cout<<endl
<<"Thank You! Name registered as: "
<<a<<endl;
cin.sync();
cin.get();
return 0;
}
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 ??
Are you sure? This action cannot be undone.
-
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.
Are you sure? This action cannot be undone.
-
grsalvi
The Function cin.getline() , does the same. Right ??
there is a difference!
cin.getline() belongs to <iostream> whereas getline() belongs to <string>
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).
Are you sure? This action cannot be undone.
-
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!! #-Link-Snipped-#
might know the answer for this!
I've tagged him here.. hope he helps you 😀
Are you sure? This action cannot be undone.
-
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 ??
Are you sure? This action cannot be undone.
-
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.
Are you sure? This action cannot be undone.
-
Check out following code.
Case 1:
#include<fstream.h>
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<fstream>
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.
Are you sure? This action cannot be undone.
-
#-Link-Snipped-#
which programming environment do you use??
Are you sure? This action cannot be undone.
-
Vishal0203
#-Link-Snipped-#
which programming environment do you use??
IDE is visual c++ 6.0 .
Are you sure? This action cannot be undone.
-
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!
Are you sure? This action cannot be undone.
-
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 <fstream.h> file.
Anyways issue resolved.
Are you sure? This action cannot be undone.
-
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 <fstream.h> 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
Are you sure? This action cannot be undone.
-
#-Link-Snipped-#
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...
Are you sure? This action cannot be undone.
-
#-Link-Snipped-#
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! 😀
Are you sure? This action cannot be undone.
-
#-Link-Snipped-#
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
Are you sure? This action cannot be undone.
-
this is very useful post for us.keep posting these kind of posts which helps us in programming ic various languages.
Are you sure? This action cannot be undone.
-
safiajen0055
#-Link-Snipped-#
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
Are you sure? This action cannot be undone.
-
safiajen0055
#-Link-Snipped-#
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.
Are you sure? This action cannot be undone.
-
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 ..
😀 😀
Are you sure? This action cannot be undone.
-
Hello... You give me best knowledge about system("cls");. Before reading your sharing, always i was use clrscr();.
Are you sure? This action cannot be undone.
-
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");
Are you sure? This action cannot be undone.
-
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 <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
this is very very useful foe us keep it going for better growth....
Are you sure? This action cannot be undone.
-
hey can you suggest me some c++ projects for high school!!!
Are you sure? This action cannot be undone.
-
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
Are you sure? This action cannot be undone.
-
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"???
Are you sure? This action cannot be undone.
-
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
#-Link-Snipped-#
Please explore the site as you will be able to find lot of god things here
Are you sure? This action cannot be undone.
-
Thanks a lot for sharing this awesome information 😉
Are you sure? This action cannot be undone.
-
It's so nice..
Thanks very much for posting this..
Are you sure? This action cannot be undone.
-
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 #-Link-Snipped-#
Feel Free to ask doubts 😀
Are you sure? This action cannot be undone.
-
Its a valuable information and useful to beginners...Thanks for that information..
Are you sure? This action cannot be undone.
-
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.
Are you sure? This action cannot be undone.
-
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)
Are you sure? This action cannot be undone.
-
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.
Are you sure? This action cannot be undone.
-
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 <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
Nice Information 😀
Thanks for sharing this Information With us 😀
Are you sure? This action cannot be undone.
-
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 😀
Are you sure? This action cannot be undone.
-
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 <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
Nice post.! It is so useful..!!
Are you sure? This action cannot be undone.
-
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
Are you sure? This action cannot be undone.
-
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 <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
Thank you Vishal sir.. In future further programming i will definitely follow your instruction...Hattsss of your knowledge sir...
with regards,
Abhi
Are you sure? This action cannot be undone.
-
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!
Are you sure? This action cannot be undone.
-
what is the meaning of the line 'using namespace std'?
what are namespaces?
Are you sure? This action cannot be undone.
-
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.
Are you sure? This action cannot be undone.
-
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.
Are you sure? This action cannot be undone.
-
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 ?
Are you sure? This action cannot be undone.
-
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?
Are you sure? This action cannot be undone.
-
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!
Are you sure? This action cannot be undone.
-
I think this the c++ coding
Are you sure? This action cannot be undone.
-
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.
Are you sure? This action cannot be undone.
-
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 #-Link-Snipped-# click there are the brilliant tutorial
Are you sure? This action cannot be undone.
-
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 #-Link-Snipped-# 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.
Are you sure? This action cannot be undone.
-
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
//..
}
Are you sure? This action cannot be undone.
-
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.
Are you sure? This action cannot be undone.
-
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!
Are you sure? This action cannot be undone.
-
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.
Are you sure? This action cannot be undone.
-
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
Are you sure? This action cannot be undone.
-
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.
Are you sure? This action cannot be undone.
-
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 #-Link-Snipped-# has written.
Am I missing anything here?
Are you sure? This action cannot be undone.
-
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 #-Link-Snipped-# 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!
Are you sure? This action cannot be undone.
-
k.i got it.........
Are you sure? This action cannot be undone.
-
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 #-Link-Snipped-# has written.
Am I missing anything here?
you are right........m just saying that on the basis of c++ program.
Are you sure? This action cannot be undone.
-
sneha thorat
you are right........m just saying that on the basis of c++ program.
Yeah! actually it doesn't make much difference...
this is for cout!
This is for printf()
Are you sure? This action cannot be undone.
-
Vishal0203
Yeah! actually it doesn't make much difference...
this is for cout!
This is for printf()
yaah i understood.thanks for the extra knowledge.
Are you sure? This action cannot be undone.
-
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<stdio.h>
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!
Hence there is a problem of memory out source and ultimately, the application collapse showing this error report!
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<stdio.h>
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!
Are you sure? This action cannot be undone.
-
For more questions and answers on c++ hit upon <a href="https://www.crazyengineers.com">CrazyEngineers</a>
Are you sure? This action cannot be undone.
-
Nothing in this link ( c++ ) #-Link-Snipped-# <a href="https://www.crazyengineers.com">CrazyEngineers</a> #-Link-Snipped-#
Are you sure? This action cannot be undone.
-
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.
..
Are you sure? This action cannot be undone.
-
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.
Are you sure? This action cannot be undone.
-
thanks......very useful.
Are you sure? This action cannot be undone.
-
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.
Are you sure? This action cannot be undone.
-
thanks fr d useful post........
Are you sure? This action cannot be undone.
-
how to initialise and declare a funtion inside and outside the class
Are you sure? This action cannot be undone.
-
oumar
Member •
Feb 24, 2015
thanks guys for your contributions
Are you sure? This action cannot be undone.
-
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 <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
Are you sure? This action cannot be undone.
-
Really useful that topic a lot's of thanks
Are you sure? This action cannot be undone.
-
hey dude its really useful keep updating
Are you sure? This action cannot be undone.
-
is it necessary to learn c++ or html for PHP or if i want to learn wordpress only then, can i learn direct wordpress ?
Are you sure? This action cannot be undone.
-
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 😉
Are you sure? This action cannot be undone.
-
I am currently using turbo c++ compiler should I change to any other ?
Are you sure? This action cannot be undone.
-
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.
Are you sure? This action cannot be undone.
-
Vishal0203
Yeah, use dev c++ if yu are a starter or else use visual studio.
Thank u vishal where can I download dev c++
Are you sure? This action cannot be undone.
-
manu bharadwaj
Thank u vishal where can I download dev c++
Google it buddy.
Are you sure? This action cannot be undone.
-
dude pliz would u hel[p and elaborate to me how to doiwnload and install the gcc compilers on window seven operating sytem?
Are you sure? This action cannot be undone.
-
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
Are you sure? This action cannot be undone.
-
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++.
Are you sure? This action cannot be undone.
-
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.
Are you sure? This action cannot be undone.