3

My job is write a program what converts a sentence into lower and upper case.

#include <iostream>
using namespace std;


int main ()
{
int current;
string sent;
cout << "Please enter a sentence: ";
cin >> sent;
for (int i = 0; i < sent.length(); i++)
{
    current = (int) sent[i];
    cout << "Your sentence in all lower case: "<< sent;
    cout << endl;
    if (current >= 65 && current <= 90)
    {
        current += 32;
        sent[i] = (char) current;
        cout << "Your sentence in all upper case: " << sent;
    }
    }

return 0;

}

The output should be:

Please enter a sentence: I Eat Apples!

Sentence in all lower case: i eat apples!

Sentence in all upper case: I EAT APPLES!

However I keep getting a lowercase "i" for the lower case and the upper case I get an upper case I, how come my code doesn't print out the full sentence? I don't get what I did wrong or where the wrong doing is.

IdriveEK9
  • 33
  • 1

4 Answers4

5

The input operator >> separates on white-space (space, newline, tab, etc.). If you want to read a whole line use std::getline:

cout << "Please enter a sentence: ";
getline(cin, sent);

On an unrelated note, please don't use magic numbers like 65 or 32. If you mean characters then use the actual character literals like 'A' or 'a' - 'A' (please note that e.g. 'a' - 'A' is not valid in all encodings, it works in ASCII which is the most common encoding, but it's really not portable). This is also assuming this is a school assignment, otherwise you should be using e.g. std::toupper and some suitable standard algorithm function.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

You should have two loops. One loop will be used to convert all letters of the sentence to the lower case and the other loop will be used to convert all letters of the sentence to the upper case.

Also to enter a sentence you should use standard function std::getline. And you need to include header <string>.

For example

#include <iostream>
#include <string>
//^^^^^^^^^^^^^^^

using namespace std;

int main()
{
    string sentence;

    cout << "Please enter a sentence: ";
    getline( cin, sentence );
    //^^^^^^^^^^^^^^^^^^^^^^

    cout << "Your sentence in all lower case: ";

    for ( string::size_type i = 0; i < sentence.length(); i++ )
    {
        char c = sentence[i];
        if ( c >= 'A' && c <= 'Z' )
        {
            c |= ' ';
        }
        cout << c;
    }
    cout << endl;


    cout << "Your sentence in all upper case: ";
    for ( string::size_type i = 0; i < sentence.length(); i++ )
    {
        char c = sentence[i];
        if ( c >= 'a' && c <= 'z' )
        {
            c &= ~' ';
        }
        cout << c;
    }
    cout << endl;

    return 0;
}

The program output might look like

Please enter a sentence: I Eat Apples!
Your sentence in all lower case:  i eat apples!
Your sentence in all upper case:  I EAT APPLES!

Pay attention to using the blank character ' ' instead of magic number 32 to convert letters to lower or upper case. This allows to apply the approach also to EBCDIC characters.

You could substitute the loops for range based for loops. For example

    for ( char c : sentence )
    {
        if ( c >= 'A' && c <= 'Z' ) c |= ' ';
        cout << c;
    }
    cout << endl;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

your code is quite strange to convert to upper/lower case, there are algorithms int STL to do that easily in C++ . Check out --> How to convert std::string to lower case?

And also , as others mentioned, use getline to get your line from an input stream

Hope this help =)

Community
  • 1
  • 1
0

The extraction operator >> considers spaces (whitespace, tabs, newline etc.) as terminating the value being extracted.

Therefore writing:

std::string str;
std::cin >> str;

If we input This is a string only This will be extracted.


To get a whole line, you could use the function std::getline by including <string> that takes an input stream as first argument, a string as second argument and optionally a delimiter as third argument:

std::string sentence;
std::cout << "Please enter a sentence: ";
std::getline(std::cin, sentence);

To convert all the characters to uppercase or lowercase, there exists a library that you can use <cctype>, it has std::toupper and std::tolower among others.

This can easily be accomplished by using a loop:

std::string sentence_upper(sentence);
for (unsigned int i = 0; i != sentence_upper.length(); ++i) {
    sentence_upper[i] = std::toupper(sentence_upper[i]);
}

Or if you have a C++11 compliant compiler you can use range-based for loop:

std::string sentence_upper(sentence);
for (auto &ch : sentence_upper)
    ch = std::toupper(ch);

Putting it all together:

int main()
{
    // Get input from user
    std::string sentence;
    std::cout << "Please enter a sentence: ";
    std::getline(std::cin, sentence);

    // Create new string, convert to upper
    std::string sentence_upper(sentence);
    for (unsigned int i = 0; i != sentence_upper.length(); ++i) {
        sentence_upper[i] = std::toupper(sentence_upper[i]);
    }

    // Output converted string
    std::cout << "Your sentence in upper case:\n";
    std::cout << sentence_upper << '\n';
}
Andreas DM
  • 10,685
  • 6
  • 35
  • 62