i have many questions to ask with regards to C++. In our programming class we do not use the "#include<iostream.h>" or something like that and MANY MORE! :D it is a lot different from ours anywhere i look. :D here is an example of our program. :D

#include<stdio.h>
#include<conio.h>
int x;
float y,z;
int sum;
float prod,ave;

main()
{
      printf("Enter three integer values:\n");
      scanf("%d,%f,%f",&x,&y,&z);
      sum=(x+y+z)*(x+y+z);
      prod=(float)(x*y*z)*(x*y*z)*(x*y*z);
      ave=(float)(x+y)/2;
      printf("the square of the sum is %d\n",sum);
      printf("the product of the cube is %4.2f\n",prod);
      printf("the average of the 1st and 2nd is %4.2f\n",ave);
      getch();
      }

could someone please explain to me why is it different? :D and could someone teach me or where i can learn? please? :D thank you! :D

This is not C++, it's C

i see. but it's weird though, our professor is teaching us C but we are using C++. :D but what's the difference between them? :D is C++ much more convenient than C?

His teaching you C but you are using C++? That doesn't make any sense, C++ is not a compiler. C++ is a more advanced code and language than C, a modified version if you will, hence the ++

If your instructor/prof told you this was C++, they lied.

This is C (with some errors). C code will work/compile on a C++ compiler because of how the languages are related to each other, but it's not technically C++ because of how it's written (the headers used, and no namespaces).

To make this actual C++ code it would have to be:

#include<cstdio>     //different header
using namespace std; //namespace
int x;
float y,z;
int sum;
float prod,ave;

int main()           //format of function head
{
  printf("Enter three integer values:\n");
  scanf("%d,%f,%f",&x,&y,&z);
  sum=(x+y+z)*(x+y+z);
  prod=(float)(x*y*z)*(x*y*z)*(x*y*z);
  ave=(float)(x+y)/2;
  printf("the square of the sum is %d\n",sum);
  printf("the product of the cube is %4.2f\n",prod);
  printf("the average of the 1st and 2nd is %4.2f\n",ave);
  return 0;
}

Notice the header and namespace changes... (and the lack of <conio.h>)

An actual C++ program would be more like this though (translated literally even though some of the equations don't make sense):

#include <iostream>

using std::cout;
using std::cin;
usint std::endl;

int main() {
  int x=0, y=0, z=0;
  int sum = 0;
  float prod = 0.0, ave = 0.0;

  cout << "Enter three integer values:\n");
  cin >> x >> y >> z;

  sum = (x+y+z) * (x+y+z);
  prod = (float)( (x+y+z) * (x+y+z) * (x+y+z) );
  ave = (x+y)/2.0;

  cout << "The square of the sum is " << sum << endl;
  cout << "The product of the cube is " << prod << endl;
  cout << "The average of the 1st and 2nd is " << ave << endl;

  cin.ignore();
  cin.get()
  return 0;
}

Note the different header, functions, operators, and objects.

If your instructor/prof told you this was C++, they lied.

This is C (with some errors). C code will work/compile on a C++ compiler because of how the languages are related to each other, but it's not technically C++ because of how it's written (the headers used, and no namespaces).

To make this correct C++ it would have to be:

#include<cstdio>
using namespace std;
int x;
float y,z;
int sum;
float prod,ave;

int main()
{
  printf("Enter three integer values:\n");
  scanf("%d,%f,%f",&x,&y,&z);
  sum=(x+y+z)*(x+y+z);
  prod=(float)(x*y*z)*(x*y*z)*(x*y*z);
  ave=(float)(x+y)/2;
  printf("the square of the sum is %d\n",sum);
  printf("the product of the cube is %4.2f\n",prod);
  printf("the average of the 1st and 2nd is %4.2f\n",ave);
  return 0;
}

Notice the header and namespace changes... (and the lack of <conio.h>)

This is still not C++, you've corrected his errors but have not changed the language. For one all printf will become cout and scanf wil be cin

His teaching you C but you are using C++? That doesn't make any sense, C++ is not a compiler. C++ is a more advanced code and language than C, a modified version if you will, hence the ++

