LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-24-2005, 03:27 PM   #1
timhardy
LQ Newbie
 
Registered: Nov 2003
Location: London
Distribution: Suse 9.2
Posts: 12

Rep: Reputation: 0
Problem passing string::size_type by reference (C++)


I'm teaching myself C++ with "Accelerated C++" (Koenig/Moo) and struggling with an exercise in Chapter 4, question 4-5

Quote:
Write a function that reads words from an input stream and stores them in a vector.
Use that function to write a program that counts the number of words in the input and to count how many times each word occurred.
I've also set myself the extra task of using setw() to make the output neat but to do so I need to calculate the length of the longest string:

I've written the following:

Code:
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
#include <vector>

using std::cin; 	using std::cout;
using std::endl;	using std::max;
using std::sort;	using std::string;
using std::vector;	using std::istream;

istream& word_store(istream& in, vector<string>& ws, string::size_type& maxlength)

{
	if (in) {
	ws.clear();
	string x;
	string::size_type maxlength = 0;
	while (in >> x){
		ws.push_back(x);
		maxlength = max(maxlength, x.size()); 
	}	
	in.clear();
	}
	return in;
}

int main () 
{
	vector<string> words;
	string::size_type maxlen;
	word_store(cin,words,maxlen);
	typedef vector<string>::size_type vec_sz;
	vec_sz wc = words.size();
	cout << "You entered " << wc << " words" << endl;
	cout << "And the longest word was " << maxlen << " characters long." << endl;
	sort(words.begin(), words.end());
        //Sort as a preliminary to counting amount of times each used
	cout << "And here they are sorted in ascii order..." << endl;
	for (vec_sz i = 0; i != wc; ++i){
		cout << words[i] << endl;
	}	
	;
	return 0;
}
It works exactly as I'd hoped apart from one thing: "maxlen" always returns zero and I'm baffled.

I'm probably trying to run before I can crawl but I don't understand why passing the vector "words" as a reference to the function enables the function to modify it but the same thing does not happen when I pass the reference "string::size_type&".

Testing within the function "word_store" reveals that the internal variable "maxlength" is calculated correctly. Since "maxlength" is created as a pass by reference from "maxlen" I thought (naively) that all changes to "maxlength" should modify "maxlen" but this does not seem to be the case.

I'm sure this is an idiotic beginner's question but if anyone more experienced could throw some light on this for me I'd be grateful. Thanks in advance.
 
Old 04-24-2005, 04:54 PM   #2
lyle_s
Member
 
Registered: Jul 2003
Distribution: Slackware
Posts: 392

Rep: Reputation: 55
Replace the line reading:

string::size_type maxlength = 0;

with:

maxlength = 0;

'maxlength' is already declared in your function declaration; declaring another variable called 'maxlength' in the function hides the one declared in your function declaration. Your function is updating the new one you introduced; the original is never touched until you echo it to the user.

It was just luck that the number you got was zero. Here's the output when I tried it before fixing it:

lyle@bowman:~$ g++ test.cxx && ./a.out
this is a test of timehardy's program
You entered 7 words
And the longest word was 134524005 characters long.
And here they are sorted in ascii order...
a
is
of
program
test
this
timehardy's

I think you'll go far as a C++ programmer because you've picked the best book to learn from. Keep up the good work!

Lyle
 
Old 04-24-2005, 05:14 PM   #3
timhardy
LQ Newbie
 
Registered: Nov 2003
Location: London
Distribution: Suse 9.2
Posts: 12

Original Poster
Rep: Reputation: 0
Lyle, thank you so much for that. Not only does the code now work but your explanation explains clearly why it wasn't working before.

Actually I think my original code initialised "maxlen" to zero in the main function - I must have deleted that by mistake when tidying up before pasting it in -

Code:
string::size_type maxlen = 0;
Without that I too get the kind of bizarre random numbers you found.

Thanks for the kind words too. I looked around before deciding on using Accelerated C++: a lot of people rate it highly. I must admit, I'm finding it slow going but I'm confident that if I can get through the whole book, I'll have been given a very comprehensive crash course in the language.

Thanks again!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ / Passing a variable by reference to a class member function. sepulture Programming 12 11-15-2005 10:23 PM
passing variables by reference in javascript djgerbavore Programming 2 06-07-2005 11:34 PM
passing php string to javascript function djgerbavore Programming 2 03-01-2005 11:34 AM
passing passing variable in Java as reference djgerbavore Programming 3 11-10-2004 02:18 PM
passing a string to a function jkobrien Programming 8 11-05-2003 01:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration