I'm trying to do this program for many hours, but I couldn't.
The question is I've to take some amount of money from user. Then I've to ask how many 5s he have and how many 2s he have. I've to convert that amount of money to 5s and 2s and 1s. User have unlimited source of 1s. In short if user enters 27 I've to tell him 5 5s and 1 2s.
Now i did this type of program in which I converted time(from seconds to years and so on)
Now I did this in program:-
int money;
cout << "How much amount of money= ";
cin >> money;
cout << endl;
int ao5;
cout << "how many number of 5 coins available in your drawer= ";
cin >> ao5;
cout << endl;
int ao2;
cout << "how many number of 2 coins available in your drawer= ";
cin >> ao2;
cout << endl;
int fchange, tchange, ochange;
ochange = (money / 1) % ao5%ao2;
tchange = (money / ao2) % ao5;
fchange = money / ao5;
cout << " " << fchange;
cout << " " << tchange;
cout << " " << ochange;
I've tried other methods. I tried dividing money by 5 and then subtract it with 5s I've but it makes no sense.
Can anyone just take me to right path?
You can use this
void divid(int num){
if(num>=5){
int remainderAfterdevidingTo5 = num%5;
int numOf5s = num/5;
std::cout << " numOf5s "<< numOf5s<< std::endl;
divid(remainderAfterdevidingTo5);
}else if(num>=2){
int remainderAfterdevidingTo2 = num%2;
int numOf2s = num/2;
std::cout << " numOf2s "<< numOf2s<< std::endl;
divid(remainderAfterdevidingTo2);
}else if(num>=1){
std::cout << " numOf1s " <<num<< std::endl;
}
}
Related
at the moment I'm trying to add up the numbers for my hand and the hit cards, the issue is I created a function for my random number generator so that I can keep calling it into my dice game and the blackjack game, I would normally add the number generator to a variable and call it a day but I made it into a function instead. I am still new to c++.
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
void RandomNumber() { cout << (rand() % 10) + 1; }
void blackjack(int total) {
int startstakes = 15;
int stakes;
int hand;
cout << "Welcome to the Blackjack(21) table\n"
<< "How much are you adding to your initial 15 chip stake - ";
cin >> stakes;
cout << "New stake - " << stakes + 15 << " remaining chips - "
<< total - (stakes + 15) << endl;
cout << "Here is your hand - ";
RandomNumber();
cout << " and ";
RandomNumber();
cout << "Hit me cards: 0 - 0 - 0\n"
<< "Total = ";
system("pause>0");
}
int main() {
int total = 0;
int choice;
srand((unsigned)time(NULL));
cout << "Welcome to Royal Casino!!!, How much money do you wish to "
"convert? ";
cin >> total;
cout << "Excelent you currently have " << total << " Chips.\n"
<< "Let's Begin!\n\n"
<< "We have to tables available today\n";
cout << "1) Blackjack (21)\n"
<< "2) Dice\n"
<< "Both have an entry fee of 15 Chips\n"
<< "Select a table - ";
cin >> choice;
if (choice == 1) {
blackjack(total);
}
if (choice == 2) {
dice();
}
system("pause");
}
So the issue is that you should return the value instead of printing it. Like this (note the return type has changed from void to int)
int RandomNumber() {
return (rand() % 10) + 1;
}
A function which returns a value is much more flexible than a function which prints a value.
Now you can use a function call RandomNumber() pretty much the same way as a variable. E.g.
cout << RandomNumber() << " and " << RandomNumber();
or
int var = RandomNumber();
I apologize in advance for the terrible title, it's hard to find a few words that encapsulate my issue effectively.
I have to do a programming project for making a cash register. After I find the total amount of money for the items, I have to input a change value, and make change with twenties, tens, fives, singles, quarters, dimes, nickels, and pennies. I have done the programming to calculate the change in bills and coins, but my professor wants us to not include the bills or coins if there aren't any being returned.
Here is my code so far for it-
void find_change(){
double change_given, updated_price, coin_price;
//item_price_total = 318.32; keep this in here for testing purposes
//change_given = 405.23; ' '
int twenties,
tens,
fives,
singles,
quarters,
dimes,
nickels,
pennies;
//finds the change in bills
do{
cout << "How much change is given? " << endl;
while(!(cin >> change_given)){ //tests to make sure value entered can be used
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Try again: ";
}
if(change_given < item_price_total){
cout << "You did not give the machine enough money" << endl;
}
}while(change_given < item_price_total);
updated_price =change_given-item_price_total;
cout << "The total price is: " << item_price_total << endl;
cout << "The change given is: " << change_given << endl;
cout << "The change back will be: " << updated_price << endl;
twenties = updated_price / 20;
cout << "Twenties: " << twenties << endl;
updated_price = updated_price -(twenties *20);
tens = updated_price/10;
cout << "Tens: " << tens << endl;
updated_price = updated_price - (tens*10);
fives = updated_price/5;
cout << "Fives: " << fives << endl;
updated_price = updated_price - (fives*5);
singles = updated_price/1;
cout << "Singles: " << singles << endl;
updated_price = updated_price - (singles*1);
//this part finds the coins left
coin_price = updated_price * 100;
//finds the change in coins
quarters = coin_price/25;
cout << "Quarters: " << quarters << endl;
coin_price = coin_price - (quarters*25);
dimes = coin_price/10;
cout << "Dimes: " << dimes << endl;
coin_price = coin_price - (dimes*10);
nickels = coin_price/5;
cout << "Nickels: " << nickels << endl;
coin_price = coin_price - (nickels*5);
pennies = coin_price/1;
cout << "Pennies: " << pennies << endl;
coin_price = coin_price - (pennies*1);
}
I apologize for the bad formatting of it when I pasted it all in here. The function runs fine by itself, by when the change given back doesn't include a certain bill or coin I don't know how to not include it. Thanks!
Add tests for the coins/notes values.
#include <iostream>
#include <vector>
using namespace std;
struct Currency {
const char * name;
int value;
};
Currency values[] = {
{"Twenty", 2000},
{"Tens", 10000},
{"Dimes", 10},
{"Pennies",1}
};
int main() {
int change = 101;
for(auto& i: values){
int units = change /i.value;
if(units)// test that there is some change with these coins/notes
std::cout << i.name << units << endl;
change -= units*i.value;
}
return 0;
}
Homework problem I am helping one of my mentees out with (check my history, I have previously asked help with Java in more advanced programs. This is something simple I can't help her figure out). We need to use a while loop to read in numbers, keep track of the count, and keep summing up the numbers entered. We keep getting an error in line 24. Even when I comment it out and run it, the programs doesn't do what it is supposed to do. Been forever since I've done a program in C++ and I need the help of you guys!
#include <iostream>
using namespace std;
int main()
{
int num;
int sum = 0;
int count = 0;
float avg;
cout << "Enter numbers, 999 to quit" << endl;
cin >> num; //
while (num != 999)
{
cout << "Number entered is" << num << endl;
cout << "Enter numbers, 999 to quit" << endl;
cin >> num;
sum = sum + num;
count++;
}
cout << "Total numbers entered: " + count << endl;
cout << "Sum of numbers entered is " + sum << endl;
avg = sum/count;
cout << "Average of numbers entered:" + avg << endl;
return 0;
}
cout << "Total numbers entered: " + count << endl;
cout << "Sum of numbers entered is " + sum << endl;
avg = sum/count;
cout << "Average of numbers entered:" + avg << endl;
Change those +'s to <<'s.
cout << "Total numbers entered: " << count << endl;
cout << "Sum of numbers entered is " << sum << endl;
avg = sum/count;
cout << "Average of numbers entered:" << avg << endl;
#include<iostream>
using namespace std;
int main()
{
int num,count;
float sum,average;
cout << "Enter numbers, 999 to quit" << endl;
cin>>num;
count=0;
sum=0;
while (num!=999)
{
cout<<"Number entered is"<<num<<endl;
++count;
sum+=num;
cout << "Enter numbers, 999 to quit" << endl;
cin>>num;
}
if (count==0) {
count=1;
}// if the first number you enter is 999 count should be 1
// , otherwise avg will be (sum/0 ),which doesn't make sense.
cout << "Total numbers entered: " <<count << endl;
cout << "Sum of numbers entered is " <<sum << endl;
average = sum/count;
cout << "Average of numbers entered:"<<average << endl;
// use << not + because "Total..." is string type and count is int type
system("pause");
return 0;
}
You should pay attention to the type of variable when you do something,which often can cause small errors.
This code is used to run a loop three times, that takes the numbers of eggs gathered and outputs the number in dozens and extra until the user enters a negative number. Then, it prints out the average amount of eggs gathered (entered), and outputs the total number of dozens and extra.
The inputs we were assigned to use are:
43,
31,
-1,
24,
8,
14,
-999,
-5.
Everything is fine up until we input -5. Our teacher doesn't want the average or total number of dozens and extras to print (you'll see what I mean in the output).
The source code is as follows:
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
int eggNum;
int eggDozens;
int eggExtra;
int eggTotal;
int loopCount;
int forCount;
float eggAvg;
int totalDozens;
int totalExtra;
for(forCount = 1; forCount <= 3; forCount=forCount + 1)
{
cout << left << "TEST #" << forCount << ":" << endl;
cout << "Welcome to Aunt Ellen\'s eggs to dozens converter!";
cout << endl << endl;
cout << "\tEnter the number of eggs gathered: ";
cin >> eggNum;
eggTotal = 0;
loopCount = 0;
while(eggNum >= 0)
{
eggDozens = eggNum / 12;
eggExtra = eggNum % 12;
if(eggDozens != 0)
{
if(eggExtra != 0)
{
cout << "\tYou have " << eggDozens << " dozen ";
cout << eggExtra << " eggs.";
cout << endl << endl;
}
else
{
cout << "\tYou have " << eggDozens << " dozen eggs.";
cout << endl << endl;
}
}
else
{
cout << "\tYou have " << eggExtra << " eggs.";
cout << endl << endl;
}
loopCount = loopCount + 1;
eggTotal = eggTotal + eggNum;
cout << "\tEnter the number of eggs gathered: ";
cin >> eggNum;
}
cout << endl << "TOTALS:" << endl;
eggAvg = eggTotal / float(loopCount);
cout << "\tOn average " << eggAvg << " eggs have been";
cout << " gathered.";
totalDozens = eggTotal / 12;
totalExtra = eggTotal % 12;
cout << endl << "\tA total of " << totalDozens << " dozen ";
cout << totalExtra << " and eggs have been gathered!" << endl;
cout << endl << endl;
}
return 0;
}
And the output:
TEST #1:
Welcome to Aunt Ellen's eggs to dozens converter!
Enter the number of eggs gathered: 43
You have 3 dozen 7 eggs.
Enter the number of eggs gathered: 31
You have 2 dozen 7 eggs.
Enter the number of eggs gathered: -1
TOTALS:
On average 37 eggs have been gathered.
A total of 6 dozen 2 and eggs have been gathered!
TEST #2:
Welcome to Aunt Ellen's eggs to dozens converter!
Enter the number of eggs gathered: 24
You have 2 dozen eggs.
Enter the number of eggs gathered: 8
You have 8 eggs.
Enter the number of eggs gathered: 14
You have 1 dozen 2 eggs.
Enter the number of eggs gathered: -999
TOTALS:
On average 15.3333 eggs have been gathered.
A total of 3 dozen 10 and eggs have been gathered!
TEST #3:
Welcome to Aunt Ellen's eggs to dozens converter!
Enter the number of eggs gathered: -5
TOTALS:
On average -1.#IND eggs have been gathered.
A total of 0 dozen 0 and eggs have been gathered!
I don't want the very last "TOTALS" and the lines following. I want the program to terminate after entering -5.
The simplest thing is to do this before entering the while loop:
cin >> eggNum;
if (eggNum < 0)
break ;
That will quit the for loop, and return 0;
You may, if you want to, add some comments to the caller about entering negative numbers before calling break.
You mentioned that you only want to omit the last block of TOTALS.
You can simply add a special case to leave the outer loop early in this case.
Right before this block, but after the closing brace of the while loop.
cout << endl << "TOTALS:" << endl;
eggAvg = eggTotal / float(loopCount);
Insert this:
if (forCount == 3) break;
If you just want to avoid printing whenever the average is less than 0, then instead you should insert in the same location.
if (eggAvg < 0) continue;
This will skip the rest of that iteration of the for loop.
I think a simple answer to your problem would be to just put an if statement around the printing total code. Like this:
if(eggNum > -5){ //won't print for negative 5
cout << endl << "TOTALS:" << endl;
eggAvg = eggTotal / float(loopCount);
cout << "\tOn average " << eggAvg << " eggs have been";
cout << " gathered.";
totalDozens = eggTotal / 12;
totalExtra = eggTotal % 12;
cout << endl << "\tA total of " << totalDozens << " dozen ";
cout << totalExtra << " and eggs have been gathered!" << endl;
cout << endl << endl;
}
I hope this helps!
I've been teaching myself C++ on and off for a few months and now I'm trying to make a payroll system. Here's my code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void wageCompute (int, int);
int main()
{
int loopTimes=0;
int empNum=100, workHours, otHours, rate, bPay, otPay, grossPay;
string empName, empPos;
cout << "PAYROLL FOR THE MONTH OF MARCH" << endl;
cout << "Employees: " << empNum << endl;
while (empNum>loopTimes)
{
cout << "NAME: ";
cin.ignore();
getline (cin, empName);
cout << "\nPOSITION: ";
getline (cin, empPos);
cout << "\nHOURS WORKED: ";
cin >> workHours;
cout << "\nWAGE PER HOUR: ";
cin >> rate;
bPay = workHours*rate;
cout << "YOUR BASE PAY IS: " << bPay << endl << endl;
cout << "HOURS WORKED OVERTIME: ";
cin >> otHours;
otPay = (1.5*rate)*otHours;
cout << "\nOVERTIME PAY: " << otPay << endl;
grossPay = bPay + otPay;
cout << "GROSS PAY: " << grossPay << endl;
wageCompute(bPay, grossPay);
loopTimes++;
}
return EXIT_SUCCESS;
}
void wageCompute(int bPay, int grossPay)
{
double rate, dedInsurance, dedMedical, totDeduct, netPay, tax;
if (bPay<10001)
{
rate = 0.05;
}
else if (bPay<15001)
{
rate = 0.1;
}
else if (bPay<20001)
{
rate = 0.15;
}
else
{
rate = .2;
}
tax = bPay*rate;
dedInsurance = bPay*0.05;
dedMedical = bPay*0.01;
totDeduct = tax + dedInsurance + dedMedical;
cout << "TAX: " << tax << endl;
cout << "SSS DEDUCTION: " << dedInsurance << endl;
cout << "Medicare DEDUCTION: " << dedMedical << endl;
cout << "TOTAL DEDUCTIONS: " << totDeduct << endl;
netPay = grossPay - totDeduct;
cout << "NET PAY: " << netPay << endl;
}
The part where it goes wrong is when I input a certain value for the Hours Worked, Wage per Hour and Hours worked overtime. The program checks the basic pay for the suitable amount of tax it should deduct, what I input was 160 for the hours worked, 100 for the wage per hour, and 10 for overtime. I've tried lessening and increasing it and it worked just fine it seems that it's just these combination of numbers is the part where it goes wrong.
A screenshot of the output:
Your question isn't very clear but I suspect that what you are seeing here is a well known limitation of floating point numbers; numbers that are easy to represent exactly in base 10 don't have an exact representation in base 2. One example : 0.1 in base 10 is 0.0001100110011… repeating in base 2; the accuracy of the approximation depends on how many bits one is willing to use to write it.
An alternative approach is to use integer arithmetic with a known precision, so say you want to calculate to the nearest hundredth of a penny (I'm using UK currency here). Represent £1.01 as 10100 and when your finished val / 10000 is the pounds and (val % 10000) / 100 is the pence. If needed you can implement some more complex rules around rounding for the pence.