Same code in c & c++ but different compiler.

Why does it causes different result....

Page 1 of 1

6 Replies - 716 Views - Last Post: 15 March 2008 - 08:38 AM Rate Topic: -----

#1 mayann126  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-March 08

Same code in c & c++ but different compiler.

Posted 14 March 2008 - 08:19 PM

Attached File  Coutput.bmp (636.03K)
Number of downloads: 59I used visual C++ and Turbo C compilersand I used the same code for both, I only converted the c++ to c but the output is different. Why does it happen? please help me, I really can't understand why..... Actually this is a conversion from java. It was successfully converted to C++ but there was an error converting it in C language. I used the code I converted in C++ so that it will be easier to convert it in C language. Then the problem occurred and I reviewed it so many times but still I canít get the desired result. THANKS A LOT..... :) :) :)

UNDESIRED RESULT in TurboC
Attached File  Coutput.bmp (636.03K)
Number of downloads: 59

RIGHT RESULT in VisualC++
Year &amount deposit

1year $1365
2years $1433.25
3years $1504.91
4years $1580.16
5years $1659.17
6years $1742.12
7years $1829.23
8years $1920.69
9years $2016.73
10years $2117.56
11years $2223.44
12years $2334.61




#include <iostream.h>
#include <math.h>

main()
{
double amount=0.00, n_rate=0.00;
double n_principal=0.00, n_convert_rate=0.00;
int n_years=0, year=0;

cout<<"Enter principal amount:=>$";
cin>>n_principal;
cout<<"Enter interest rate percent:=>% ";
cin>>n_rate;
cout<<"Enter number of years:=> ";
cin>>n_years;

n_convert_rate = (0.01 * n_rate);
cout<<"\n";
cout<<"Year \t Amount on deposit \n";

for (int line=1; line<=30; line+=1)
{
cout<<"-";
}
cout<<"\n";
for (year=1; year <=n_years; year++)
{
amount=(n_principal) * pow(1.0 + n_convert_rate, year);
cout<<""<< year <<"\t" <<"$" << amount<< "\n";
}
return 0;
}

This post has been edited by mayann126: 14 March 2008 - 09:37 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Same code in c & c++ but different compiler.

#2 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Same code in c & c++ but different compiler.

Posted 14 March 2008 - 08:26 PM

Post your code, with the expected output and the output that you receive. And press the Anti-Panic button in your mind. :)

This post has been edited by letthecolorsrumble: 14 March 2008 - 08:26 PM

Was This Post Helpful? 0
  • +
  • -

#3 mayann126  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-March 08

Re: Same code in c & c++ but different compiler.

Posted 14 March 2008 - 09:42 PM

thanks... Well I already posted it. Hope I'll be able to find answers soon because I really need it in our project.
I think that the Anti-Panic button in my mind is not working that good anymore... :) anyway thanks....
Was This Post Helpful? 0
  • +
  • -

#4 MorphiusFaydal  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,376
  • Joined: 12-May 05

Re: Same code in c & c++ but different compiler.

Posted 14 March 2008 - 10:56 PM

Okay, compiling it mingw-g++, I get the same output as posted, which I guess is correct. What's the C code look like? There's no problem with the C++ code, but can you post the C code you're trying to compile?

EDIT:
Remember, converting from C++ to C does not mean just trying to recompile the C++ code with a C compiler.

This post has been edited by MorphiusFaydal: 14 March 2008 - 11:10 PM

Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,140
  • Joined: 14-September 07

Re: Same code in c & c++ but different compiler.

Posted 14 March 2008 - 10:59 PM

Different compilers have different standards on various things like floating points, etc...
Was This Post Helpful? 0
  • +
  • -

#6 ap0c0lyps3  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 19-June 07

Re: Same code in c & c++ but different compiler.

Posted 15 March 2008 - 04:08 AM

its a good idea to indent your code as well. It not only makes it easier to read but easier to find problems as well
Was This Post Helpful? 0
  • +
  • -

#7 MorphiusFaydal  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,376
  • Joined: 12-May 05

Re: Same code in c & c++ but different compiler.

Posted 15 March 2008 - 08:38 AM

View Postap0c0lyps3, on 15 Mar, 2008 - 06:08 AM, said:

its a good idea to indent your code as well. It not only makes it easier to read but easier to find problems as well


I'm sure that's just the lack of code tags.

:code:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1