Reputation: 275
I'm working through a book and one of the assignments is to write a program that does this:
Prompts the user for values. Stores the highest and lowest value. Displays the highest and lowest value. All using a while loop.
So I wrote this:
#include <iostream>
double length;
double length_highest=0;
double length_lowest=0;
int main()
{
std::cout << "Please enter a length.\n";
while(std::cin>>length){
if (length_lowest+length_highest==0){
length_lowest = length;
length_highest = length;
}else if (length<length_lowest){
length_lowest = length;
}else if(length>length_highest){
length_highest = length;
}
std::cout << "The highest length is " << length_highest << ".\n";
std::cout << "The lowest length is " << length_lowest << ".\n";
}
}
Then, the book asks me to modify the program so that it will also accept the units of length of cm, m, ft, and in AND to take into account conversion factors. So, if a user entered in 10 cm, then one inch, the program would have to know that 10 cm > 1 inch. The program would have to store it AND display it WITH the correct unit that corresponds to it.
I've been trying to write this in for the past 3 days or so and all of my methods have failed so I kind of want to move on with the book at this point.
Any suggestions help.
Upvotes: 0
Views: 73
Reputation: 1757
Since it's an exercice i won't give you a direct answer with the code solution.
First of all, since you will need to know which number goes with wich units. You will have to store each numbers.
You could store all numbers in an array which contains 2 element, the number, and the units. To do so, just parse the input.
Then, since you'll have to retrieve in your array your elements. Instead of storing the length as the maxLength, you should store the index where it is stored in the array as the maxIndex.
Then everything is easy, you know how to convert from cm to inch (basic maths), you know how to retrieve the max length and min length with their units.
Another piece of advice to help you is that you should make function. Easy and small functions. Ideas of functions you could do :
There are other ways to do this, it's is just one
Upvotes: 1