3 Replies - 469 Views - Last Post: 20 October 2010 - 10:01 AM Rate Topic: -----

#1 Guest_Ahsan*


Reputation:

Program giving wrong output

Posted 20 October 2010 - 09:46 AM

Hello! I am c++ beginner.
I have made this program. it is a calculator which calculates invest money iwth interest after given years.
COmpiler shows no error, but it is giving me wrong output.
for example, when I put in 10 years, it gives me output of 6 years only.
please tell me where i went wrong.
#include <iostream>

using namespace std;
int main()
{
    float amount, interest, total1, total2;
    int year;
    cout << "Enter amount: " ;
    cin >> amount;
    cout << "Enter years: " ;
    cin >> year;
    cout << "Enter interest rate: " ;
    cin >> interest;
    
    for    (int j=0; j<=year; j++)
    {
        total1 = (amount/100)*interest;
        total2 = amount+ total1;
        cout << total2 << endl;
        ++j;
        
        amount = total2;
    }
    cout << "Money: " << total2 << endl;

return 0;
}



Thanks in advance!

Is This A Good Question/Topic? 0

Replies To: Program giving wrong output

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Program giving wrong output

Posted 20 October 2010 - 09:55 AM

++j; on line 20 should warrant your attention, why is it there, what is happening because of it?
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6032
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: Program giving wrong output

Posted 20 October 2010 - 09:56 AM

You're incrementing j a second time within the loop.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Ahsan*


Reputation:

Re: Program giving wrong output

Posted 20 October 2010 - 10:01 AM

oh thanks a lot all :D got the problem.

its working now :D
Was This Post Helpful? 0

Page 1 of 1