4 Replies - 494 Views - Last Post: 01 July 2012 - 02:18 AM Rate Topic: -----

#1 osu1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 17-June 12

Pointers and Formula Errors

Posted 17 June 2012 - 07:15 PM

Hello , I am having trouble making a code. am supposed to make a program where I can enter a time and a deposit and get the total value over time with the formula Interest amount = (Amount Deposited * Annual %Interest Rate * Period of deposit in years)/100 and the interest is 10%.


This is what I have so far, the problem is that I was getting a pointer error then I received a syntax error under the parantheses after 10%.




#include <iostream>

int main()
{
       using namespace std;

       int deposit; 

       cout << "Enter deposit:" endl;
       cin >> deposit;

       int year;

       cout << " Enter years:' << endl;
       cin >> years;

       in total;

       total = ((deposit * 10%* years ) / 100) + deposit;

       cout << " Total is" << total  << endl;
       return0; 
}
       


Is This A Good Question/Topic? 0
  • +

Replies To: Pointers and Formula Errors

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Pointers and Formula Errors

Posted 17 June 2012 - 07:59 PM

You shouldn't have the percentage sign there, that's why. In C++ it is called the modulus operator.

Come to think of it you can have the % operator there but then the C++ parser will take the * operator as that of a pointer. Since your variable year isn't a pointer you will receive the error.

You also didn't declare a variable named years, lines 15 and 19 I believe this is a typo.

Also on line 22 place a space between return and 0.

This post has been edited by codeprada: 17 June 2012 - 08:03 PM

Was This Post Helpful? 2
  • +
  • -

#3 osu1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 17-June 12

Re: Pointers and Formula Errors

Posted 17 June 2012 - 08:09 PM

View Postcodeprada, on 17 June 2012 - 07:59 PM, said:

You shouldn't have the percentage sign there, that's why. In C++ it is called the modulus operator.

Come to think of it you can have the % operator there but then the C++ parser will take the * operator as that of a pointer. Since your variable year isn't a pointer you will receive the error.

You also didn't declare a variable named years, lines 15 and 19 I believe this is a typo.

Also on line 22 place a space between return and 0.


I thought I declared the integer years in line 12 as a variable? Is that not correct? Because the problem is that I dont know how to incorporate a formula with more than 1 variable in my code. Is there another way to do this? I have been looking online for hours and the book isn't helping. Thank you.
Was This Post Helpful? 0
  • +
  • -

#4 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: Pointers and Formula Errors

Posted 17 June 2012 - 10:09 PM

There are numerous errors in your code, to name a few:

You refer to a variable called years yet on line 12 you declare a variable called year. The compiler has no idea what years is as you have not declared a variable like that.

If you want to use a variable called years then line 12 must become:
int years = 0;



And as codeprada said, the modulus operator (%) does not refer to percentage but rather will divide two numbers and leave you with the remainder. I think this would be a more appropriate way to get a percentage but I am unsure of what formula you are trying to implement:

 total = (10 *deposit) * years   ;
	   total = total /100; 



Also on a number of lines namely 9 and 14, you do not print your output correctly.

On line 9:
cout << "Enter deposit:"  endl;



There should be a redirection operator (<<) between your string literal and endl like so:
cout << "Enter deposit:" << endl;



Whilst on line 14:

cout << " Enter years:' << endl;



You do not end your string literal. ' is for a charachter, " is for a string so it should be like this:

cout << " Enter years: " << endl;



Also you declare a variable called total by using the following:
in total;



There is no datatype called in so that will not work. You should be declaring it as a int.

Hope this helps
Was This Post Helpful? 1
  • +
  • -

#5 osu1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 17-June 12

Re: Pointers and Formula Errors

Posted 01 July 2012 - 02:18 AM

View Postv0rtex, on 17 June 2012 - 10:09 PM, said:

There are numerous errors in your code, to name a few:

You refer to a variable called years yet on line 12 you declare a variable called year. The compiler has no idea what years is as you have not declared a variable like that.

If you want to use a variable called years then line 12 must become:
int years = 0;



And as codeprada said, the modulus operator (%) does not refer to percentage but rather will divide two numbers and leave you with the remainder. I think this would be a more appropriate way to get a percentage but I am unsure of what formula you are trying to implement:

 total = (10 *deposit) * years   ;
	   total = total /100; 



Also on a number of lines namely 9 and 14, you do not print your output correctly.

On line 9:
cout << "Enter deposit:"  endl;



There should be a redirection operator (<<) between your string literal and endl like so:
cout << "Enter deposit:" << endl;



Whilst on line 14:

cout << " Enter years:' << endl;



You do not end your string literal. ' is for a charachter, " is for a string so it should be like this:

cout << " Enter years: " << endl;



Also you declare a variable called total by using the following:
in total;



There is no datatype called in so that will not work. You should be declaring it as a int.

Hope this helps



Thank you very much and sorry for the late reply. Your reply did help clear some things up. It was my first time programming so I had a lot of trouble.

View Postcodeprada, on 17 June 2012 - 07:59 PM, said:

You shouldn't have the percentage sign there, that's why. In C++ it is called the modulus operator.

Come to think of it you can have the % operator there but then the C++ parser will take the * operator as that of a pointer. Since your variable year isn't a pointer you will receive the error.

You also didn't declare a variable named years, lines 15 and 19 I believe this is a typo.

Also on line 22 place a space between return and 0.


Thank you for your input Prada!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1