Please check-I'm a newbie

This is a discussion on Please check-I'm a newbie within the C++ Programming forums, part of the General Programming Boards category; Do I have the correct code? It compiles and runs but I'm new to this and I'm just making sure. ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    12

    Cool Please check-I'm a newbie

    Do I have the correct code?
    It compiles and runs but I'm new to this and I'm just making sure.

    Code:
    #include<iostream>
    #include<iomanip>
    #include<stdlib.h>
    using namespace std;
    
    // prototype goes here
    
        double calcint(double amount, double rate);
    
        int year;
    	double rate;
    	double amount;
        double annInt;
    int main()
    {
    	// declare variables
    	annInt = amount * rate * 1;
    		
    	// get user input
    	cout << "Enter loan amount: ";
        cin >> amount;
    	cout << "Enter loan rate: ";
        cin >> rate;
    
    	// call function for calculation
    	annInt = calcint(amount, rate);
    	cout << "This is your annual intrest rate: " << annInt << endl;
    	
    	// display results
    	system("pause");
    	
    	return 0;
    }
    
    double calcint(double amount, double rate)
    {
    	return amount * rate * 1;
    }

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    Code:
    annInt = amount * rate * 1;
    Code:
    return amount * rate * 1;
    the last multiplication is redundant.

    Code:
    // declare variables
    annInt = amount * rate * 1;
    This line doesn't do anything. None of your variables have been initialized. By doing this, annInt won't automatically update itself when you change the value of amount or rate.
    I see you labeled it "declared variables". I think you want initialize variables (give a value to the variables)
    The variables have been declared here:
    Code:
        int year;
    	double rate;
    	double amount;
        double annInt;
    You can work on your indenting as well.
    Last edited by h_howee; 03-08-2008 at 09:26 PM.

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,534
    I noticed that you are using both spaces and tabs in your code indentation, this is big no-no.

    Yes your code works, but here is a much simpler way to the identical thing:
    Code:
    int main()
    {
       int amount, rate;
       cout << "Enter loan amount: ";
       cin >> amount;
       cout << "Enter loan rate: ";
       cin >> rate;
       cout << "This is your annual intrest rate: " << (amount * rate) << endl;
       system("pause");
       return 0;
    }
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 12:52 AM
  4. Newbie Again
    By christianne in forum C Programming
    Replies: 14
    Last Post: 04-06-2006, 12:39 AM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21