#include<iostream.h>
#include<conio.h>
float si(int p,int r,int t)
{
return(float)(p*r*t)/100;  }
int main()
{

 int p,r,t,s;
 cout<<"enter the p,r,t";
 cin>>p>>r>>t;
 float s=si(p,r,t);
 cout<<"simple interest="<<s;

 }
 getch();
 }

iam new to c++ and i was going through UDF's when i pondered upon this problem of calculatin the simle interest ... pls help me n pls tell whats wrong with this program.
thank you.

Recommended Answers

All 2 Replies

line 1: should be <iostream> -- no .h extension. If what you are reading tells you to use iostream.h then stop reading it right now and read something else.

line 2: delete it. conio.h is non-standard and its use is discouraged in c++ programs.

line 5: put that } on a new line where its easy for you and anyone else reading your program can see it.

line 15: delete that line

line 16: replace getch() with cin.get().

After line 1 you will need to add this:

using std::cin;
using std::cout;

Except for deleting line 15, what Ancient Dragon wants you to do will make your code more portable. That's a good goal, but you have a real problem to fix first: integer overflow. p, r and t can't be very big before multiplying them together will overflow an int, and overflowing an int is a lot worse than all of the other stuff Ancient Dragon is worrying about.

You can work around integer overflow by not allowing it, and that means testing for overflow before it happens. Here is your code with one of those tests added:

#include <iostream>
#include <stdexcept>
#include <climits>

using namespace std;

namespace Daniweb
{
    int Product(int a, int b, int max=INT_MAX)
    {
        if (b > max / a) throw range_error("integer overflow");
        
        return a * b;
    }

    float si(int p, int r, int t, int max=INT_MAX)
    {
        return Product(Product(p, r, max), t, max) / 100.f;
    }
}

int main() 
try
{
    int p = 1000, 
        r = 1000, 
        t = 1000;

    cout << "max test = ";
    cout << fixed << Daniweb::si(p, r, t) << '\n';
    cout << "\t!passed!\nlimit test = ";
    cout << fixed << Daniweb::si(p, r, t, 1000000000) << '\n';
    cout << "\t!passed!\noverflow test = ";
    cout << fixed << Daniweb::si(p, r, t, 999999999) << '\n';
    cout << "\t!passed!\n";
}
catch (range_error& e)
{
    cerr << e.what() << '\n';
}
commented: Good point :) +36
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.