Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,291 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,086 people online right now. Registration is fast and FREE... Join Now!




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

 
Reply to this topicStart new topic

Same code in c & c++ but different compiler., Why does it causes different result....

mayann126
post 14 Mar, 2008 - 07:19 PM
Post #1


New D.I.C Head

*
Joined: 14 Mar, 2008
Posts: 4

Attached File  Coutput.bmp ( 636.03k ) Number of downloads: 18
I 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..... smile.gif smile.gif smile.gif

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


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 Mar, 2008 - 08:37 PM
User is offlineProfile CardPM

Go to the top of the page

letthecolorsrumble
post 14 Mar, 2008 - 07:26 PM
Post #2


Student of The Sun

Group Icon
Joined: 7 Nov, 2007
Posts: 550



Thanked 1 times
My Contributions


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 Mar, 2008 - 07:26 PM
User is offlineProfile CardPM

Go to the top of the page

mayann126
post 14 Mar, 2008 - 08:42 PM
Post #3


New D.I.C Head

*
Joined: 14 Mar, 2008
Posts: 4

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... smile.gif anyway thanks....
User is offlineProfile CardPM

Go to the top of the page

MorphiusFaydal
post 14 Mar, 2008 - 09:56 PM
Post #4


D.I.C Lover

Group Icon
Joined: 12 May, 2005
Posts: 1,086



Thanked 8 times

Expert In: Hardware, Networking

My Contributions


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 Mar, 2008 - 10:10 PM
User is offlineProfile CardPM

Go to the top of the page

KYA
post 14 Mar, 2008 - 09:59 PM
Post #5


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,205



Thanked 50 times

Dream Kudos: 1150
My Contributions


Different compilers have different standards on various things like floating points, etc...
User is offlineProfile CardPM

Go to the top of the page

ap0c0lyps3
post 15 Mar, 2008 - 03:08 AM
Post #6


D.I.C Head

Group Icon
Joined: 19 Jun, 2007
Posts: 79



Dream Kudos: 25
My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

MorphiusFaydal
post 15 Mar, 2008 - 07:38 AM
Post #7


D.I.C Lover

Group Icon
Joined: 12 May, 2005
Posts: 1,086



Thanked 8 times

Expert In: Hardware, Networking

My Contributions


QUOTE(ap0c0lyps3 @ 15 Mar, 2008 - 06: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


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

code.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 01:20AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month