Extra Blank line on output

Hey everyone. For starters, this is my first time coding in C++ and I have litte experience in code generally. I made a little code that reads through multiple lines of user input and returns it capitalized. If the word is already all caps, it prints nothing..or it should.
eg:
Input:
This
IS
a
tEst

Output(dsired):
THIS
A
TEST

I am using the following lines of code to determine if the word is caps or not, works fine. Only problem is when the word is ignored, I get a blank line in output like this:
THIS

A
TEST

Any way to avoid this? I hope I didn't overcomplicate this. Thanks in advance

1
2
  if (any_of(begin( part ), end( part ), []( char c ) 
        {  return ( islower( c ) ); }))
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
Something like this, perhaps:

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

int main()
{
    std::string word ;
    while( std::cin >> word ) // for read each word read from stdin till eof
    {
        std::size_t n_uppercase = 0 ; // count of uper case chars in the word

        for( char& c : word ) // for each character in the word
        {
            // if it is an upper case character, increment cnt of upper case chars
            if( std::isupper(c) ) ++n_uppercase ;

            // otherwise, convert it to upper case (if it is a lower case character)
            else c = std::toupper(c) ;
        }

        // If the word is already all caps, it prints nothing
        // note that a word which contains non-alpha character eg. HELLO! is not considered to be all caps
        if( n_uppercase != word.size() ) // if it was not already all caps
            std::cout << word << '\n' ; // print out the capitalised word
    }
}
JLBorges thank you for your time!
The program is working OK, except for that extra blank line.
Whole code looks like 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <algorithm>
#include <sstream>

using namespace std;

int main ()
{
  string str;
  string line;
  while (getline(cin,line)&& line.length()!=0) //put all user inputs in one string until an empty string ("ENTER") is entered
  {
      str+=line+"\n";
  }
  
  istringstream iss(str); //turns string to stream so I can modify each word later
                    
  string part;
  while (getline(iss,part)) //cut stream into words
  {
        int i=0;
        char c;
        
        if (any_of(begin( part ), end( part ), []( char c ) //check if word is all caps, if not capitalize
        {  return ( islower( c ) ); })) //WHAT I NEED: If this is FALSE, do not print a new line
        while (part[i-1])
        {
            c=part[i];
            putchar (toupper(c));
            i++;
            
        }
        if (part.length()==1) //stop program if 1-letter word is read
        {
            break;
        }
    cout<<endl;

  }
  return 0;
}


Except for capitalizing, it must stop if a 1-char-long word is entered, and also take multiple lines of input at once.
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
30
31
32
33
34
35
#include <iostream>
#include <string>
#include <cctype>
#include <sstream>

// capitalise the word; return true if at least one character
// was converted from lower case to upper case
bool capitalise( std::string& word )
{
    bool found_lc_char = false ;

    for( char& c : word ) if( std::islower(c) ) // for each lower case character in word
    {
        found_lc_char = true ; // the word has a lower case character
        c = std::toupper(c) ; // convert it to upper case
    }
    return found_lc_char ; // true if at least one lower case character was present
}

int main()
{
    std::string text ; // all text entered by the use

    std::string line ;
    // for read each line read from stdin till eof
    while( std::getline( std::cin, line ) ) text += line + ' ' ; // append to text

    std::istringstream stm(text) ;
    std::string word ;

    // for each word in text, till a single character word is encountered
    while( stm >> word && word.size() > 1 )
        if( capitalise(word) ) // if at least one character was lower case
            std::cout << word << '\n' ; // print it out
}
This is awesome, thank you!
It needs some mini-modifications but I will figure them out.
So, bool is used in order to build a function you can then call in your main? Pretty convenient!
More compact version (no string stream):

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
30
31
#include <iostream>
#include <string>
#include <cctype>

// capitalise the word; return true if at least one character
// was converted from lower case to upper case
bool capitalise( std::string& word )
{
    bool found_lc_char = false ;

    for( char& c : word ) if( std::islower(c) ) // for each lower case character in word
    {
        found_lc_char = true ; // the word has a lower case character
        c = std::toupper(c) ; // convert it to upper case
    }

    return found_lc_char ; // true if at least one lower case character was present
}

int main()
{
    std::string result ; // result to be printed out

    std::string word ;
    // for each word read from stdin, till a single character word is encountered
    while( std::cin >> word && word.size() > 1 )
        if( capitalise(word) ) // if at least one character was lower case
            result += word + '\n' ; // add a the capitalised word to the result

    std::cout << result ; // print out the result
}

about your original code, ¿why do you do while (part[i-1])?
in the first iteration you'll be checking part[-1], which is out of bounds
also, you operate on part[i], so the last iteration you are again out of bounds

you also said «it must stop if a 1-char-long word is entered», however in your example output
THIS
A //¿why didn't you stop here?
TEST


> So, bool is used in order to build a function
no. http://www.cplusplus.com/doc/tutorial/functions/
Topic archived. No new replies allowed.