I would like the user to input a sentence that contains quotes around a portion of it and then my program will convert any letters in quotes to upper case.

My problem is how do i write the statement to start toupper() one i == " and end the toupper() one it hits " again.

#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <cctype>
#include <cstring>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])

{
    char szSentance[128];
    int n, i=0;
    char c;


    cout << "Enter a Sentence of all lower case letters.\n" << endl;
    cout << "Place part of the sentence in \"Quotes\". \n" << endl;
    cout << "I.E This is a \"Sentence with \"Quotes\". \n" << endl;
    cin.getline(szSentance, 128);

    while (szSentance[i])

        {
    c=szSentance[i];
    putchar (toupper(c));
    i++;
        }


     n= strlen(szSentance);

    cout << "\n\nThe Sentance had a word count of: " << n <<endl;
    return 0;
}

You have to find the start and end position of the quoted text. In C++, using std::string you can use the following call to find the first quote position stringVariable.find_first_of("\"")//find the first '"' . So you can use "\"" to represent a quote character.

Edited 5 Years Ago by firstPerson: n/a

Set a variable to FALSE - this value will keep track of "
Look at each character.
When you see a ", set the flag to TRUE
When you see a another ", set the flag to FALSE

As you look at each character, if the flag is TRUE, set the character to upper case.

Edited 5 Years Ago by WaltP: n/a

Set a variable to FALSE - this value will keep track of "
Look at each character.
When you see a ", set the flag to TRUE
When you see a another ", set the flag to FALSE

As you look at each character, if the flag is TRUE, set the character to upper case.

bool u = false;

look at each charater.....

i'm not sure how to look at the characters in the array and then set a flag to true
can you please elaborate a little on this.

bool u = false;

So u is your flag. good start.

look at each charater.....

i'm not sure how to look at the characters in the array ...

You don't? What does c=szSentance[i]; do in the code you posted?

...and then set a flag to true
can you please elaborate a little on this.

Try an IF statement.

Edited 5 Years Ago by WaltP: n/a

So u is your flag. good start.


You don't? What does c=szSentance[i]; do in the code you posted?


Try an IF statement.

I thought that may be the reply of sorts, but i was trying to make sure i was on the right mindset before i tried to code:

i ended up try to do this

if(szSentance[i] == " \" ")
     u = true;
     while(u == true)

        {
    c=szSentance[i];
    putchar (toupper(c));
    i++;
        }

but i recieved this error

C:\Users\Freddy\Documents\C++test\anothertest\main.cpp||In function 'int main(int, char**)':|
C:\Users\Freddy\Documents\C++test\anothertest\main.cpp|23|warning: comparison with string literal results in unspecified behaviour|
C:\Users\Freddy\Documents\C++test\anothertest\main.cpp|23|error: ISO C++ forbids comparison between pointer and integer|
||=== Build finished: 1 errors, 1 warnings ===|

If i understand the error correctly I keep getting that I can not compare of szSentance to " \" "

