Can someone tell me the different if there is any

I was messing around with toupper() because we just learned it in school and I wan't to change a whole word toupper so i look it up and got this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* toupper example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
  int i=0;
  char str[]="Test String.\n";
  char c;
  while (str[i])
  {
    c=str[i];
    putchar (toupper(c));
    i++;
  }
  return 0;
}


well I started messing around with it because I didn't understand putchar at first then figure that putchar is just like cout so I was messing around and made this program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cctype>
#include <stdio.h>

using namespace std;

int main( )
{
    char name[50];
    int i = 0;
    char c;

    cin >> name;

    while( name[i] )
    {
        c = name[i];


        cout << static_cast<char>( toupper( c ) );

        i++;
    }


    return( 0 );
}


I was just wondering are they the same or is there a big difference? Better to user putchar or cout ?

The difference is obviously that you are coding to a different interface with each program which requires the inclusion of different header files. I lean more heavily towards using the C++ std template classes. The iostream template classes are fairly complex in and of themselves so if you try to mix the usage within one program you can really create a lot of unnecessary confusion for yourself. You can overload operator>> and operator<< for user defined types which allows you to write code that can directly interact with the streams in a very convenient and intuitive way. The cstdio functionality exists for forward compatibility (C to C++) I think. Nothing will stop you from using the c libraries if you want. however if you are an object oriented programmer you'll want to fully utilize those new mechanisms that are provided by the C++ template libraries.

This is a really difficult question for beginners to C++ because you may not fully understand the history of C++. When I took my first college class I used the cstdio library because that is what I was taught to do and I didn't know any better. After writing code for many years now I would never go back to using those libraries unless I had no choice. For instance if I am assigned to a project to maintain existing code that uses those libraries then I have to maintain a consistent programming style while maintaining that code. However if I am writing code for a brand new OO C++ project I will want to use the STL to the greatest extent possible.
Last edited on
Thanks for the information... I think I will be going lean more towards C++... But since i want to lean towards C++ more then C does that mean i shouldn't use <cctype> or <cmath> and stuff like that or is that OK?
Well cctype and cmath are just C++ includes for the C headers "type.h" and "math.h" respectively. I don't know why the standard is removing the .h suffix, maybe it's to avoid confusion with user created header files. Anyway, yes, it's fine to use them if you need them. AFAIK there isn't a math header specifically for C++ and there's no std::math.
Last edited on
I don't know why the standard is removing the .h suffix, maybe it's to avoid confusion with user created header files

that might have to do with rewriting some classes as well, plans for future versions?...

Also good to have in your toolbox, if you have a toolbox.(at least until you know it off by heart):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void convert2Upper(string& str)
{
   for ( unsigned i=0; i<str.size(); i++)
      str[i]=toupper(str[i]);
}

// or to print as toupper

void print2Upper(string str)
{
   for (unsigned i=0; i<str.size(); i++)
      cout << toupper(str[i]);
}

// or compare toupper

bool compare2Upper(string str, string compare)
{
   for (unsigned i=0; i<str.size; i++)
   {
      if (toupper(str[i]) != toupper(compare[i]))
         return false;
   }
   return true;
}

Last edited on
I never use toupper/tolower and friends. If I need string manipulation I use Python or Perl.
So if you needed string manipulation in a C/++ project you'd embed Python and pass your parameters to a Python function?
No, I just never use much string manipulation in C/C++. Even so, I don't see why I would need to convert strings to uppercase.
One reason that you would need to convert strings to uppercase/lowercase one is that what if you ask the use to enter yes or no: but they enter Yes, yEs, or No, NO they are the same word right but in programming they are different so if you convert them down or up then you fix this problem.
Or just any other type of case-insensitive string comparison.
Most of the programs I write use command line arguments, or take no user input. They just do a task, and print some output. But most of the programs I write are unusable; I write them just to learn.
Topic archived. No new replies allowed.