Sign in to follow this  
shawnre

Book Drills, Bjarne Stroustrup, Ch. 4 Drill #1

Recommended Posts

Hello all, So, after a few semesters away from C Plus Plus programming, I have found that I have forgotten much of what I knew (or thought I knew). I am getting back into it, and bought Stroustrups' book "Principles and Practice Using C Plus Plus." I am currently on Chapter 4, Drill exercise #1. While it is simple in most regards, we are asked to do the exercises with material that we have been shown thus far in the book and nothing from beyond. The drill is: Write a program that consists of a while-loop that (each time around the loop) reads two ints and then prints them. Exit the program when a terminating '|' is entered. What I wrote:
//Drill #1
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string first, second; //Using string since it is 'white space delimited'

	while (second != "|")
	{
		cout << "Plese enter two numbers: ";
		cin >> first >> second;

		cout << "You entered " << first << ", " << second << endl;
	}
}


Questions: 1. The directions say read ints, and I chose to use a string since the "|" is not an int. Am I reading to far into that? Is there a way to use the while loop and use "int first, second" and still detect the "|" even though it would not be an int at that point and should cause a runtime-error right? 2. I guess is just a preference question, but how would you write that? 3. Curiosity, the Plus Plus never appears in my post, how do I get those to show? rather than writing out "Plus Plus." Thanks for any input! Shawn

Share this post


Link to post
Share on other sites
I understand what you are saying about the ASCII equivalent. However, at this point in the book, we have only discussed basic data types. If I had declared the variables as such, "int first, second," and then entered my while loop, the cin would fail no? (Guess I am going to check that just to see for myself what actually happens)....Ok, just tried, now I am stuck on how to convert "|" in the while condition to equate to the ASCII equivalent.....and I think that is maybe beyond what we have covered at this point.

It is amazing how easy it is to just read stuff, without trying it, and then finding out how little you actually really comprehended I suppose.

Share this post


Link to post
Share on other sites
It's not the strings that are white-space delimited, but the stream extracting itself; the only exception to this is std::getline. When you expect an integer, and the user enters anything other than an integer, the stream will set itself to a failure state and will refuse to process input until the failure has been handled. You can also use std::stringstream to convert to and from types. Take a look at C++ FAQ Lite's Input/output via iostream.

There's a bug in the preview post mode that fails to render the '+', but they're there!

EDIT:
This is how I would write it:


#include <iostream>
#include <sstream>
#include <string>

int main() {
using namespace std;

// Continue reading while cin is in its good state
// (ctrl+z terminates input and sets cin to a fail state)
while ( cin.good() ) {
string line;

// Read the entire line of input
getline( cin, line );

// Check for delimiter
if ( line == "|" )
break;

// Prepare the input for tokenizing
stringstream ss( line );

int first, second;
if ( !(ss >> first) || !(ss >> second) ) // Read in both integers, if either fails, then display error
cout << "Syntax Error!\n";
else
cout << "You entered " << first << " and " << second << ".\n";
}
}





[Edited by - _fastcall on December 25, 2009 1:55:06 PM]

Share this post


Link to post
Share on other sites
You could do something like this :

int main()
{
int first = 0;
int second = 0;
const char EXIT = '|';

while( second != EXIT )
{
cin >> first;
//move pointer until we see a non white space or a new line
while( cin.peek() == ' ' || cin.peek() == '\n'){
cin.ignore(1);
}
//check if the pointer now is a "valid" data.
if(cin.peek() == '|' ) break;

cin >> second;

cout << "First : " << first << endl;
cout << "Second : " << second << endl;
}
}

Share this post


Link to post
Share on other sites
Well, stringstream is a new one on me, even drawing from previous experience. The sstream include is new, thus the new stringstream I guess.

Peek() has not been covered yet, though I see again where you are going with it.

I may just be reading too much into it. I suppose it is hard for others to understand unless you also have the book to reference what has been covered and what has not. Of course, to understand my ignorance of the language may come into play also :-).

I just hate to continue in books when there is something I feel I don't quite understand/comprehend. In the long run I figure it will come back to haunt me. Yet this exercise/drill does seem so trivial....

@Concentrate: If it were not for the peek() function you used....I can see where you are going with that.

Share this post


Link to post
Share on other sites
To be honest, it sounds like you're reading too far into it. If the book is only covering basic usage of cin and cout, as well as practicing while loops, I wouldn't imagine they are trying to make you handle invalid data, different types, or anything else which can complicate the issue.

My recommendation is pick some integer value to use as the stopping point, such as -1 or 99 and do the drill that way instead. It seems like an oversight of the book.

Share this post


Link to post
Share on other sites
@fastcall: Thanks for that link. Finally took the time to go read it and found some nice info there. Thanks for that correction on my misunderstanding of string vs. stream extraction being white space delimited.

I ended up just going back and doing this, and leaving it at that:


//Drill #1
#include <iostream>

using namespace std;

int main()
{
int first, second;
cout << "Please enter two numbers: ";
while(cin>>first>>second)
{
cout << "You entered " << first << ", " << second << endl;
cout << "Please enter two numbers: ";
}
return 0;
}




Not quite exactly what was wanted, since any non integer input will terminate the loop, but good enough for what we have seen so far.

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