By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,402 Members | 1,099 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,402 IT Pros & Developers. It's quick & easy.

ostream outputting garbage

P: n/a
Hi, i have the following program to display the input received
separated into any word that has Uppercase letters and all lowercase
words.

For the following input "Upper lower" i get the following
output on the console

Upper
0002A634lower
0002A634

I was expecting just

Upper
lower

Why am i getting the print of some memory address presumably, code
below:

using std::string;
using std::vector;
using std::cin;
using std::cout;
using std::ostream;
using std::endl;

bool IsUpper(const string& s);
ostream& Write(ostream& out, vector<string>& v);
vector<stringGetUpper(vector<string>& v);
int _tmain()
{
vector<stringvec;

string s;

while (cin > s)
vec.push_back(s);

vector<stringUpper = GetUpper(vec);

cout << Write(cout, Upper);
cout << Write(cout , vec);

return 0;

}
ostream& Write(ostream& out, vector<string>& v)
{
for(vector<string>::size_type i =0; i!=v.size(); ++i)
{
out << v[i] << endl;
}
return out;
}
vector<stringGetUpper(vector<string>& v)
{
vector<stringUpper;
vector<string>::iterator iter = v.begin();
while (iter != v.end())
{
if (IsUpper(*iter))
{
Upper.push_back(*iter);
iter = v.erase(iter);
}
else
{
++iter;
}
}
return Upper;

}

bool IsUpper(const string& s)
{
bool ret = false;
typedef string::size_type string_size;
string_size i = 0;

while (i != s.size() && (ret==false))
{
if (isupper(s[i]))
ret = true;
++i;
}
return ret;
}
Jan 3 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On 2008-01-03 11:50, dev_15 wrote:
Hi, i have the following program to display the input received
separated into any word that has Uppercase letters and all lowercase
words.

For the following input "Upper lower" i get the following
output on the console

Upper
0002A634lower
0002A634

I was expecting just

Upper
lower

Why am i getting the print of some memory address presumably, code
below:
Please post the full program, including header files. We should only
have to copy & paste and compile, not modify.
cout << Write(cout, Upper);
cout << Write(cout , vec);
Write(cout, Upper);
Write(cout, vec);

--
Erik Wikström
Jan 3 '08 #2

P: n/a
Sorry, header files now included
#include <iostream>
#include <string>
#include <vector>
#include <cctype>

using std::string;
using std::vector;
using std::cin;
using std::cout;
using std::ostream;
using std::endl;

bool IsUpper(const string& s);
ostream& Write(ostream& out, vector<string>& v);
vector<stringGetUpper(vector<string>& v);
int _tmain()
{
vector<stringvec;

string s;

while (cin > s)
vec.push_back(s);

vector<stringUpper = GetUpper(vec);

cout << Write(cout, Upper);
cout << Write(cout , vec);

return 0;

}
ostream& Write(ostream& out, vector<string>& v)
{
for(vector<string>::size_type i =0; i!=v.size(); ++i)
{
out << v[i] << endl;
}
return out;
}
vector<stringGetUpper(vector<string>& v)
{
vector<stringUpper;
vector<string>::iterator iter = v.begin();
while (iter != v.end())
{
if (IsUpper(*iter))
{
Upper.push_back(*iter);
iter = v.erase(iter);
}
else
{
++iter;
}
}
return Upper;

}

bool IsUpper(const string& s)
{
bool ret = false;
typedef string::size_type string_size;
string_size i = 0;

while (i != s.size() && (ret==false))
{
if (isupper(s[i]))
ret = true;
++i;
}
return ret;
}
Jan 3 '08 #3

P: n/a
On Jan 3, 5:22*pm, dev_15 <naumansulai...@googlemail.comwrote:
Sorry, header files now included

#include <iostream>
#include <string>
#include <vector>
#include <cctype>

using std::string;
using std::vector;
using std::cin;
using std::cout;
using std::ostream;
using std::endl;

bool IsUpper(const string& s);
ostream& Write(ostream& out, vector<string>& v);
vector<stringGetUpper(vector<string>& v);

int _tmain()
{
* * * * vector<stringvec;

* * * * string s;

* * * * while (cin >*s)
* * * * * * * * vec.push_back(s);

* * * * vector<stringUpper = GetUpper(vec);

* * * * cout << Write(cout, Upper);
* * * * cout << Write(cout , vec);

* *return 0;

}

ostream& Write(ostream& out, vector<string>& v)
{
* * * * for(vector<string>::size_type i =0; i!=v.size(); ++i)
* * * * {
* * * * * * * * out << v[i] << endl;
* * * * }
* * * * return out;

}

vector<stringGetUpper(vector<string>& v)
{
* * * * vector<stringUpper;
* * * * vector<string>::iterator iter = v.begin();
* * * * while (iter != v.end())
* * * * {
* * * * * * * * if (IsUpper(*iter))
* * * * * * * * {
* * * * * * * * * * * * Upper.push_back(*iter);
* * * * * * * * * * * * iter = v.erase(iter);
* * * * * * * * }
* * * * * * * * else
* * * * * * * * {
* * * * * * * * * * * * ++iter;
* * * * * * * * }
* * * * }
* * * * return Upper;

}

bool IsUpper(const string& s)
{
* * * * bool ret = false;
* * * * typedef string::size_type string_size;
* * * * string_size i = 0;

* * * * while (i != s.size() && (ret==false))
* * * * {
* * * * * * * * if (isupper(s[i]))
* * * * * * * * * * * * ret = true;
* * * * * * * * ++i;
* * * * }
* * * * return ret;

}- Hide quoted text -

- Show quoted text -
Hope you have seen the correction. Repeating again

Write(cout, Upper);
Write(cout, vec);

Additional thoughts:

vector<stringUpper = GetUpper(vec);

After this, Upper is used only for printing. We can write it as

const vector<stringUpper(GetUpper(vec));
Also, the for loop inside Write could well be written as

for(vector<string>::size_type i =0, const vector<string>::size_type
j(v.size()); i!=j; ++i)

Thanks,
Balaji.
Jan 3 '08 #4

P: n/a
LR
dev_15 wrote:
Hi, i have the following program to display the input received
separated into any word that has Uppercase letters and all lowercase
words.

For the following input "Upper lower" i get the following
output on the console

Upper
0002A634lower
0002A634

I was expecting just

Upper
lower

Why am i getting the print of some memory address presumably, code
below:
I didn't look all that carefully at your code, but please consider:
std::ostream &operator<<(std::ostream &o,
const std::vector<std::string &v);

and the line
std::cout << v;
probably produces some code that we can think of like this:
operator<<(std::cout, v);

whereas

std::ostream &write(std::ostream &o, const std::vector<std::string&v);

and the line
std::cout << w(std::cout,v);
probably produces some code that we can think of like this:
operator<<(std::cout, write(std::cout,v));

In this case, the call to write will, if written like your code, write
out each element of v to std::cout and then return a reference to
std::cout. The call to operator<< will write out the 'value' of the
reference. I'm going to guess and say that somehow this reference
decays into a pointer of some kind. I'm probably not technically correct
about that.

You may want to play with this example and see if it clarifies things.

#include <iostream>

const int *p(std::ostream &o, const int &t) {
o << "*" << t << "*" << std::endl;
return &t;
}

int main() {
const int a = 55;
std::cout << "$" << p(std::cout, a) << "$" << std::endl;
std::cout << "#" << &a << "#" << std::endl;
}

LR


Jan 3 '08 #5

P: n/a
On Jan 3, 5:50 am, dev_15 <naumansulai...@googlemail.comwrote:
Hi, i have the following program to display the input received
separated into any word that has Uppercase letters and all lowercase
words.

