Currency Conversion Program - c++

I'm working on a currency converter program that converts the old system of British pounds, Shillings, and pence, into their new system, which is a type of Decimal Pound. Where 100 pence equals a pound. Here is the code for the program
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;
int calcNum(int pound, int shilling, int pence)
{
pence = pound*240 + shilling*12 + pence;
return pence;
}
int calcNew(int total_pence, double dec_pound)
{
dec_pound = total_pence / 240;
return dec_pound;
}
int main()
{
int pence;
int shilling;
int pound;
const int OLD_POUND = 240;
const int OLD_SHILLING = 12;
double total_pence;
double dec_pound = 0;
double deci_pound;
cout << "Please Enter the Amount of old pounds: ";
cin >> pound;
cout << endl;
if(cin.fail())
{
cout << "That's not a valid number\n";
cout << "This program will terminate on any keypress!";
_getch();
exit(1);
}
cout << "Please Enter the Amount of old shillings: ";
cin >> shilling;
cout << endl;
if(cin.fail())
{
cout << "That's not a valid number\n";
cout << "This program will terminate on any keypress!";
_getch();
exit(1);
}
cout << "Please Enter the Amount of old pence: ";
cin >> pence;
cout << endl;
if(cin.fail())
{
cout << "That's not a valid number\n";
cout << "This program will terminate on any keypress!";
_getch();
exit(1);
}
total_pence = calcNum(pence, shilling, pound);
deci_pound = calcNew(dec_pound, total_pence);
cout << (5, "\n");
cout << "The total amount in decimal pounds is: ";
cout << setprecision(2) << "\x9c" << deci_pound;
_getch();
return 0;
}
When I run this program however, I'm having a bit of a problem. No matter what the number input is, it always says 0 pounds. Just to make sure that the setprecision function at the end wasn't interfering with the code, I had originally set a cout statement with a _getch() after the two functions to show how much deci_pound came out to be calculated to, and once again, it came out as zero. So my issue seems to be somewhere in the functions running the calculations. If someone could help me with this, I would really appreciate it.

Your calcNew(...) function returns an int, make it return a double. Right now it casts to int which involves stripping the decimals.
In your code, dec_pound is set equal to zero, and you're deci_pound = calcNew(dec_pound, total_pence), which divides 0 by 240 = 0.

The order of the parameters when you call both functions is wrong. Your functions are declared and implemented as:
int calcNum(int pound, int shilling, int pence);
int calcNew(int total_pence, double dec_pound);
And then you call them like this:
total_pence = calcNum(pence, shilling, pound);
deci_pound = calcNew(dec_pound, total_pence);

Related

I keep getting a variable uninitialized error when calling a function that is asking for user input

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int numofEmployees();
int daysMissed(int);
int AverageMissed(int, int);
int main()
{
cout << "Welcome to employee absentee calculator!" << endl;
int numEmployees = numofEmployees();
int Missed = daysMissed(numEmployees);
double misAverage = AverageMissed(numEmployees, Missed);
cout << "There are " << numEmployees << " in the company. They have missed " << Missed << " days total. On average, they have missed " << misAverage << " days." << endl;
return 0;
}
int numofEmployees() {
cout << "How many employees are in your company? ";
int employees;
cin >> employees;
while (employees < 1) {
cout << "Employee count must 1 or greater!" << endl;
}
return employees;
}
int daysMissed(int numEmployees) {
int Absence, totAbsence = 0;
for (int i = numEmployees; i < numEmployees; i++) {
cout << "How many days has each employee missed this passed year? ";
cin >> Absence;
totAbsence += Absence;
}
while (Absence < 0) {
cout << "Values entered must be positive numbers!" << endl;
cin >> Absence;
}
return totAbsence;
}
int AverageMissed(int numEmployees, int Missed){
double Average;
Average = double(numEmployees) / double(Missed);
return Average;
}
This code is being used to calculate the average number of employee absences by way of using three functions. The second function is not working correctly as it is not being called properly by the main. This is for a school assignment.
The problem is daysMissed - if numEmployees is <= 0, then Absense will be uninitialized. But, you say, "I check that in numofEmployees" - the problem is that the compiler doesn't do that sort of whole-program analysis before issuing these warnings.
There is another problem: daysMissed is wrong (twice). If there are two employees, and I enter -2 and 1, there will be no error for the negative number. If on the other hand, if I enter 1 and -2, you never correct totAbsence. You would be much better off writing a little function which reads a number >= some limit in a loop, and keeps prompting until given the correct value. Something like:
int read(const char* prompt, const char* err_prompt, int limit) {
cout << prompt << endl;
for(;;) {
int result;
cin >> result;
if (result >= limit) {
return result;
}
cout << err_prompt << endl;
}
}
Then daysMissed becomes much pleasanter to write - and you can use the same function to read the number of employees (which will go into an infinite loop at the moment)
You should also validate a division by zero plus change the return type.
double AverageMissed(int numEmployees, int Missed){
if (Missed > 0) return double(numEmployees) / Missed;
return 0;
}
by the way, there is no need to cast both operands in the division (/). Casting one of them will be enough to return a double type.

