I am trying to set the parameters or range for my rands on Friday, Saturday and Sunday so they will only display numbers between minimumSnowfall entered and maximumSnowfall entered. How would one go about doing this? I know how to seed a random number, but have no idea on setting a range. Simple terms if you please(I'm still learning).
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
int main(void)
{
int minimumSnowfall;
int maximumSnowfall;
char skiResortName[81];
cout << "I will forecast how much snowfall there will be on each \nday this weekend. You get to pick the range. " << endl;
cout << "Please enter the minimum amount of snow that \nwill fall in one day in inches: ";
cin >> minimumSnowfall;
cin.ignore(1, '\n');
cout << "Please enter the maximum amount of snow that \nwill fall in one day in inches: ";
cin >> maximumSnowfall;
cin.ignore(1, '\n');
if (minimumSnowfall < maximumSnowfall)
{
cout << "Enter the name of the ski resort: ";
cin.getline(skiResortName, 81);
cout << "\nThe local forecast for snowfall at " << skiResortName << " this weekend: " << endl;
cout << "Friday: " << rand() % 100 << " inches" << endl;
cout << "Saturday: " << rand() % 100 << " inches" << endl;
cout << "Sunday: " << rand() % 100 << " inches" << endl;
}
else
{
cout << "\nThe maximum snowfall entered is less than the minimum snowfall. " << endl;
cout << "No snowfall reported.\n" << endl;
}
return 0;
}
Related
I am trying to store multiple int into a vector by having the user input the grade they have received. However I do believe there is a more simplified version of trying to do this. Would it be better to have separate functions? Is there anyone who can guide me in the right direction?
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main() {
cout << "Welcome to the Grade Calculator! \n"
<< "What is the student's full name? ";
string student_name;
getline(cin,student_name);
cout << "\nPlease input the points earned for each assignment.";
vector<int> student_grades(8);
cout << "\nLab 2: ";
cin >> student_grades[0];
cout << "Lab 3: ";
cin >> student_grades[1];
cout << "Lab 4: ";
cin >> student_grades[2];
cout << "Lab 5: ";
cin >> student_grades[3];
cout << "Lab 6: ";
cin >> student_grades[4];
cout << "Lab 7: ";
cin >> student_grades[5];
cout << "Lab 8: ";
cin >> student_grades[6];
cout << "Lab 9: ";
cin >> student_grades[7];
cout << "Lab 10: ";
cin >> student_grades[8];
//Finding sum of the grades
int sum_of_grades = accumulate(student_grades.begin(),student_grades.end(),0);
//Console Output
cout << '\n'<< student_name << " has earned " << sum_of_grades << " points in the course, so far." << endl;
cout << "\nThere are 2000 points possible in this course." << endl;
cout << "\nInput the anticpated score for the final project (200 points possible): ";
int anticipated_score;
cin >> anticipated_score;
int total_score = sum_of_grades+anticipated_score;
cout << "Total Projected Assignment Points: " << total_score << endl;
cout << "\nFinal Exam Score Needed for Final Course Grade (1000 Points Possible)"
<< "\nTo Earn an A, need at least: " << 1800 - total_score << " points, for a total of: 1800"
<< "\nTo Earn a B, need at least: " << 1600 - total_score << " points, for a total of: 1600"
<< "\nTo Earn a C, need at least: " << 1400 - total_score << " points, for a total of: 1400"
<< "\n To Earn a D, need at least: " << 1200 - total_score << " points, for a total of: 1200" << endl;
return 0;
}
You can use a for-loop:
for(int i = 0; i < 8; ++i){
cout << "\nLab " << i+2 << ": ";
cin >> student_grades[i];
}
This code is essentially equivalent to what you wrote in your program; it loops through all possible indices, prompting the user to input numbers into the vector.
assignment at school asks me to find the present value using double, and void. i was able to write my code up to a certain degree but the result is not what i was expecting.. i ended up separating the present value into different section so at the end i'd multiply the amount given with the rest.. any tips on how to make the code actually work the way its supposed to?
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double payment,year_term, interest;
double sum;
double Power;
double presentv;
double present;
cout << "Hello, how are you doing?" << endl;
cout << "Please insert a payment amount" << endl;
cin >> payment;
cout << " amount inserted: " << payment << endl;
cout << "Enter number of years" << endl;
cin >> year_term;
cout << " number of years: " << year_term << endl;
cout << "Enter interest rate" << endl;
cin >> interest;
cout << " the interest is: " << interest << "%" << endl;
Presentv = ((1 - (pow((1 + interest),year_term))))/interest;
cout << " the value: " << Presentv << endl;
presentva = payment * Presentv;
cout << " the present value is: " << presentva << endl;
}
I have a problem with my code, every time I loop it with the answer 'y'(Yes) it loops to infinity?
I'm trying to make a loan calculator and every time the user is done calculating with a transaction and wants to reset, and do another calculation if he enters in a value 'y', and if he enters 'n' the program will end.
Here's my code so far:
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
char ans = 'y';
do {
string name;
int Months;
int n;
double LoanAmount, Rate, MonthlyInterest, TotalLoanAmount, MonthlyAmortization, OutstandingBalance;
cout << fixed << showpoint;
cout << "Enter Name of Borrower: ";
getline(cin, name);
cout << "Enter Loan Amount: ";
cin >> LoanAmount;
cout << "Enter Number of Months to Pay: ";
cin >> Months;
cout << "Enter Interest Rate in Percent(%): ";
cin >> Rate;
cout << setprecision(2);
MonthlyInterest = LoanAmount * Rate;
TotalLoanAmount = LoanAmount + (MonthlyInterest * Months);
cout << "Monthly Interest: " << MonthlyInterest << endl
<< "Total Loan Amount with interest: " << TotalLoanAmount << endl;
cout << setw(100)
<< "\n\tSUMMARY OF OUTSTANDING INSTALLMENT" << endl
<< "\tName of Borrower: " << name
<< "\n\nMonth\t\tMonthly Amortization\t\tOutstanding Balance"
<< "\n";
for(n = 1; n <= Months; n++) {
MonthlyAmortization = TotalLoanAmount / Months;
OutstandingBalance = TotalLoanAmount - MonthlyAmortization;
cout << n << "\t\t" << MonthlyAmortization << "\t\t\t" << n - 1 << OutstandingBalance << endl;
}
cout << "\nEnd of Transaction";
cout << "Do you want to compute another transaction?[y/n]?" << endl;
cin >> ans;
}
while(ans == 'y');
}
After your cin>>ans, add these two lines :
cin.clear();
cin.sync();
That usually fixes a lot of the infinite looping problems I get with cin.
Also, I would recommend against initializing ans as 'y' when you declare it. I don't think this is causing you problems but it's an uncessesary thing.
You seem to expect pressing y and enter to register as only 'y'. If you want to get the input of just one character have a look at std::cin.get(char)
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.
My program I have been working on is supposed to output the following:
* The number of gallons of paint required
* The hours of labor required
* The cost of the paint
* The labor charges
* The total cost of the paint job
However, it displays 0 in every field.. What have I done wrong now?
Your help would be greatly appreciated.
Here is my code:
//Headers
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
using namespace std;
void PaintJobEstimator(double gallonprice, double calc)
{
float numBucket=0;
float hours=0;
float bucketCost=0;
float laborCharges=0;
float totalCost=0;
//calculates number of buckets of paint (gallons) needed
numBucket=numBucket+calc*(1/115);
//calculates paint cost
bucketCost=bucketCost+gallonprice*numBucket;
//calculates labor hour
hours=hours+calc*(8/115);
//calculates labor charges
laborCharges=hours*18;
//calculates total cost
totalCost=totalCost+bucketCost+laborCharges;
//Console output
cout << "The number of Gallons of paint required:\t" << setprecision(2) << numBucket << endl;
cout << "The hours of labor required:\t" << setprecision(2) << hours << " hrs" << endl;
cout << "The labor charges:\t$" << setprecision(2) << laborCharges << endl;
cout << "The cost of the paint:\t$" << setprecision(2) << bucketCost << endl;
cout << "The total cost of the paint job:\t$" << setprecision(2) << totalCost << endl;
}
void main ()
{
int rooms;
double calc=0;
double wallspace;
double gallonprice;
cout << "=========================================================\n";
cout << "___________________Paint Job Estimator___________________\n";
cout << "_________________________________________________________\n";
cout << endl;
cout << "Enter the number of rooms: ";
cin >> rooms;
while (rooms<1) //validates rooms
{
cout << "Invalid entry, enter one or more rooms:\t";
cin >> rooms;
}
for (int roomNum=1;
roomNum<=rooms;
roomNum++)
{
cout << "Enter the wall space in square meters for room " << roomNum << ":\t" << endl;
cin >> wallspace;
while (wallspace < 0.01)//validates wallspace
{
cout << "Invalid entry, please re-enter the wall area for room " << roomNum << ":\t";
cin >> wallspace;
}
calc=calc+wallspace;
}//end loop
cout << "\nEnter price of the paint per gallon: ";
cin >> gallonprice;
if (gallonprice <10) //validates price per gallon
{
cout << "Invalid entry, Reenter price at a $10.00 minimum: ";
cin >> gallonprice;
}
PaintJobEstimator(gallonprice,wallspace);
system ("pause");
}
Here is a screenshot of the console:
You're multiplying by zero in some of the calculations. For example, in the following line of code:
numBucket=numBucket+calc*(1/115);
You put 1/115 in parenthesis, which evaluates to zero because of integer division. To achieve the desired effect, try:
numBucket = calc / 115.0f;