0
\$\begingroup\$

I have written code exclusively in both C and C++. I see clear advantages in both and therefore have recently began using them together. I have also read, what I would consider, outrageous comments claiming that to code in C is outright dumb, and it is the language of past generations. So, in terms of maintenance, acceptance, common practice and efficiency; is it something professionals on large scale projects see/do?

Here's an example snippet: I obviously need to #include both <stdio.h> and <iostream>.

#define STRADD ", "
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include "Struct.h"
#include "Rates.h"
#include "Taxes.h"
using namespace std; 

And later I utilize functions like...

void printHeading(FILE * fp)
{
    fprintf(fp, "Employee            Pay        Reg Hrs     Gross       Fed        SSI        Net\n");
    fprintf(fp, "Name                Rate       Ovt Hrs     Pay         State      Defr       Pay\n");
    fprintf(fp, "==================================================================================\n");
    return;
}

and..

void getEmpData(EmpRecord &e)
{
    cout << "\n Enter the employee's first name: ";
    getline(cin, e.firstname);
    cout << " Enter the employee's last name: ";
    getline(cin, e.lastname);
    e.fullname = e.lastname + STRADD + e.firstname; //Fullname string creation
    cout << " Enter the employee's hours worked: ";
    cin >> e.hours;
    while(e.hours < 0)
    {
        cout << "  You did enter a valid amount of hours!\n";
        cout << "  Please try again: ";
        cin >> e.hours;
    }
        cout << " Enter the employee's payrate: ";
        cin >> e.rate;

    while(e.rate < MINWAGE)
    {
        cout << "  You did enter a valid hourly rate!\n";
        cout << "  Please try again: ";
        cin >> e.rate;
    }
    cout << " Enter any amount to be tax deferred: ";
    cin >> e.deferred;
    while(e.deferred < 0)
    {
        cout << "  You did enter a valid deferred amount!\n";
        cout << "  Please try again: ";
        cin >> e.deferred;

    }
    cin.ignore(100, '\n');
    return;
}

Thanks in advance!

\$\endgroup\$

closed as off-topic by Jamal Jun 4 '16 at 21:58

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

5
\$\begingroup\$

You can't write exclusively in both C and C++.

You can write

  • exclusively in C
  • exclusively in C++
  • or using a combination of the two different languages.

to code in C is outright dumb, and it is the language of past generations.

C may be an old language but it is still heavily used and has its place.
It is practically the only glue language that is universal so great for writing effecient modules for other languages or gluing languages together.

It is also great for low level coding where you want/need to get close to the hardware.

But there are downsides to writing in C (but saying it is dumb to do so is stretching it a bit). But you should be able to justify your choice of choosing C over an alternative.

So, in terms of maintenance, acceptance, common practice and efficiency; is it something professionals on large scale projects see/do?

Are there large code bases writing in C that need maintenance: Yes.

Is it common practice to use C: That is entirely dependent on what you are doing it is imposable to generalize.

Personally I write exclusively in C++.
There is nothing I can do in C I can't do in C++ so I don't write C anymore (In fact I have dropped it from my resume (especially since I don't want to write C)). The advantage of C++ is I can get code written to as low a level as C but I also have higher level constructs (though not as high as modern scripting languages).

Code Review:

I see no advantage of using frpintf(). In situations like this C++ std::ofstream is much more flexible,

printHeading(FILE * fp)
{
    fprintf(fp, "Employee            Pay        Reg Hrs     Gross       Fed        SSI        Net\n");

// I would always use C++ stream.
// It can be more than just a file.
printHeading(std::ostream& stream)
{
     stream <<  "Employee            Pay        Reg Hrs     Gross       Fed        SSI        Net\n";

Also if you are doing anything complex it is TYPE SAFE. *Unlike C code using fprintf(). This is one major area that C falls down in and is the cause of some of the major bugs in C code.

You check for an invalid number. But you are not checking for completely invalid input. What happens if somebody typed "Fred"

cin >> e.hours;
while(e.hours < 0)
{
    cout << "  You did enter a valid amount of hours!\n";
    cout << "  Please try again: ";
    cin >> e.hours;
}

You expect that there is never more than 100 bad characters in the input?

cin.ignore(100, '\n');

What happens if I accidentally paste in a paragraph of text.

And last but worst of all:

using namespace std; 

Never do this. It is OK if you are writting a ten line toy project. But once you get past anything more than a toy it causes more problems (in name clashes) than it is worth. The reason standard library is shortened to std:: is to make it easy and quick to type.

\$\endgroup\$
  • 1
    \$\begingroup\$ sorry about the confusion, I meant I have done both exclusively meaning separately. I was asking more if they are indeed used simultaneously. Being a newb I don't really know the full potential of either language and would prefer not to develop bad habits. For maintenance I was speaking to issues with other programmers maintaining dual-language code. I'd justify using C functions because they seem more direct/easy/dependable. I've heard of people changing c(in/out) to the C counterparts and making the difference in passing a benchmark test. Are these wives tales? \$\endgroup\$ – ChiefTwoPencils Jul 23 '12 at 23:22
  • \$\begingroup\$ -After edit: I understand on input validation, at this point I do what I'm told! With cin.ignore(100, '\n'), we're using Win32 Console Apps so it's actually only used to hold the console win open for screen shots. You're not the first person to recommend std::, so I read a book which refers to std:: as an older style and made it seem it was normal/usual to declare classes inside of a namespace, define an instance, using namespace ns; and refer to it as class.mem as opposed to ns::class.mem. I also asked a professor of mine who agreed. \$\endgroup\$ – ChiefTwoPencils Jul 23 '12 at 23:53
  • 1
    \$\begingroup\$ Well your professor is absolutely wrong. In real production code you will hardly ever see using namespace X;. Just read a couple of other reviews here. Sure in toy code you will see it. But anything more than a few hundred lines using multiple namespaces you will end up with nameclashes. The whole point of namespaces is to avoid nameclashes and by having using namesapce X you totally break something the language is trying to fix from C. \$\endgroup\$ – Martin York Jul 23 '12 at 23:58
  • 1
    \$\begingroup\$ You should probably read some modern C++ books: "Herb Sutters" Exceptional C++, "Scott Myers" Effective C++, "Alexandrescu" Modern C++ design \$\endgroup\$ – Martin York Jul 24 '12 at 0:06
  • \$\begingroup\$ Check here for generic information. Reading List for a copy of the C++ standard \$\endgroup\$ – Martin York Jul 24 '12 at 0:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.