1. Become a Developer for CrazyEngineers - Apply NOW!

Tips For C++ Programmers - Read & Learn

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

  1. simplycoder

    simplycoder Ace

    Engineering Discipline:
    Electronics & Communications
    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

    Engineering Discipline:
    Computer Science

    If you show your approach we'll try to correct you.
    Direct codes are not given!
     
  3. Jeffrey Samuel

    Jeffrey Samuel Star

    Engineering Discipline:
    Electronics
    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

    Engineering Discipline:
    Computer Science
    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..
    Code:
    #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

    Engineering Discipline:
    IT
    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. simplycoder

    simplycoder Ace

    Engineering Discipline:
    Electronics & Communications

    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

    Engineering Discipline:
    Computer Science

    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!
     
  8. sneha thorat

    sneha thorat Apprentice

    Engineering Discipline:
    IT
    k.i got it.........
     
  9. sneha thorat

    sneha thorat Apprentice

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

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    Yeah! actually it doesn't make much difference...
    cout.

    this is for cout!

    pf.
    This is for printf()
     
  11. sneha thorat

    sneha thorat Apprentice

    Engineering Discipline:
    IT
    yaah i understood.thanks for the extra knowledge.
     
  12. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    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....

    Code:
    #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!

    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()

    Code:
    #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!

    .
     
  13. anishrushi

    anishrushi Newbie

    Engineering Discipline:
    Computer Science
  14. rahulkhan

    rahulkhan Newbie

    Engineering Discipline:
    Computer Science
    Last edited: Feb 3, 2014
  15. vaibhav bhanawat

    vaibhav bhanawat Apprentice

    Engineering Discipline:
    Computer Science
    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

    Engineering Discipline:
    Computer Science
    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.
     

Share This Page