Program loops forever when entering 10 digits on CIN

Can you help me guys? I'm a total beginner. My code worked fine then KEEP LOOPING FOREVER and never goes back to or cmd would crash with "Process terminated with status -1073741676". It should loop once then CIN >> again. It happens when I enter 10 digit numbers in my CIN >>.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class noteAssign { //This class return "A" if the random number generated is between 1 and 10
public:
int x;
int noteOut(int x){
if(x>1 && x<10){
cout << "ITS A" << endl;
return x;
}else{
cout << "IT'S NOT A" << endl;
return x;
}
}
}gonote;
int main()
{
cout << "Match the note's Hertz!" << endl;
cout << "Your answer may range from 1 to 20" << endl;
cout << "Type 0 to quit" << endl;
int noteIn; //No real purpose YET
do {
srand(time(0)); //ADDING MULTIPLE RAMDOMIZER FOR WIDER RANDOM RANGE
int rand1 = 1+(rand()%20); //randomizer 1
int rand2 = 1*(rand()%20); //randomizer 2
int hzout = (rand1 * rand2 + rand1 / rand2)%20; //rand 3
noteAssign gonote;
cout << gonote.noteOut(hzout) << endl; //calls the function and gives the parameter
cin >> noteIn; //No real purpose YET
} while(noteIn != 0); //program quits when you enter "0"
};

Trying to find the lowest number in my array, used the same line for highest number

Basically what I am trying to do is get the lowest number, but the program is feeding me back garbage, but I use the same line of code to get the highest value, only change I made was > to <, the program gives me back the highest value no problem put not the lowest. And I have tried everything I can think of from making the lowest= x[0], lowest=101( user is suppose to enter in grades on scale of 0-100, thought made it had something to do with the value. ) and lowest =highest and it still give me back a number like -9.255596e...., any help or suggestion or greatly appreciated, or maybe a point in the right direction just really trying to understand why it works for one set of numbers and not the others.
#include "stdafx.h"
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
double average(double,int);
double sum1(double[],int);
double highest(double[], int);
double lowest(double[], int);
int _tmain(int argc, _TCHAR* argv[])
{
double gradeBook[1000];
char x;
int count = 0;
cout << "Do you wish to start the program if so enter y to stop enter q" << endl;
cin >> x;
while (x != 'q')
{
cout << "Enter in test grade on a scale of 0 to 100" << endl;
cin >> gradeBook[count];
if (gradeBook[count]<0 || gradeBook[count]>100)
{
cout << " Please try again ";
count--;
}
else
cout << "valid answer" << endl;
count++;
cout << "Do you wish to continue entering in grades? If so enter y to stop enter q" << endl;
cin >> x;
}
highest(gradeBook, count);
cout << "The highest grade enter is " << highest(gradeBook, count) << endl;
lowest(gradeBook, count);
cout << "The lowest grade enter is " << lowest(gradeBook, count) << endl;
cout << lowest <<endl;
return 0;
}
double highest(double x[], int y)
{
double highest = 0;
for (int i = 0; i<= y; i++)
{
if (x[i]>highest)
highest = x[i];
}
return highest;
}
double lowest(double x[], int y)
{
double lowest = 100;
for (int i = 0; i<= y; i++)
{
if (x[i]< lowest)
lowest = x[i];
}
return lowest;
}
A way to resolve your question is to use code already tested.
In your case you can use min_element and max_element to find min and max element of your code:
cout << "The highest grade enter is " << *max_element(gradeBook,
gradeBook+count) << endl;

I modified the following C++ program, but did i get it correct?