I was trying to say that while(szSentence is == (comparable) to " "\" " then bool value of u is true. then while u comparable (==) to true. run the following code. which then in turn converts the text after the quote to upper case. but then stopping it by saying, acutally i guess something like while(u == true != " \" ") after it has already established a true boolean result from the initial if statement. so that no matter where the quotes were in the text it would see the " and then exit the uppercase loop

You are comparing character by character, so you need single quotes '\"' (since \" is considered to be one character).

Also, since you are comparing a true value with a true value, it will always be true, so the loop will run infinitely (and there's nothing in the loop body to change the value of u.

Edited 5 Years Ago by jonsca: n/a

You can't compare a single character ( szSentance[i] ) with a string " \" " ), especially a 3 character string. Single quotes represent a character: '"'

ok so i placed this into the code snippet and thought it does not work the way i would like it still runs and does something that i can build off, though i think the u = true; statement is being ignored or passed by

#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <cctype>
#include <cstring>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])

{
    char szSentence[128];
    int n, i=0;
    char c;
    bool u= false;


    cout << "Enter a Sentence of all lower case letters.\n" << endl;
    cout << "Place part of the sentence in \"Quotes\". \n" << endl;
    cout << "I.E This is a \"Sentence with \"Quotes\". \n" << endl;
    cin.getline(szSentence, 128);

    if(szSentence[i] == '"')
        u = true;
        while(szSentence[i] != '"')
        {
    c=szSentence[i];
    putchar (toupper(c));
    i++;
        }


     n= strlen(szSentence);

    cout << "\n\nThe Sentance had a word count of: " << n <<endl;
    return 0;
}

I wanted to correct for my above post, the " character in single quotes doesn't need to be escaped.

You don't use u anywhere, so it's just assigned and forgotten.

Also, on 35, that's not going to give you the word count for the sentence, look up what strlen gives you for a result.

Why is your IF statement outside to loop? All you are doing is testing if the first character is a ".

Then in the the loop you never even consult your flag. How do you know whether the character should be changed?

And please be more careful on your formatting. Consistent formatting is a must in programming.

i'm sorry i did not realize the post had gone to page two and missed both replies;

@jonsca line 35 from what i understood counts all characters even white space and out puts an int of how many there are. I thought that was the same thing as a word count.

@ WaltP I'm sorry i've been trying to change this and that to make more sense of what i'm doing and in the end loosing my original code. I'm going back to the code in my previous post and starting again.

Edited 5 Years Ago by alonewolf23: n/a

Good idea. And rather than "trying to change this and that", take out paper and pencil and design what you need. Thinking through the problem is what you need to do, and plan the code.

The best thing a new programmer can do is run through the problem on paper as is you are the computer. This way you understand all aspects of the problem and coding becomes much easier.

when i try and write it out on paper i get snagged.

start:
set boolean var. u to false.

Enter sentence with quotes <-- cout

store szSentence <--cin

while char. in szsentence is not '"'

print out szsentence

go to next <-- (i++)

if szsentence == '"' <-- This is where i get stuck because i feel like my program will exit the while loop once it hits ' " ' so anything i put after this wont be looked at.

this is also where if i understood i would set a flag to say bool u is true and while its true and not equal to ' " ' print my uppercase

when szsentence hit ' " ' again u is to be set back to false and return to the first while loop that continues to print non quoted text.

Edited 5 Years Ago by alonewolf23: n/a

In writting out the previous statement i felt i was right in that the loop was terminating when it hit ' " '

so i just added i++ under it to make it look at the next (passing quote)
then put another while loop that stops my toupper() at the 2nd ' " '.

then again passing the 2nd quote with i++;

and another while loop to print the rest of input sentence terminating when it hits the null character \0 at the end of the string.

This works, but i want to understand what you were trying to teach me about flags and setting the boolean var to true after the first quote and false after the second quote i just cant picture in my head or write down a statement that would allow for such conditions.


#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <cctype>
#include <cstring>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])

{
    char szSentence[128];
    int n, i=0;
    char c;
    //bool u= false;


    cout << "Enter a Sentence of all lower case letters.\n" << endl;
    cout << "Place part of the sentence in \"Quotes\". \n" << endl;
    cout << "I.E This is a \"Sentence with \"Quotes\". \n" << endl;
    cin.getline(szSentence, 128);

    n= strlen(szSentence);

    while(szSentence[i] != '"')
    {
    cout << szSentence[i];
    i++;
    }
        i++;

        while(szSentence[i] != '"')
        {
        c=szSentence[i];
        putchar (toupper(c));
        i++;
        }

        i++;
            while(szSentence[i] != '\0')
            {
            cout << szSentence[i];
            i++;
            }




    cout << "\n\nThe Sentance had a word count of: " << n <<endl;
    return 0;
}

what i see for a boolen var is like a light swicth.

start printing until it hits a quote.

when it hits the quote turn the switch on or ture and execute another loop of the toupper()
and when it hits the next quote turn off the switch or set to false and exit the loop going back to the orignal loop that would be printing .

coding that type of statement is what is giving me trouble i understand the concept but not a way to say it or code it

Edited 5 Years Ago by alonewolf23: n/a

You only need 1 loop. It is used to look at each character up to the ending \0.
In the loop you need
1) test for "
2) test for flag
3) convert char to upper IF appropriate

Now, look again at expanding this to further detail at your desk. For example, what's supposed to happen when step 1 is TRUE?

Also decide when you need to print. Each and every character? Once when the string is finished?

And when your written steps actually work (what you posted obviously doesn't) then and only then do you write the code.

This question has already been answered. Start a new discussion instead.