change each element of vector to upper case?

N

nvangogh

The problem is to read a sequence of words from cin and store the values
in a vector. After you have read all the words, process the vector and
change each word to upper case. Print the transformed elements.

I have done the first part - storing all words in a vector, but I cannot
seem to access each element of the vector and change each letter to
upper case.

-------------
int main()
{
vector<string> collection;
string aword;
cout << "Input some words in lowercase and see them transformed! type
'end' to end input" << endl;
while(cin >> aword)
{
if(aword != "end")
collection.push_back(aword);
else
break;
}
// process the vector
auto sz = collection.size(); // sz the number of elements in the vector
int index = 0;
while(index <= sz)
{
for(auto &c : collection[index];
{
c = toupper(c);
}
++index;
}
return 0;
}

This does not even compile. Can someone please show me how to do it?
 
V

Victor Bazarov

The problem is to read a sequence of words from cin and store the values
in a vector. After you have read all the words, process the vector and
change each word to upper case. Print the transformed elements.

I have done the first part - storing all words in a vector, but I cannot
seem to access each element of the vector and change each letter to
upper case.

-------------
int main()
{
vector<string> collection;
string aword;
cout << "Input some words in lowercase and see them transformed! type
'end' to end input" << endl;
while(cin >> aword)
{
if(aword != "end")
collection.push_back(aword);
else
break;
}
// process the vector
auto sz = collection.size(); // sz the number of elements in the vector
int index = 0;
while(index <= sz)
{
for(auto &c : collection[index];
{
c = toupper(c);
}
++index;
}
return 0;
}

This does not even compile. Can someone please show me how to do it?

What's the error message you get? The code seems OK, if you add proper
header inclusion, like <vector> and <string>... Did you write it
yourself or did somebody write it for you? If the latter, you should
ask them to explain to you what "a header" is and how to use it.

V
 
N

nvangogh

The problem is to read a sequence of words from cin and store the values
in a vector. After you have read all the words, process the vector and
change each word to upper case. Print the transformed elements.

I have done the first part - storing all words in a vector, but I cannot
seem to access each element of the vector and change each letter to
upper case.

-------------
int main()
{
vector<string> collection;
string aword;
cout << "Input some words in lowercase and see them transformed! type
'end' to end input" << endl;
while(cin >> aword)
{
if(aword != "end")
collection.push_back(aword);
else
break;
}
// process the vector
auto sz = collection.size(); // sz the number of elements in the
vector
int index = 0;
while(index <= sz)
{
for(auto &c : collection[index];
{
c = toupper(c);
}
++index;
}
return 0;
}

This does not even compile. Can someone please show me how to do it?

What's the error message you get? The code seems OK, if you add proper
header inclusion, like <vector> and <string>... Did you write it
yourself or did somebody write it for you? If the latter, you should
ask them to explain to you what "a header" is and how to use it.

V
I did not include the headers for clarity. This is the complete program:

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

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

// program reads sequence of words from cin and stores values in a vector
// after reading all the words process the vector and change each word
to uppercase
// print the transformed elements, eight words to a line

int main()
{
vector<string> collection;
string aword;
cout << "Input some words in lowercase and see them transformed! type
'end' to end input" << endl;
while(cin >> aword)
{
if(aword != "end")
collection.push_back(aword);
else
break;
}
// process the vector
auto sz = collection.size(); // sz the number of elements in the vector
int index = 0;
while(index <= sz)
{
for(auto &c : collection[index];
{
c = toupper(c);
}
++index;
}
return 0;
}
The error:
In function ‘int main()’:
exercise3.17.cpp:35:4: error: ‘c’ was not declared in this scope
 
N

nvangogh

This is the section of code that I want to look at each element (word)
in the vector and change each letter to upper case:

auto sz = collection.size(); // sz the number of elements in the vector
int index = 0;
while(index <= sz)
{
for(auto &c : collection[index]);
{
c = toupper(c);
}
++index;
}

for some reason c is not getting a type automatically assigned. What is
the correct way to do this?
 
J

James Lothian

nvangogh said:
The problem is to read a sequence of words from cin and store the values in a
vector. After you have read all the words, process the vector and change each
word to upper case. Print the transformed elements.

I have done the first part - storing all words in a vector, but I cannot seem to
access each element of the vector and change each letter to upper case.

-------------
int main()
{
vector<string> collection;
string aword;
cout << "Input some words in lowercase and see them transformed! type 'end' to
end input" << endl;
while(cin >> aword)
{
if(aword != "end")
collection.push_back(aword);
else
break;
}
// process the vector
auto sz = collection.size(); // sz the number of elements in the vector
int index = 0;
while(index <= sz)
Whatever else is wrong, this smells bad. The test should almost always be <, not
<=.
{
for(auto &c : collection[index];
Here you're going to index one past the end of the vector.

James
 
M

Martin Shobe

This is the section of code that I want to look at each element (word)
in the vector and change each letter to upper case:

auto sz = collection.size(); // sz the number of elements in the vector
int index = 0;
while(index <= sz)
{
for(auto &c : collection[index]);
{
c = toupper(c);
}
++index;
}

for some reason c is not getting a type automatically assigned. What is
the correct way to do this?

The c in the for loop is getting the correct type automatically
assigned. The problem is the c in the block immediately following the
for loop is not the same c. (Look for extra semi-colon).

Martin Shobe
 
N

nvangogh

This is the section of code that I want to look at each element (word)
in the vector and change each letter to upper case:

auto sz = collection.size(); // sz the number of elements in the vector
int index = 0;
while(index <= sz)
{
for(auto &c : collection[index]);
{
c = toupper(c);
}
++index;
}

for some reason c is not getting a type automatically assigned. What is
the correct way to do this?

The c in the for loop is getting the correct type automatically
assigned. The problem is the c in the block immediately following the
for loop is not the same c. (Look for extra semi-colon).

Martin Shobe
I corrected this error. The program now compiles, but it does not seem
to work. I have made some changes to the code:
// process the vector
auto sz = collection.size(); // sz the number of elements in the vector
int index = 0;

while(index < sz)
{
string word = collection[index];
for(auto &c : word)
{
c = toupper(c);

}
++index;
}
cout << collection[0] << endl; // test output to see if first element
// is now uppercase?

It outputs the word in lower case - not upper case.
 
N

nvangogh

Replace the code below
string word = collection[index];

by

string& word = collection[index];

You are good to go.

I wonder if you could explain briefly why a reference is necessary here?
I have completed my program but cannot understand why 'word' needs to be
a reference to have 'collection[index] assigned. Why did it not work as
a simple string?
 
N

Ney André de Mello Zunino

Replace the code below
string word = collection[index];

by

string& word = collection[index];

You are good to go.

I wonder if you could explain briefly why a reference is necessary here?
I have completed my program but cannot understand why 'word' needs to be
a reference to have 'collection[index] assigned. Why did it not work as
a simple string?

The line
string word = collection[index];

creates a new string object, identified by /word/, as a *copy* of the
existig string object at /collection[index]/.

Therefore, when you iterate over and modify the characters in /word/,
you're not actually touching the original string object inside your
/collection/.

I hope that helps.
 
S

Stuart

Replace the code below
string word = collection[index];

by

string& word = collection[index];

You are good to go.

I wonder if you could explain briefly why a reference is necessary here?
I have completed my program but cannot understand why 'word' needs to be
a reference to have 'collection[index] assigned. Why did it not work as
a simple string?

The line

string word = collection[index];

will create a copy of the object collection[index]. This is by design of
C++. However, you want to manipulate the object collection[index] itself
(how this can be done without introducing another variable is also an
interesting exercise).

You decided to use another variable to manipulate collection[index], so
you can only use either a pointer or a reference (which is, in a
nutshell, a pointer that can never point nowhere). Both pointer or
reference will not create a new string object. They effectively only
contain the address of another object (the reference may even get
optimized away by the compiler so that no additional memory is needed to
store the address of collection[index]).

Regards,
Stuart
 
J

Jorgen Grahn

The problem is to read a sequence of words from cin and store the values
in a vector. After you have read all the words, process the vector and
change each word to upper case. Print the transformed elements.

Side note: in a real program, if you can just "stream" the data
through, you should. Easier to program, and better for the user and
the computer running it.

If I wanted to force you to use a vector for educational purposes, I
would have specified something like "uppercase the words and print
them on std::cout in reverse order" (and perhaps hinted that
std::vector is a good tool for the job).

/Jorgen
 
L

Luca Risolia

nvangogh said:
The problem is to read a sequence of words from cin and store the values
in a vector. After you have read all the words, process the vector and
change each word to upper case. Print the transformed elements.

std::vector<std::string> v;
std::transform(std::istream_iterator<std::string>{std::cin},
std::istream_iterator<std::string>{},
std::back_inserter(v),
[](std::string s) { s[0] = std::toupper(s[0]); return s; });
std::copy(std::begin(v),
std::end(v),
std::eek:stream_iterator<std::string>{std::cout, "\n"});
 
I

Ike Naar

nvangogh said:
The problem is to read a sequence of words from cin and store the values
in a vector. After you have read all the words, process the vector and
change each word to upper case. Print the transformed elements.

std::vector<std::string> v;
std::transform(std::istream_iterator<std::string>{std::cin},
std::istream_iterator<std::string>{},
std::back_inserter(v),
[](std::string s) { s[0] = std::toupper(s[0]); return s; });

That changes only the first character of each word to uppercase.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top