For the following input "Upper lower" i get the following
output on the console

Upper
0002A634lower
0002A634

I was expecting just

Upper
lower

Why am i getting the print of some memory address presumably, code
below:
Because thats what you asked it to do.
>
using std::string;
using std::vector;
using std::cin;
using std::cout;
using std::ostream;
using std::endl;

bool IsUpper(const string& s);
ostream& Write(ostream& out, vector<string>& v);
vector<stringGetUpper(vector<string>& v);

int _tmain()
int main()
{
vector<stringvec;

string s;

while (cin > s)
vec.push_back(s);
read input indefinitely?
>
vector<stringUpper = GetUpper(vec);

cout << Write(cout, Upper);
cout << Write(cout , vec);
Write(...) is being used like an operator, or trying to rather.
Instead of streaming the results of a function, doesn't it make sense
to stream the object(s), in this case the elements in that
std::vector?
Why don't you define an operator<< to stream std::vector< T instead?
( staggered for readability and untested code )

#include <algorithm>
#include <iterator>

// operator<< for std::vector< std::string >
std::ostream&
operator<<( std::ostream& out,
const std::vector< std::string >& v )
{
std::copy( v.begin(),
v.end(),
std::ostream_iterator< std::string >(out, "\n") );
return out;
}

// then:

std::cout << Upper << vec; // so simple, clean

To further improve the above insertion op, template the operator so it
works with std::vectors of any type, not just std::string. Try it -
it'll be eye opening. An operator that can stream a vector of
anything, including a vector of types that don't exist yet (hint:
std::string already has an overload for op<<, same goes with all
primitive types).

template< typename T >
std::ostream&
operator<<( std::ostream& out,
const std::vector< T >& v )
{
...
}
>
return 0;

}

ostream& Write(ostream& out, vector<string>& v)
{
for(vector<string>::size_type i =0; i!=v.size(); ++i)
{
out << v[i] << endl;
}
return out;

}

vector<stringGetUpper(vector<string>& v)
{
vector<stringUpper;
vector<string>::iterator iter = v.begin();
while (iter != v.end())
{
if (IsUpper(*iter))
{
Upper.push_back(*iter);
iter = v.erase(iter);
}
else
{
++iter;
}
}
return Upper;

}

bool IsUpper(const string& s)
{
bool ret = false;
typedef string::size_type string_size;
string_size i = 0;

while (i != s.size() && (ret==false))
{
if (isupper(s[i]))
ret = true;
++i;
}
return ret;

}
Jan 3 '08 #6

P: n/a
LR
Salt_Peter wrote:
On Jan 3, 5:50 am, dev_15 <naumansulai...@googlemail.comwrote:
> while (cin > s)
vec.push_back(s);

read input indefinitely?

I don't understand, why would that read input indefinitely?

LR
Jan 3 '08 #7

P: n/a
On Jan 3, 3:44 pm, LR <lr...@superlink.netwrote:
I didn't look all that carefully at your code, but please consider:
std::ostream &operator<<(std::ostream &o,
const std::vector<std::string &v);
That function doesn't exist. And if you actually define it, it
won't be found in a template.

The correct way of handling this is to wrap std::vector in a
user defined class.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 3 '08 #8

P: n/a
LR
James Kanze wrote:
On Jan 3, 3:44 pm, LR <lr...@superlink.netwrote:
>I didn't look all that carefully at your code, but please consider:
>std::ostream &operator<<(std::ostream &o,
const std::vector<std::string &v);

That function doesn't exist.
Oops. I should have made that clear.

And if you actually define it, it won't be found in a template.
Sorry, but I'm not sure that I understand what you meant by that. Could
you please expand on that?
The correct way of handling this is to wrap std::vector in a
user defined class.
Do you mean to wrap it if you're going to output it? Or create an
operator<< function? Why is wrapping the correct way?

TIA

LR
Jan 3 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.