Hello Dustin792,
After I posted my response I noticed you figured out code tags. The second link is worth reading a time or two to understand the extras that you can use with the code tag and the quote tag.
Having a chance to run your program the problem is not with the inner do/while loops, but with this:
1 2
|
cout << "\n\nEnter Y to restart the program and N to quit: ";
cin >> continueLoop;
|
First you need to understand the difference between formatted and unformatted input.
The "std::getline()" is unformatted input. It will extract everything up to and including the "\n" at the end, but it discards the "\n" leaving the input buffer empty. Best used when you have a space in your input that you want to keep.
Using something like:
std::cin >> dim;
is formatted input and will extract from the input buffer anything up to a white space or "\n" whichever comes first, but it leaves the "\n" in the input buffer.
When you reach:
cin >> continueLoop;
this leaves the "\n" in the input buffer and your next call to "std::getline()" extracts this into "dim" not waiting for any user input making it look like it is invalid input.
There are 2 ways, maybe more, to fix this.
First:
1 2 3
|
cout << "\n\nEnter Y to restart the program and N to quit: ";
cin >> continueLoop;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
|
The first parameter just creates a very large number and as I understand it considered the most portable way to do this that can be used by any set of header files and compiler. Truth be told a large number like 1000 or more will work.
The other option is to change your "std::getlin()"s to formatted input:
std::cin >> dim;
and
std::cin >> start;
.
I have tried both methods and they work giving an output of:
This program is designed to allow the user to create a table of unit conversions.
First you will be given many choices of starting units and ending units.
Then you will be asked to enter the starting dimension(length, mass or time) and a corresponding
initial unit and final unit to convert to.
Next you will enter the starting value, ending value and the amount you would like to increment.
Also, the program will ask for the number of digits after the decimal point for the starting unit and ending unit.
After this occurs the table will increment the starting value to the ending value and convert all values
from starting unit to ending unit. These values will be displayed in a table.
Enter the dimension of choice(length, mass or time): len
INVALID ENTRY - RESTARTING
Enter the dimension of choice(length, mass or time): ma
INVALID ENTRY - RESTARTING
Enter the dimension of choice(length, mass or time): tim
INVALID ENTRY - RESTARTING
Enter the dimension of choice(length, mass or time): length
Enter the unit of choice(meters,feet,centimeters or inches): mt
INVALID ENTRY - RESTARTING
Enter the unit of choice(meters,feet,centimeters or inches): feet
Enter Y to restart the program and N to quit: y
Enter the dimension of choice(length, mass or time):
|
I also made a change:
1 2 3 4 5 6 7 8 9
|
if (dim == "length" || dim == "mass" || dim == "time")
{
userinput = false;
}
else
{
cout << "\n INVALID ENTRY - RESTARTING\n";
userinput = true;
}
|
It gets a bit long, but you could write the next if statement as:
1 2 3 4 5 6 7 8 9 10
|
if (start == "meters" || start == "meter" || start == "m"
|| start == "feet" || start == "ft" || start == "foot"
|| start == "centimeter" || start == "centimeters" || start == "cm"
|| start == "inch" || start == "inches" || start == "in")
userinput = false;
else
{
cout << "\n INVALID ENTRY - RESTARTING\n";
userinput = true;
}
|
Just a thought.
I believe that using the "ignore()" or changing the "std::getline()" to formatted should solve your problem
Andy