In the analysis of the following program my book says, "In this sample, the input parameter containing the number sent by the user has been modified. If you need both values, the original and the square, you can have the function accept two parameters: one that contains the input and the other that supplies the square."
#include <iostream>
using namespace std;
void ReturnSquare(int& Number)
{
Number *= Number;
}
int main()
{
cout<< "enter a number you wish to square: ";
int Number = 0;
cin>> Number;
ReturnSquare(Number);
cout<< "square is: " << Number <<endl;
cout<< "press enter to continue..." <<endl;
cin.ignore(10, '\n');
cin.get();
return 0;
}
I made a quite extensive modification to this program, which compiles and runs properly, but did I demonstrate what the book was asking me to correctly? Please help me I am only beginning. Modification follows:
#include <iostream>
using namespace std;
void ReturnSquare(int& Number, int Number2)
{
cout<< "do you wish to add number ?...(y/n) ";
char CalcCircum = 'n';
cin>> CalcCircum;
if (CalcCircum == 'n' || 'y')
Number *= Number;
if (CalcCircum =='y')
cout<< "addition of number is..." << Number2 + Number2 <<endl;
}
int main()
{
cout<< "enter a number you wish to square: ";
int Number = 0;
cin>> Number;
ReturnSquare(Number, Number);
cout<< "square is: " << Number <<endl;
cout<< "press enter to continue..." <<endl;
cin.ignore(10, '\n');
cin.get();
return 0;
}
Please tell me Where my admittedly little experienced thinking has, as I suspect, gone wrong. Thank you all, sincerely newmanadam
The exercise asks for a very unconventional manner to return a value.
void calc(int argument, int& returnValue){
returnValue = argument * argument; }
You have to call it in an odd way:
int returnValue;
int square = 17;
calc(square, returnValue);
std::cout << returnValue;
You might see 289 on your console.
You made it a lot more complicated that it needs to be. Change the function to:
void ReturnSquare(int in, int& out)
{
out = in*in;
}
and then change main to:
int main()
{
cout<< "enter a number you wish to square: ";
int messaround = 0;
cin>> messaround;
int sq;
ReturnSquare(messaround, sq);
cout<< "original number is: " << messaround <<endl;
cout<< "square is: " << sq <<endl;
cout<< "press enter to continue..." <<endl;
cin.ignore(10, '\n');
cin.get();
return 0;
}
This very slight variation of my modified problem program also works, and is perhaps slightly more readable. Variation follows:
#include <iostream>
using namespace std;
void ReturnSquare(int& Number, int Number2)
{
cout<< "do you wish to add number ?...(y/n) ";
char CalcCircum = 'n';
cin>> CalcCircum;
Number *= Number;
if (CalcCircum =='y')
cout<< "addition of number is..." << Number2 + Number2 <<endl;
}
int main()
{
cout<< "enter a number you wish to square: ";
int messaround = 0;
cin>> messaround;
ReturnSquare(messaround, messaround);
cout<< "square is: " << messaround <<endl;
cout<< "press enter to continue..." <<endl;
cin.ignore(10, '\n');
cin.get();
return 0;
}
Thank you all very much. Much respect from a newbie programmer (but hopefully not by this time next year). Peace to all fellow coders, sincerely, newmanadam

Why is my code not outputting (just) numbers?

exercise prompt for code: Write a program that tells what coins to give for any amount of change from 1 cent to 99 cents. Use coin denominations of 25 cents (quarters), 10 cents (dimes), and 1 cent (pennies). Do not use nickel and half-dollar coins. Your program will use the following function (among others):
void compute_coins(int coin_value, int& num, int& amount_left);
#include <iostream>
#include <string>
using namespace std;
void prompt(int *amount_left);
void remaining_change(int *amount_left, int coin_value);
void compute_coins(int coin_value, int *num, int *amount_left);
void output(string coin_name, int *num);
int main() {
int change = 0, num = 0, amount_left = 0;
const int quarter = 25, dime = 10, penny = 1;
string q = "quarter(s)", d = "dime(s)", p = "penny(s)";
prompt(&change);
compute_coins(quarter, &num, &amount_left);
remaining_change(&amount_left, quarter);
output(q, &num);
compute_coins(dime, &num, &amount_left);
remaining_change(&amount_left, dime);
output(d, &num);
compute_coins(penny, &num, &amount_left);
output(p, &num);
}
void prompt(int *change)
{
cout << "How much change is there? ";
cin >> *change;
cout << "You entered " << change << endl;
cout << "That is equal to: ";
}
void remaining_change(int *amount_left, int coin_value)
{
*amount_left = (*amount_left % coin_value);
}
void compute_coins(int coin_value, int *num, int *amount_left)
{
*num = *amount_left / coin_value;
}
void output(string coin_name,int *num)
{
cout << num << " " << coin_name << ", ";
}
You are outputting the value of the pointers, not the value of the object pointed to.
The simple fix is to dereference the pointers first:
cout << "You entered " << *change << endl;
// ^
cout << *num << " " << coin_name << ", ";
// ^
However, I'd suggest not using pointers for things like this at all. For built-in types you should take a reference when you want to update the variable and a value otherwise.
Personally I wouldn't update those variables from inside the functions either, I'd carry out the necessary input or calculation and return a value to be assigned.
In prompt(), change is a pointer, so in order to output the value that change points to you would need to modify this line:
cout << "You entered " << change << endl;
to:
cout << "You entered " << *change << endl;
Better still, though, you could use a reference rather than a pointer:
void prompt(int &change)
{
cout << "How much change is there? ";
cin >> change;
cout << "You entered " << change << endl;
cout << "That is equal to: ";
}
and then you would just call this as:
prompt(change);
This is much more idiomatic C++ – the pointer method is more "old skool" C-style programming.
Ditto for the other places where you are printing the pointer itself, e.g. num.