vector problem2

Pages: 12
my program is
#include<iostream>
#include<vector>
#include<string>
#include<cctype>
using namespace std;

int main()
{
vector<string> s;
string word;
cout <<"Enther your words, EOF to quit: " << endl;
while(cin>>word){
s.push_back(word);
}
if(s.size()>=4 || s.size()<=5 ){
for(vector<string>::size_type ch1=0;ch1!=s.size();ch1++)
{
for(string::size_type ch2=0;ch2!=s[ch1].size();ch2++)
if(isupper(s[ch1][ch2]))
s[ch1][ch2]=tolower(s[ch1][ch2]);
cout<<s[ch1]<<""<<endl;
}

}


return 0;
}

But it can't store all of the four letter words and all of the five letter words. and discard any inputs with fewer than four or more than five characters. how to do it.
Hi,
Firstly :
1
2
3
4
5
6
7
8
9
10
11
12
cout <<"Enter your words, EOF to quit: " << endl;
while(cin>>word){
s.push_back(word);
}
if(s.size()>=4 || s.size()<=5 ){
for(vector<string>::size_type ch1=0;ch1!=s.size();ch1++)
{
for(string::size_type ch2=0;ch2!=s[ch1].size();ch2++)
if(isupper(s[ch1][ch2]))
s[ch1][ch2]=tolower(s[ch1][ch2]);
cout<<s[ch1]<<""<<endl;
}


This will look better :
1
2
3
4
5
6
7
8
9
10
11
12
13
while(word != "EOF")
{
cout << "Enter your words, EOF to quit: ";

cin >> word;


if(word.length() == 4 || word.length() == 5)
{
    s.push_back(word);
    for(char &c : s.back()) c = tolower(c);
}
}


You didn't write anything to make your program display the final output. Try implementing one yourself.
Hi, I use your prgram to fix my program, but it doesn't run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<vector>
#include<string>
#include<cctype>
using namespace std;

int main()
{
	vector<string> s;
    string word;
    while(word != "EOF")
	{
	cout << "Enter your words, EOF to quit: ";
	cin >> word;
	if(word.length() == 4 || word.length() == 5)
	{
	    s.push_back(word);
	    for(char &c : s.back()) c = tolower(c);
	}
}


	return 0;
	}



the system says:
$ g++ -Wall -pedantic test111.cpp
test111.cpp: In function ‘int main()’:
test111.cpp:18:20: warning: range-based ‘for’ loops only available with -std=c++11 or -std=gnu++11
for(char &c : s.back()) c = tolower(c);
^
test111.cpp:18:41: warning: ‘c’ may be used uninitialized in this function [-Wmaybe-uninitialized]
for(char &c : s.back()) c = tolower(c);

what is happen?
Hi,

range-based ‘for’ loops only available with -std=c++11 or -std=gnu++11


Compile with this as a minimum always:

g++ -std=c++14 -Wall -Wextra -pedantic-errors *.cpp -o test111

The test111 is whatever you want to call the executable file - better than a.out IMO. I put c++14 in there - it's the current standard, put c++11 if your compiler doesn't support c++14.

There is still no code to output anything.
but when I input the word,the system says:
Segmentation fault (core dumped)

how to fix it
for(char &c : s.back()) c = tolower(c);

This line is equivalent to :
for(int x = 0; x < s.back().length(); x++) s.back()[x] = tolower(s.back()[x]);

Your program encounters segfaults simply because of this warning :
> warning: ‘c’ may be used uninitialized in this function [-Wmaybe-uninitialized]

...meaning your compiler does not fully support C++11 features.
The for loop on line 18 looks different, the part after the colon is supposed to be a range expression - usually the name of a container.

Anyway, if I show you this:

1
2
3
for (auto item& : s ) { // item is one of the elements (a std::string) in container s
   // do something with item
}


What do you need to do to transform it to lower case?
Last edited on
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<vector>
#include<string>
#include<cctype>
using namespace std;

int main ()
{
  vector<string> s;
  string word;
  while (word != "EOF")
  {
    cout << "Enter your words, EOF to quit: ";
    cin >> word;
    if (word.length () == 4 || word.length () == 5)
    {
      for (size_t i = 0; i < word.size (); ++i)
        word[i] = tolower (word[i]);

      s.push_back (word);
    }
  }
  cout << "Words you entered:\n";
  for (size_t i = 0; i < s.size (); ++i)
    cout << s[i] << '\n';

  system ("pause");
  return 0;
}

Output:
1
2
3
4
5
6
7
8
9
Enter your words, EOF to quit: Anna
Enter your words, EOF to quit: Lisa
Enter your words, EOF to quit: Andrea
Enter your words, EOF to quit: Cathleen
Enter your words, EOF to quit: EOF
Words you entered:
anna
lisa
Press any key to continue . . .
Last edited on
@closed account 5a8Ym39o6

Why couldn't you use word rather than s.back() ?
1
2
s.push_back(word);
	    for(auto &c : word) c = tolower(c);
why my system say :
test111.cpp:27:18: error: ‘system’ was not declared in this scope
system ("pause");
Google c++ std::system ?

Just saying it's a good idea to look for answers yourself:+)
@TheIdeasMan
> Why couldn't you use word rather than s.back()?
Well, that was pretty unclever of me ;-)

@OP
To be able to use Thomas's example, include <stdio.h>
Why I use ctrl-D to leave, the system will endless loop?
how to fix it?
I want to use ctrl-D to leave.
Last edited on
ctrl-D is not at all the same as "EOF". The latter is a string literal.

@closed account 5a8Ym39o6

std::system is in #include <cstdlib>
I write a new program to use Thomas's example now I can use ctril-D to leave
my program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
#include<vector>
#include<string>
#include<cctype>
#include <cstdlib>
#include <fstream>
#include <stdio.h>
using namespace std;

int main ()
{
  vector<string> s;
  string word;
  cout << "Enter your words, EOF to quit: " << endl;
  while (cin >> word)
  {  s.push_back (word);
    if (word.length () == 4 || word.length () == 5)
    {
      for (size_t i = 0; i < word.size (); ++i)
        word[i] = tolower (word[i]);
    }
}
  for (size_t i = 0; i < s.size(); ++i)
  cout << s[i] << endl;;

 system ("pause");
  return 0;
}

I want to my all output is lower litter, but I input capital letters,the output isn't a lower litter
So you do not understand the assignment & the algorithm at all : (
Why must you use ctrl-D to exit your program? You can simply press the [x] button on the caption window to exit the program right
why my input capital letters,the output isn't a lower litter ?
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<string>

using namespace std;

int main ()
{
    string word;
    cout << "Enter your words, EOF to quit: " << endl;
    
    while (cin >> word && word != "EOF")
    {
        if (word.length () == 4 || word.length () == 5)
        {
          for (size_t i = 0; i < word.size (); ++i)
            word[i] = tolower (word[i]);
        }
        cout << word << '\n';
    }
    
    cout << "Please turn the power off\n";
  
  return 0;
}

All litters are now lower litters :)
why my input capital letters,the output isn't a lower litter ?

Why not? Thomas1965's code works.
Pages: 12