I am currently going through Bjarne Stroustrups book "Principles and Practice Using C++" on my own. When reaching the end of Chapter 4 I decided to do the drill a little different and create a program that was more than what was asked of me. I asked a friend to do a code review over my simple program. One of his comments was "Overuse of references will create copies at runtime" and he suggested using pointers instead. I thought that passing by reference did not copy the data, but instead allowed you to modify the variable via its location in memory. Could someone please explain if this is true and the cost/benefits of either method. I'm not to the chapters in which these topics are discussed in detail, but my curiosity couldn't wait.
Please note this is not a homework assignment. Just me trying to better understand the storage, optimization, and best practices within the language itself.
//Takes input, converts to inches, cm, meters, and ft, stores meters in vector, allows selection
//based off switch statement.
// Stores all conversions as meters in a vector.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
const string c_units[4] = {"cm", "m", "in", "ft"};
constexpr double c_centimeters = {2.54};
constexpr double c_feet = {12};
constexpr double c_meters = {100};
bool LegalUnits(const string& init_units){
for(const string& x : c_units)
if (x == init_units) {
return true;
}
return false;
}
double Conversions(double init_value, const string& conv_units, vector<double>& converted_numbers){
double meters = 0.0;
if(conv_units == "m"){
meters = init_value;
converted_numbers.push_back(meters);
cout << init_value << " meters is :\n"
<< init_value*c_meters << " centimeters.\n"
<< init_value*c_meters/c_centimeters << " inches.\n"
<< init_value*c_meters/c_centimeters/c_feet << " feet.\n\n";
return 0;
} else if (conv_units == "cm"){
meters = init_value/c_meters;
converted_numbers.push_back(meters);
cout << init_value << " centimeters is :\n"
<< meters << " meters.\n"
<< init_value/c_centimeters << " inches.\n"
<< init_value/c_centimeters/c_feet << " feet.\n\n";
return 0;
} else if (conv_units == "ft") {
meters = init_value*c_feet*c_centimeters/c_meters;
converted_numbers.push_back(meters);
cout << init_value << " feet is :\n"
<< meters << " meters.\n"
<< init_value*c_feet << " inches.\n"
<< init_value*c_feet*c_centimeters << " centimeters.\n\n";
return 0;
} else {
meters = init_value*c_centimeters/c_meters;
converted_numbers.push_back(meters);
cout << init_value << " inches is :\n"
<< init_value/c_feet << " feet.\n"
<< init_value*c_centimeters << " centimeters.\n"
<< meters << " meters.\n\n";
return 0;
}
}
int main(){
string infinite_while = " ";
vector<double> converted_numbers = {};
while(infinite_while != "Exit"){
string init_units;
double init_value = 0;
char selection = ' ';
cout << "Please make a selection from the following switch statement."
"\n\t1. Make a conversion: 'A'"
"\n\t2. List all stored conversions in meters: 'B'"
"\n\t3. List smallest and largest conversions: 'C'"
"\n\t4. Exit: 'D'" << endl;
cin >> selection;
switch (selection) {
case 'A':
case 'a':
case '1':
cout << "Please input your value followed by the type separated by a space: ";
cin >> init_value >> init_units;
if(LegalUnits(init_units)){
Conversions(init_value, init_units, converted_numbers);
} else {
cerr << "\nSorry. That is not a valid unit for conversion.\n" << endl;
}
break;
case 'B':
case 'b':
case '2':
for(double x : converted_numbers)
cout << x << " m\n";
cout << "\n";
break;
case 'C':
case 'c':
case '3':
sort(converted_numbers.begin(), converted_numbers.end());
cout << converted_numbers[0] << " m\n" << converted_numbers[converted_numbers.size()-1]
<< " m\n\n";
break;
case 'D':
case 'd':
case '4':
cout << "Goodbye!";
infinite_while = "Exit";
break;
default:
cout << "Sorry! I don't know that conversion unit." << endl;
break;
}
}
return 0;
}
I have included the code in which I am referring. I have looked around the internet and haven't seen anything that I understood to answer this question. I was not overly thorough in my error handling, it could use more comments, and more error messages for debugging. That being said this is the level in which I am currently at. Also, feel free to comment on how I could do better in the future.
Thank you for your time and consideration.