YES! that really doesn't make any sense and also even what he is teaching us, so i seldom listen to his lectures. :D

Like Fbody said, you can code C in a C++ compiler, but that's about it

so since its C, i should post my questions in C forum? THANK YOU for answering my unanswered questions. i really appreciate it! :D

C is valid C++. In simplest terms, C++ adds features to the C language. If you choose to write a C++ program using C functions/constructs, that is still legal C++. What really makes the difference is the headers that you use.

For one all printf will become cout and scanf wil be cin

Because C is valid C++, this type of stuff is trivial, it's not required to make the code C++. It's an advisable change to make it easier to tell that it's C++, but not absolutely necessary.

The <stdio.h> header is a C header. However, if you replace that header with the <cstdio> header and add a resolution for the std namespace, suddenly that code has become C++ code.

The OP's code is C code because of the style and components used. Because it's C code, a C++ compiler can compile it. I think you're missing the technical difference there.

so since its C, i should post my questions in C forum? THANK YOU for answering my unanswered questions. i really appreciate it! :D

You can however post it in the C++ section if you want your C code to be converted to C++

C is valid C++. In simplest terms, C++ adds features to the C language. If you choose to write a C++ program using C functions/constructs, that is still legal C++. What really makes the difference is the headers that you use.

The <stdio.h> header is a C header. However, if you replace that header with the <cstdio> header and add a resolution for the std namespace, suddenly that code has become C++ code.

I beg to differ. Yes the headers in C and C++ do vary accordingly but C code is still C code, and cannot be C++. I understand what you mean given the similarities between them, as C++ is derived from C, but that code presented in the beginning is C, unless the necessary changes are made as I have mentioned earlier like cout and cin and what have you

I don't disagree that the original code is C. But try compiling this version as C:

#include<cstdio>     //different header
using namespace std; //namespace
int x;
float y,z;
int sum;
float prod,ave;

int main()           //format of function head
{
  printf("Enter three integer values:\n");
  scanf("%d,%f,%f",&x,&y,&z);
  sum=(x+y+z)*(x+y+z);
  prod=(float)(x*y*z)*(x*y*z)*(x*y*z);
  ave=(float)(x+y)/2;
  printf("the square of the sum is %d\n",sum);
  printf("the product of the cube is %4.2f\n",prod);
  printf("the average of the 1st and 2nd is %4.2f\n",ave);
  return 0;
}

It shouldn't work. That's because it's C++. The header used and the addition of the namespace make it C++ code. A C compiler won't recognize the <cstdio> header or the std namespace.

Changing printf() to cout << and scanf() to cin >> is advisable, but not necessary because printf() and scanf() are both C and C++. Whether it's C or C++ is determined by whether or not they are declared within the std namespace. Changing the header from <stdio.h> to <cstdio> moves the functions from the global scope to the std namespace. You must have a C++ compiler to access the std namespace. Thus, they are C++ because of the header change and the namespace.

Just because the code looks like C, that doesn't mean that it actually is.

Fair enough

Oh! It is a c code that you have post. In c++ we use <iostram.h> but not in c.

There are many difference between c and c++.
The main difference is C++ is object oriented programming language but c is not.
C++ reduces the length of code by using object. So it is easy to handle.

In c++ we use <iostram.h> but not in c.

You might, but since many modern compilers fail to accept <iostream.h> at all, a lot of us prefer the standard <iostream>.

C++ reduces the length of code by using object.

Reduced code size isn't one of the advantages of OOP in C++. In fact, due to the boilerplate, you'll find that the code size increases noticeably over a procedural design.

Yea Narue,
You are right. Modern compiler support <iostream>.

But Narue....

You might, but since many modern compilers fail to accept <iostream.h> at all, a lot of us prefer the standard <iostream>.

Is Dev C++ a modern compiler?
Some has said me that it is very old one.
But it supports <iostream>.
Thank you.

Is Dev C++ a modern compiler?
Some has said me that it is very old one.
But it supports <iostream>.

The default compiler for Dev-C++ is a relatively old version of gcc. However, it's new enough to support C++98, which is when <iostream> was introduced.

OK Narue.
Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.