Tips For C++ Programmers - Read & Learn

Discussion in 'Computer Science | IT | Networking' started by Vishal0203, Aug 28, 2012.

  1. 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.
  2. Vishal0203

    Vishal0203 Maniac

    If you show your approach we'll try to correct you.
    Direct codes are not given!
  3. 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
  4. Vishal0203

    Vishal0203 Maniac

    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
  5. sneha thorat

    sneha thorat Apprentice

    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.

  6. 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?
  7. Vishal0203

    Vishal0203 Maniac

    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!
    • Agree Agree x 1
  8. sneha thorat

    sneha thorat Apprentice

    k.i got it.........
  9. sneha thorat

    sneha thorat Apprentice

    you are right........m just saying that on the basis of c++ program.
  10. Vishal0203

    Vishal0203 Maniac

    Yeah! actually it doesn't make much difference...

    this is for cout!

    This is for printf()
  11. sneha thorat

    sneha thorat Apprentice

    yaah i understood.thanks for the extra knowledge.
  12. Vishal0203

    Vishal0203 Maniac


    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....

    int main() {
      char a[10];
      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()

    int main() {
       char a[10];
       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!

  13. anishrushi

    anishrushi Newbie

  14. rahulkhan

    rahulkhan Newbie

    #134 rahulkhan, Jan 23, 2014
    Last edited: Feb 3, 2014
  15. vaibhav bhanawat

    vaibhav bhanawat Apprentice

    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.
  16. Vishal0203

    Vishal0203 Maniac

    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.
  17. thanks......very useful.
  18. rahul69

    rahul69 Star

    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.
  19. sneha thorat

    sneha thorat Apprentice

    thanks fr d useful post........
  20. Kiran.laxman.hiwane

    Kiran.laxman.hiwane Apprentice

    how to initialise and declare a funtion inside and outside the class

Share This Page