I'm trying to convert written code used by shared_ptr to unique_ptr because in the code of its usage seems shared_ptr unnecessary and it will be an exercise with smart pointers. As far as I can detect with debugger the problem is on v.at(i)->push_back(t);. So, as soon as a value entered, the program crashes.
Working code with shared_ptr:
#include <iostream>
#include <iomanip>
#include <memory> // For smart pointers
#include <vector> // For vector container
#include <locale> // For toupper()
using std::vector;
using std::shared_ptr;
int main()
{
vector <shared_ptr<vector<double>>>records; // Temperature records by days
size_t day{ 1 }; // Day number
char answer{}; // Response to prompt
double t{}; // A temperature
while (true) // Collect temperatures by day
{ // Vector to store current day's temperatures created on the heap
auto pDay = std::make_shared<vector<double>>();
records.push_back(pDay); // Save pointer in records vector
std::cout << "Enter the temperatures for day " << day++
<< " separated by spaces. Enter 1000 to end:\n";
while (true)
{ // Get temperatures for current day
std::cin >> t;
if (t == 1000.0) break;
pDay->push_back(t);
}
std::cout << "Enter another day's temperatures (Y or N)? ";
std::cin >> answer;
if (toupper(answer) == 'N') break;
}
double total{};
size_t count{};
day = 1;
std::cout << std::fixed << std::setprecision(2) << std::endl;
for (auto record : records)
{
std::cout << "\nTemperatures for day " << day++ << ":\n";
for (auto temp : *record)
{
total += temp;
std::cout << std::setw(6) << temp;
if (++count % 5 == 0) std::cout << std::endl;
}
std::cout << "\nAverage temperature: " << total / count << std::endl;
total = 0.0;
count = 0;
}
}
Output:
23 34 29 36 1000
Enter another day's temperatures (Y or N)? y
Enter the temperatures for day 2 separated by spaces. Enter 1000 to end:
34 35 45 43 44 40 37 35 1000
Enter another day's temperatures (Y or N)? y
Enter the temperatures for day 3 separated by spaces. Enter 1000 to end:
44 56 57 45 44 32 28 1000
Enter another day's temperatures (Y or N)? n
Temperatures for day 1:
23.00 34.00 29.00 36.00
Average temperature: 30.50
Temperatures for day 2:
34.00 35.00 45.00 43.00 44.00
40.00 37.00 35.00
Average temperature: 39.13
Temperatures for day 3:
44.00 56.00 57.00 45.00 44.00
32.00 28.00
Average temperature: 43.71
The converted code with unique_ptr:
#include <iostream>
#include <vector>
#include <memory>
#include <iomanip>
using std::vector;
int main()
{ // Function scope starts here
// a vector(outside) holding unique_ptrs to a vector(inside) which type is double
vector<std::unique_ptr<vector<double>>> v;
size_t day{ 1 };
char answer{};
double t{};
while (true)
{
size_t i{};
auto pDay = std::unique_ptr<vector<double>>();
v.push_back(std::move(pDay));
std::cout << "Enter the temperatures for day " << day++
<< " separated by spaces. Enter 1000 to end:\n";
while (true)
{
std::cin >> t;
if (t >= 1000.0) break;
v.at(i)->push_back(t);
++i;
}
//std::cout << v.at(0)->at(0) << std::endl;
std::cout << "Enter another day's temperatures (Y or N)? ";
std::cin >> answer;
if (toupper(answer) == 'N') break;
}
double total{};
size_t count{};
day = 1;
std::cout << std::fixed << std::setprecision(2) << std::endl;
for (auto const& record : v)
{
std::cout << "\nTemperatures for day " << day++ << ":\n";
for (auto temp : *record)
{
total += temp;
std::cout << std::setw(6) << temp;
if (++count % 5 == 0) std::cout << std::endl;
}
std::cout << "\nAverage temperature: " << total / count << std::endl;
total = 0.0;
count = 0;
}
} // Function scope ends here
Your std::unique_ptr<std::vector<double>> doesn't point at anything. You'd need to initialize it with a pointer to a vector.
Related
I have a text file with info like this (there are 127 lines, I'll provide a few too keep it short):
Gibraltar 133.25
Israel 104.81
Seychelles 85.89
United-Arab-Emirates 63.95
Falkland-Islands 49.73
My program does work in printing country name and number with the most vaccinations, however it just prints a whole number with no decimals. This is especially problematic with lowest since theres multiple countries with numbers between 0 and 1, and it just prints the first 0 down the list, so not the true minimum, here is my code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void file(string country[], double vaccinations[]);
void highest(string country[], double vaccinations[]);
void lowest(string country[], double vaccinations[]);
void average (string country[], double vaccinations[]);
void welcomeMessage();
int main() {
//declare variables and arrays, set vaccinations and countries arrays to 127 for the number of countries, amd subsequent vaccination data
string countries[127];
double vaccinations[127];
file(countries, vaccinations);
//call wecome message function
welcomeMessage();
//calls function to count highest vaccination rate and country names
highest(countries, vaccinations);
cout << endl;
//calls function to count lowest vaccination rate and country name
lowest(countries, vaccinations);
//calls function to print the average vaccination rate
//average(countries, vaccinations);
}
void welcomeMessage() {
cout << "Welcome to the vaccine data analysis program!" << endl;
cout << endl;
cout << "This program will print which country currently has admistered the most COVID-19 vaccine doses per 100 people" << endl;
cout << endl;
cout << "The program will also print which country currently has administered the least, per 100 people, as well as the average dose administration per 100 people, out of 127 countries on the list." << endl;
cout << endl;
}
void file(string country[], double vaccinations[]) {
int index = 0;
ifstream inFile;
inFile.open("vaccinations.txt");
if (!inFile.is_open()) {
cout << "Unable to open file." << endl;
} else {
while (index < 127) {
//add code to isolate country string
inFile >> country[index];
//add code to isolate vaccination percentage
inFile >> vaccinations[index];
index++;
}
}
}
void highest(string country[], double vaccinations[]) {
string highestCountry = country[0];
int highestTotal = vaccinations[0];
for (int i = 1; i < 127; i++) {
if (vaccinations[i] > highestTotal) {
highestTotal = vaccinations[i];
highestCountry = country[i];
}
}
cout << highestCountry << " has the highest number of vaccinations at " << fixed << setprecision(100) << highestTotal << " per 100 people." << endl;
}
void lowest(string country[], double vaccinations[]) {
string lowestCountry = country[0];
int lowestTotal = vaccinations[0];
for (int i = 1; i < 127; i++) {
if (vaccinations[i] < lowestTotal) {
lowestTotal = vaccinations[i];
lowestCountry = country[i];
}
}
cout << lowestCountry << " has the lowest number of vaccinations at " << fixed << setprecision(100)<< lowestTotal << " per 100 people." << endl;
}
You declared
int highestTotal = vaccinations[0];
therefore, it drops decimals when you copy floating point values into it.
Declaring highestTotal and lowestTotal as doubles should help:
double highestTotal = vaccinations[0];
and
double lowestTotal = vaccinations[0];
EDIT: as #Casey suggests, we can slightly rewrite it (you can add the welcome message and file correctness check if you wish);
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
struct Country {
string name;
double rate;
};
istream& operator>>(std::istream& in, Country& country) {
return in >> country.name >> country.rate;
}
int main()
{
ifstream inFile;
inFile.open("vaccinations.txt");
auto comparator = [](const Country& x, const Country& y) { return x.rate < y.rate; };
auto [min, max] = minmax_element(istream_iterator<Country>{inFile}, istream_iterator<Country>{}, comparator);
string highestCountry = max->name;
double highestTotal = max->rate;
cout << highestCountry << " has the highest number of vaccinations at " << highestTotal << " per 100 people." << endl << endl;
string lowestCountry = min->name;
double lowestTotal = min->rate;
cout << lowestCountry << " has the lowest number of vaccinations at " << lowestTotal << " per 100 people." << endl;
}
When I run the program with either one or more customers. It seems to be that Only with the first customer data, the code I have does not do the calculations correctly for the first customer when trying to get the monthly payments and interest but the calculations are done correctly for the rest of the customers data inputted. What am I missing? Thank you.
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
//Variables
vector <double> Loanlgth, Loanamt, interestRate;
vector <double> monthlyPay(100), loanTotal(100), creditScore(100), totalInterest;
char yes;
const int INPUT_CUSTOMER = 1, DISPLAY_LOAN = 2, EXIT_PROGRAM = 3;
vector <string> customerName;
int option;
int numCustomers = 0;
int index = 0;
//Program
cout << "Thank you for choosing The Bank of UA for your loan requirements!\n\n";
do
{
cout << "UA Bank menu:\n\n"
<< "1. Enter your information\n"
<< "2. See your loan requirements\n"
<< "3. Exit program\n\n"
<< "Choose an option: ";
cin >> option;
while (option < INPUT_CUSTOMER || option > EXIT_PROGRAM)
{
cout << "Please enter a valid menu option: ";
cin >> option;
}
if (option == 1) //Customer enters their information
{
cout << "Please enter the number of customers you would like\n"
<< " to enter loan information for: ";
cin >> numCustomers;
for (index = 0; index < numCustomers; index++)
{
string tempName;
double tempLoanamt, tempLoanlgth, tempcreditScore, tempinterestRate, tempinterest;
cout << "Please enter your name: ";
cin >> tempName;
customerName.push_back(tempName);
cout << "Please enter the loan amount: $";
cin >> tempLoanamt;
Loanamt.push_back(tempLoanamt);
cout << "Please enter the length of the loan in months: ";
cin >> tempLoanlgth;
Loanlgth.push_back(tempLoanlgth);
cout << "What is your current credit score? ";
cin >> tempcreditScore;
creditScore.push_back(tempcreditScore);
if (tempcreditScore <= 600)
tempinterestRate = .12;
interestRate.push_back(tempinterestRate);
if (tempcreditScore > 600)
tempinterestRate = .05;
interestRate.push_back(tempinterestRate);
//Calculations
tempinterest = Loanamt[index] * interestRate[index];
totalInterest.push_back(tempinterest);
loanTotal[index] = (Loanamt[index] + totalInterest[index]);
monthlyPay[index] = loanTotal[index] / Loanlgth[index];
}
}
if (option == 2) // Displays monthly payment
{
cout << fixed << setprecision(2);
for (index = 0; index < numCustomers; index++)
cout << customerName[index] << " your total loan is " << loanTotal[index] << "\n"
<< "with a monthly payment of $" << monthlyPay[index] << "\n"
<< "for " << Loanlgth[index] << " months with an interest\n"
<< "rate of " << interestRate[index] << endl;
}
} while (option != EXIT_PROGRAM);
system("pause");
return 0;
}
Currently, interestRate is populated wrongly. Initially, it contains a garbage value because it is not initialized and if the first condition is not true, the garbage value is pushed, otherwise .12. Next, if the second condition is true, .05 is pushed, otherwise the values from the above flow. So, these combinations of garbage and assigned values are causing values to be pushed twice.
Here's your code:
if (tempcreditScore <= 600)
tempinterestRate = .12;
interestRate.push_back(tempinterestRate);
if (tempcreditScore > 600)
tempinterestRate = .05;
interestRate.push_back(tempinterestRate);
You can correct this in a number of ways:
// push after the calculation is complete
if (tempcreditScore <= 600)
tempinterestRate = .12;
if (tempcreditScore > 600)
tempinterestRate = .05;
interestRate.push_back(tempinterestRate);
or, with if-else (preferable):
if (tempcreditScore <= 600)
tempinterestRate = .12;
else
tempinterestRate = .05;
interestRate.push_back(tempinterestRate);
or, with ternary operator ?: (conciseness and you can make it const):
const double tempinterestRate = (tempcreditScore <= 600 ? .12 : .05);
interestRate.push_back(tempinterestRate);
Apart from this, there are a number of points:
The naming convention is inconsistent throughout the code.
The variables must be initialized because the uninitialized ones may lead to Undefined Behavior.
In the absence of an aggregate type such as struct or class and with multiple pieces of information to store separately in vectors, it is better to keep all the push_backs exactly to once per iteration.
Magic numbers can be avoided for 600, .12 and .5.
The magic numbers for option comparison in if conditions can be removed with their proper constant equivalents i.e. INPUT_CUSTOMER and DISPLAY_LOAN. And, if-else can be used instead of if-if.
The scoping could be improved by moving the variables and objects closer to where they are used.
The vertical blank spaces can improve readability for relevant code blocks.
And, Why is "using namespace std;" considered bad practice?
There might be some other points that you can observe in the following updated code (live):
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
int main()
{
// Variables
std::vector<std::string> customerNames;
std::vector<double> loanLengths, loanAmounts, interestRates;
std::vector<double> monthlyPays, loanTotals, creditScores, totalInterests;
// Program
std::cout << "Thank you for choosing The Bank of UA for your loan requirements!\n\n";
const int INPUT_CUSTOMER = 1, DISPLAY_LOAN = 2, EXIT_PROGRAM = 3;
int option = EXIT_PROGRAM;
do
{
std::cout << "UA Bank menu:\n\n"
<< "1. Enter your information\n"
<< "2. See your loan requirements\n"
<< "3. Exit program\n\n"
<< "Choose an option: ";
std::cin >> option;
while (option < INPUT_CUSTOMER || option > EXIT_PROGRAM)
{
std::cout << "Please enter a valid menu option: ";
std::cin >> option;
}
if (option == INPUT_CUSTOMER) //Customer enters their information
{
int numCustomers = 0;
std::cout << "Please enter the number of customers you would like\n"
<< " to enter loan information for: ";
std::cin >> numCustomers;
for ( int index = 0; index < numCustomers; index++ )
{
std::string name;
double loanAmount = 0.0, loanLength = 0.0, creditScore = 0.0;
std::cout << "Please enter your name: ";
std::cin >> name;
customerNames.push_back( name );
std::cout << "Please enter the loan amount: $";
std::cin >> loanAmount;
loanAmounts.push_back( loanAmount );
std::cout << "Please enter the length of the loan in months: ";
std::cin >> loanLength;
loanLengths.push_back( loanLength );
std::cout << "What is your current credit score? ";
std::cin >> creditScore;
creditScores.push_back( creditScore );
double interestRate = 0.0;
if (creditScore <= 600)
interestRate = .12;
else
interestRate = .05;
// Ternary operator (?:) may also be used here
// const double interestRate = creditScore <= 600 ? .12 : .05;
interestRates.push_back(interestRate);
//Calculations
const double tempTotalInterest = loanAmounts[index] * interestRates[index];
totalInterests.push_back( tempTotalInterest );
const double tempTotalLoan = loanAmounts[index] + totalInterests[index];
loanTotals.push_back( tempTotalLoan );
const double tempMonthlyPay = loanTotals[index] / loanLengths[index];
monthlyPays.push_back( tempMonthlyPay );
}
}
else if (option == DISPLAY_LOAN) // Displays monthly payment
{
std::cout << std::fixed << std::setprecision(2);
for (int index = 0; index < customerNames.size(); index++)
{
std::cout << "\n---\n";
std::cout << customerNames[index] << " your total loan is " << loanTotals[index]
<< "\nwith a monthly payment of $" << monthlyPays[index]
<< "\nfor " << loanLengths[index] << " months with an interest"
<< "\nrate of " << interestRates[index] << std::endl;
std::cout << "---\n";
}
}
} while (option != EXIT_PROGRAM);
return 0;
}
**Guidelines:**get 3 seperate lists of data(array) for head of household, annual income, and household members, then get all annual incomes and average them together. Display in a neat table.
This is from a school project I wasn't allowed to use anything very advanced but I'd like to go back and improve it now. I'd like ti make it cleaner and particularly like to find more that I can take away from it than add to it.
// <Program Name> Programming Project #3 Average Income (Using Functions and Arrays)
// <Author> Brendan Jackson
// <Date of Programs Release> 08/05/15
// <Program Description> takes 3 arrays and displays them with average income
#include <iostream> // allows cin and cout statements
#include <iomanip> //allows setprecision
#include <string> //allows strings
using namespace std; // Namespace std allows program to use entities from <iostream>
int input(string[], int[], double[]); //function 1
double calculate_average_income(double[], int); //function 2
void display_survey_data(string[], int[], double[],int , double); //function 3
int main() // main function
{
//variables for functions
string name[10];
int members[10];
double income[10];
int count_of_households;
double average;
//get input
count_of_households = input(name, members, income);
//calculate average
average = calculate_average_income(income, count_of_households);
//output all data in table
display_survey_data(name, members, income, count_of_households, average);
return 0;
}
int input(string name[], int members[], double income[]) //function 1
{
// get household info
int count_of_households = 0;
cout << "How many house holds were there? ";
cin >> count_of_households;
//TODO: handle bad input (characters and decimals)
if (count_of_households >= 11 || count_of_households < 0)
{
cout << "must enter valid # " ; //TODO: more description
count_of_households = 0; //set back to safe value
}
else
{
//cycle through arrays
for (int count = 0; count < count_of_households; count++) //TODO: take out (count + 1) start from 1 alternatively
{
// get survey info for names
cout << "Enter household #" << (count + 1) << "'s head of household name\t" ;
cin.ignore() ; // ignores keyboard buffer characters
getline (cin, name[count]) ;
// get survey info for income
cout << "Enter household #" << (count + 1) << "'s annual income\t" ;
cin >> income[count];
// get survey info for members
cout << "Enter household #" << (count + 1) << "'s household members\t" ;
cin >> members[count];
}
}
return count_of_households;
}
double calculate_average_income(double income[], int count_of_households) //function 2
{
//add incomes together
double total = 0.0;
double average = 0.0;
//loop over income
for (int count = 0 ; count < count_of_households; count++)
{
//add income to runnning total
total += income[count];
}
// save calculations
average = total / count_of_households;
return average;
}
void display_survey_data(string name[], int members[], double income[],int count_of_households, double average) //funtion 3
{
//print out header
cout << setw(30) << ""
<< setw(30) << ""
<< setw(30) << "NUMBER OF\n" ;
cout << setw(30) << "HOUSEHOLD NAME"
<< setw(30) << "ANNUAL INCOME"
<< setw(30) << "HOUSEHOLD MEMBERS\n" ;
cout << setw(30) << "--------------------"
<< setw(30) << "---------------"
<< setw(30) << "------------------------\n" ;
///loop over values
for (int count = 0 ; count < count_of_households; count++)
{
cout << setw(30) << name[count]
<< setw(30) << setprecision(2) << fixed << showpoint << income[count]
<< setw(30) << members[count]
<< endl;
}
// display average
cout << endl
<< setw(30) << "AVERAGE INCOME"
<< setw(30) << average
<< endl;
}
You could use std::array
This is simply an array on the stack, just like you used, but has iterators, type safe, bound safe, use value semantics and work with most stl algorithm.
It is declared like this:
array<string, 3> myArray;
And it must be passed by reference, because passing by value will copy it's content:
void doSomething(array<int, 6>& aArray) {
// do something
}
Notice that you must specify the length of the array, since it's a template parameter. If you want to have an array of any size, use tempaltes:
template<size_t length>
void foobar(array<double, length> theArray) {
// do something else
}
I am writing a program for a homework assignment that calculates rental car rates based on make, days rented and miles driven. Overall the program works except, when the user is prompted for the number of cars to be calculated, the program continues to prompt the user for input after the number has been exceeded. Also, the formatting for the miles is correct for the first vehicle entered but changes for subsequent entries.
Any help with these two issues would be greatly appreciated!
Code:
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
// Change the console's background color.
system ("color F0");
// Declare the variables.
char carType;
string brand, f("Ford"), c("Chevrolet");
int counter = 0, cars = 0;
double days, miles, cost_Day, cost_Miles, day_Total;
cout << "Enter the number of cars you wish to enter: ";
cin >> cars;
cin.ignore();
while (counter <= cars)
{
cout << "Enter the car type (F or C): ";
cin >> carType;
cin.ignore();
cout << "Enter the number of days rented: ";
cin >> days;
cin.ignore();
cout << "Enter the number of miles driven: ";
cin >> miles;
cin.ignore();
if (carType == 'F' || carType == 'f')
{
cost_Day = days * 40;
cost_Miles = miles * .35;
day_Total = cost_Miles + cost_Day;
brand = f;
}
else
{
cost_Day = days * 35;
cost_Miles = miles * .29;
day_Total = cost_Miles + cost_Day;
brand = c;
}
cout << "\nCar Days Miles Cost\n";
cout << left << setw(12) << brand << right << setw(6) << days << right << setw(8) << miles
<< fixed << showpoint << setprecision (2) << setw(8) << right << "$" << day_Total << "\n\n";
counter++;
}
system ("pause");
}
You have started counting from 0 int counter = 0, cars = 0;
You then count until you are equal to the number that was entered (the "or equal to" bit of while (counter <= cars)).
As a worked example, if I want 3 entries:
Start: counter = 0, cars = 3.
0 <= 3: true
End of first iteration: counter = 1
1 <= 3: true
End of second iteration: counter = 2
2 <= 3: true
End of third iteration: counter = 3
3 <= 3: true (the "or equal" part of this)
End of FORTH iteration: counter = 4
4 <= 3: false -> Stop
We have completed 4 iterations instead of 3. If we only checked for "strictly less than" (counter < cars), the condition at the end of the third iteration would be false, and we'd have ended there.
The heading of your while loop should be:
while(counter < cars)
rather than
while(counter <= cars)
I am trying to make a program which does a very basic calculation, but for some reason i can't get the code right. It is supposed to calculate the miles per gallon for one trip. You can then add this info multiple times (for different trips) and for each time it should calculate the total miles per gallon (i.e. the average miles per gallon of all the trips). This is the code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int counter = 1;
double milePerRe, milePerTo = 0, x, y;
cout << "Enter the miles used (-1 to quit): ";
cin >> x;
cout << "Enter gallons: ";
cin >> y;
while (x != -1)
{
milePerRe = x/y;
milePerTo += milePerRe;
milePerTo /= counter;
cout << "MPG this tankful: " << setprecision( 6 ) << fixed << milePerRe;
cout << "\nTotal MPG: " << setprecision( 6 ) << fixed << milePerTo << endl << endl;
counter++;
cout << "Enter the miles used (-1 to quit): ";
cin >> x;
if (x != -1)
{
cout << "Enter gallons: ";
cin >> y;
}
}
system("pause");
return 0;
}
When I run the program and say I enter 10 on the miles and 1 on the number of gallons the first time and the second time, everything works fine. Then if i do it again a third time the calculations begin to become incorrect.
You can not calculate average of averages the way you do it. In your code you are dividing by the counter EACH iteration, while you should divide it only at the end.
Best way to do what you need is something like this:
...
double totalMiles = 0;
double totalGallons = 0;
...
while (x != -1)
{
milePerRe = x/y;
totalMiles += x;
totalGallons += y;
milesPerTo = totalMiles / totalGallons;
...
However, if your task was to explicitly calculate the average of trips (not the average of miles/gallons), you would need to introduce another variable, like this:
...
double currentMilesPerTo;
...
while (x != -1)
{
milePerRe = x/y;
milePerTo += milePerRe;
currentMilesPerTo = milePerTo/counter;
....
cout << "\nTotal MPG: " << currentMilesPerTo;
...
the value of x and y are not being updated properly i guess.after every iteration try to make x and y to zero.
hope it works this way
TNQ