Making sure there is a certain character in string

Hello,
how do I make sure the input that I get ends with a full stop '.' using do-while?
Example: Hello.

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>
using namespace std;

void uppercase (string &s){

    for(int i=0; i<=s.length(); i++)
    s[i]=toupper(s[i]);
}



int main(){

    string s;

    cout << "Sentence is: ";
    getline(cin,s);
    uppercase(s);

    cout << s <<endl;

return 0;
}
Last edited on
Hello mermaidly,

You already did, for the most part.

In your for loop for(int i=0; i<=s.length(); i++ "s.length()" or "s.size()", both return the same value will run the for loop the needed amount.

The "<=" is the problem as it will loop one more time than you want. If the length, or size, of the string is 10 the string elements are 0 - 9. 10 is past the end of the string.

Changing "<=" to just "<" should solve your problem.

Also the ".length()" and ".size()" functions return a number of type "size_t", so the for loop should be:
for(size_t i = 0; i < s.length(); i++ to avoid any warnings that the compiler might generate.

Andy
Hey,

Thank you for your response, but that's not what I meant. I want the program to loop the main function if the sentence introduced does not end with a fullstop.

Example,
Sentence is: My name is mermaidly (not accepted)
Sentence is: My name is mermaidly. (accepted)

The sentence will be converted into "MY NAME IS MERMAIDLY."

1
2
3
4
5
do {cout << "Sentence is: ";
    getline(cin,s);
    uppercase(s);

    cout << s <<endl;}while(s!='.');


The codes above can't run well.
Last edited on
So
while ( s.find('.') == ? );

Read the manual and determine an appropriate substitution for the ?

Hello mermaidly,

My bad. After seeing salem c's first reply I realize I misread the question.

Looking at your do/while loop:
1
2
3
4
5
6
7
8
do
{
    cout << "Sentence is: ";
    getline(cin, s);
    uppercase(s);

    cout << s << endl;
} while (s != '.');

Lines 3 and 4 are OK for the loop. Lines 5 and 7 should be after the loop.

In the while condition you are trying to check an entire string to a single character. This will not work.

This does work as long as there is only 1 period in what was typed in.
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
#include <cctype>  // <--- std::tolower() and std::toupper().
#include <iostream>
#include <string>  // <--- For using std::string and std::getline().

using namespace std;

void uppercase(string &s)
{

    for (size_t i = 0; i < s.length(); i++)
    {
        s[i] = toupper(s[i]);
    }
}

int main()
{

    string s;

    do
    {
        cout << "Sentence is: ";
        getline(cin, s);

        if (s[s.size() - 1] != '.')
        {
            std::cout << "\n     Sentence must end with a period.\n\n";
        }
    } while (s[s.size() - 1] != '.');

    uppercase(s);

    cout << "\n Converted sentence is: "<< s << endl;

    return 0;  // <--- Not required, but makes a good break point.
}

Notice the addition of the header files that you need.

The parts in bold will get the last character in the string before comparing it to '.'.

This is based on the last character of the string being a period or not. If you need to do something with more than 1 period in a sentence you may need to go a different direction.

Andy
Thank you so much Andy!!
Careful - this will not behave properly if the string is empty.
Topic archived. No new replies allowed.