Sign in to follow this  
shawnre

Book Drills, Bjarne Stroustrup, Ch. 4 #7

Recommended Posts

Hello all again, Still working through chapter drills with this book, hope I am not annoying people asking for suggestions here. This one does have me a bit confused though. Basically we start with this program:
//Drill #7
#include <iostream>

using namespace std;

int main()
{
	double smallest = 0, largest = 0;
	double entry;
	
	cout << "Please enter a number: ";
	cin>>entry;
	smallest=largest=entry;
	cout << "Please enter another number: ";

	while(cin>>entry)
	{
		if (entry < smallest)
		{
			smallest = entry;
			cout << entry << " smallest so far.\n";
		}
		else if (entry > largest)
		{
			largest = entry;
			cout << entry << " largest so far.\n";
		}
		else
			cout << entry << " is neither the smallest nor the largest so far.\n";
		cout << "Please enter another number: ";
	}

	return 0;
}

The above is what I wrote in preceding exercises, basically tracking if the number entered is the largest or smallest or neither. The next exercise builds off of this as follows: "Add a unit to each double entered; that is, enter values such as 10cm, 2.5in, 5ft, 3.33m. Accept the four units: cm, m, in, ft. Assume conversion factors 1m== 100cm, 1in == 2.54cm, 1ft == 12in. Read the unit indicator into a string." Questions: 1. How do I take the input for this? If a user enters "12cm" vs. "12 cm" it is different right? I need to grab the first value into a double, while the unit will be a string. How do you separate that with cin? Assume white space is my guess, just wondering any other choices I may have. 2. As far as tracking the largest vs. smallest...not sure on that. Should I write functions that convert any input into one unit and compare? What would you do? Thanks for any more help. Sorry if I am being annoying, just trying to learn!

Share this post


Link to post
Share on other sites
Does it say anywhere in the book that you cannot ask for them as different variables?

Enter a number: 5.5
Enter a unit of measurement: cm
ect...

I wouldn't see any need to complicate this sort of thing if they have not already covered how to do it in the book.

Share this post


Link to post
Share on other sites
Quote:

1. How do I take the input for this? If a user enters "12cm" vs. "12 cm" it is different right? I need to grab the first value into a double, while the unit will be a string. How do you separate that with cin? Assume white space is my guess, just wondering any other choices I may have.


This should answer all of those questions:

double input;
string units;
while ( cin >> measurement >> units ) {
/* ... */
}




Just don't forget that you need to convert to a common unit so you can accurately keep track of the smallest and largest.

Quote:

2. As far as tracking the largest vs. smallest...not sure on that. Should I write functions that convert any input into one unit and compare? What would you do?


Keep it simple. What you have is fine; there are some STL functions to aid you, such as std::min and std::max (include <algorithm>). Also, you can factor out some duplicate code by setting smallest and largest to std::numeric_limits<double>::max() and std::numeric_limits<double>:min() respectively (include <limits>), such that the conditions "entry < smallest" and "entry > largest" are always true the first time around.

Share this post


Link to post
Share on other sites
This is what I ended up with, and just wonder what opinions I can get from it. Some assumptions I made were the measurement and units were separated by a space (basically because we have not covered any kind of parsing of input yet). I separated the conversion of measurements based on units and converted to centimeters. One thing I did try was a switch:case type approach. I was notified by my compiler that I could not switch on a string, so changed to if-else comparisons. Just looking for any feedback to help this beginner at C++.


//Drill #7
#include <iostream>
#include <string>

using namespace std;

double ConvertToCM(double, string); //Function prototype to convert all measurements to centimeters

int main()
{
double smallest = 0, largest = 0;
double entry;
string unit;

cout << "Please enter a measurement and unit [eg. 12 in]: ";
cin>>entry>>unit;
smallest = largest = ConvertToCM(entry, unit);

cout << "Please enter another measurement and unit: ";

while(cin>>entry>>unit)
{
double temp = ConvertToCM(entry, unit);
if (temp < smallest)
{
smallest = temp;
cout << entry << " " << unit << " smallest so far.\n";
}
else if (temp > largest)
{
largest = temp;
cout << entry << " " << unit << " largest so far.\n";
}
else
cout << entry << " " << unit << " is neither the smallest nor the largest so far.\n";
cout << "Please enter another number: ";
}

return 0;
}

double ConvertToCM(double measurement, const string unit)
{
double temp;

if (unit == "cm")
return measurement;
else if (unit == "in")
{
temp = measurement * 2.54; //2.54cm = 1 in.
return temp;
}
else if (unit == "m")
{
temp = measurement / 100; //100cm = 1m
return temp;
}
else if (unit == "ft")
{
temp = (measurement * 12) * 2.54;
return temp;
}
else
return 0.0;
}



One other note. The very next drill exercise was to check for an "illegal" representation of units (for example if someone used yards, kilometer, gallons...etc.) Based on my conversion function, I am a little unclear what I should do to accomplish that, any suggestions appreciated.

Thanks all!

Shawn

Share this post


Link to post
Share on other sites
Quote:
Original post by shawnre
Some assumptions I made were the measurement and units were separated by a space (basically because we have not covered any kind of parsing of input yet).


Did you try inputting it without a space to see what will happen?

Quote:
Just looking for any feedback to help this beginner at C++.


If you define ConvertToCM() before main(), it won't need a prototype. This kind of thing simplifies maintainance work.

Also, you can factor out the common 'cout << entry << unit' work.

Quote:
One other note. The very next drill exercise was to check for an "illegal" representation of units (for example if someone used yards, kilometer, gallons...etc.) Based on my conversion function, I am a little unclear what I should do to accomplish that


Well... how does ConvertToCM() behave in that case? Where does control flow to? You need to signal the error in that case instead. One way to do this is by throwing an exception and having the main loop catch and handle it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman

Did you try inputting it without a space to see what will happen?


No, I didn't. I made an assumption that it would fail. I did however, based on your feedback, try it. It worked, and that confuses me. Not sure what question to ask about that. Based on a few extra things I read, I assumed the '<<' extraction operator was [space] based. So, I assumed, I needed that space to assign to the different data types [namely double and string].....I am unclear now where I am confused, any help?


Quote:
If you define ConvertToCM() before main(), it won't need a prototype. This kind of thing simplifies maintainance work.


Well, even though I know about it, we have not covered, in this book, using headers to separate things. Since you mentioned it though, is there a standard between using headers vs....I don't know how to say it, I guess defining locally?

Quote:
Also, you can factor out the common 'cout << entry << unit' work.


Are you saying another function to handle the input? Sorry, guess I sound ignorant, but I am trying to clarify what you mean.

One other note. The very next drill exercise was to check for an "illegal" representation of units (for example if someone used yards, kilometer, gallons...etc.) Based on my conversion function, I am a little unclear what I should do to accomplish that

Quote:
Well... how does ConvertToCM() behave in that case? Where does control flow to? You need to signal the error in that case instead. One way to do this is by throwing an exception and having the main loop catch and handle it.


Even though I know what you mean with throwing an exception, in the book we have not really covered that. I am trying to stick with the book limitations, even though at times I feel I "know" what to do. I am not confident enough with C++ to fully qualify that statement at this point I suppose. I think I can just use my last "else" case to nulify any conversions. I do not feel that is an eloquent solution at this point. Guess I am looking for some more ideas, even though Zahlman has me thinking at this point, and I thank him for that!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this