8 Replies - 7228 Views - Last Post: 23 July 2012 - 03:11 PM Rate Topic: -----

#1 solano   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-July 12

Percentage calculator error

Posted 23 July 2012 - 11:33 AM

I just started learning C++ last week, and after reading some of the C++ for dummies and writing the fahrenheit to celsius program, I thought I'd try to make something different.

The purpose of the program is to first enter a value, then enter another value and at the end the program will display the second value as a percentage of the first value. So basically a percentage calculator.

Here is the code:

#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main(int nNumberofArgs, char* pszArgs[])

{
// Enter original value and define this variable as "original"
int original;
cout <<"Please enter original value:";
cin >> original;

// Enter value of change
int change;
cout <<"Please enter change:";
cin >> change;

// Calculate change fraction
int fraction;
fraction = change / original;

// Define constant
int constant;
constant = 100;


// Calculate percentage
int percentage;
percentage = fraction * constant;

// Display percentage change
cout <<"Percentage change is:";
cout << percentage << 
cout <<"%";
cout << endl;



// Wait for user 
system ("PAUSE");
return 0;

}



Do I need to use a different type of variable? Because the output is "00x4433c4%".

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Percentage calculator error

#2 alias120   User is offline

  • The Sum over All Paths
  • member icon

Reputation: 125
  • View blog
  • Posts: 706
  • Joined: 02-March 09

Re: Percentage calculator error

Posted 23 July 2012 - 11:40 AM

I would suggest reading up on different Data Types. How do you define an integer? Are integers ever composed of non-whole numbers? Will an integer give you the output you seek?

http://www.cplusplus...rial/variables/

This post has been edited by alias120: 23 July 2012 - 12:03 PM

Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Percentage calculator error

Posted 23 July 2012 - 11:41 AM

What are you trying to print on this line?
cout << percentage << 


What is the purpose of the second insertion operator<<?

Also beware of using integer math when doing division. Remember integers have no fractions and any fractional part is truncated. Therefore something like 1/2 would evaluate to zero.



Jim
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Percentage calculator error

Posted 23 July 2012 - 11:46 AM

Yes. Good advice reading about data types.

To fix the hexadecimal number being printed, though, you need to change line 35 to cout << percentage;.

The trailing "<<" is taking the address of cout on the next line and printing it out.
Was This Post Helpful? 0
  • +
  • -

#5 solano   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-July 12

Re: Percentage calculator error

Posted 23 July 2012 - 12:24 PM

Thank you for quick replies. I will read up on different data types and whole numbers. Btw it's not a serious project, I'm only doing it to learn and that is exactly what you guys have helped me do.
Was This Post Helpful? 0
  • +
  • -

#6 solano   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-July 12

Re: Percentage calculator error

Posted 23 July 2012 - 02:52 PM

View Postjimblumberg, on 23 July 2012 - 11:41 AM, said:

What are you trying to print on this line?
cout << percentage << 


What is the purpose of the second insertion operator<<?

Also beware of using integer math when doing division. Remember integers have no fractions and any fractional part is truncated. Therefore something like 1/2 would evaluate to zero.



Jim


I want it to display what value is stored in the integer "percentage", which I was hoping would be whatever is calculated from the formula earlier on in the code..
Was This Post Helpful? 0
  • +
  • -

#7 solano   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-July 12

Re: Percentage calculator error

Posted 23 July 2012 - 03:01 PM

btw I removed the << on line 35 and added the ;, as well as changing all the variables from int to double, and now it works! My first working program wuhu! :clap:
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Percentage calculator error

Posted 23 July 2012 - 03:03 PM

Congratulations!!!
Was This Post Helpful? 0
  • +
  • -

#9 solano   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-July 12

Re: Percentage calculator error

Posted 23 July 2012 - 03:11 PM

View PostSkydiver, on 23 July 2012 - 03:03 PM, said:

Congratulations!!!


Thank you :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1