I'm new to this site, and I heard that I can get help from here with learning programming. I am having trouble with sentence strings. If someone inputs a sentence followed by just a single character, I want the program to only write the sentence until that character is first shown.

example:
input The cow jumped over the moon.
input p
output The cow jum

C-strings are my weakness i guess, but if someone can help me out, I would appreciate it.

If you want specific help, you should post what you've already tried to do (i.e. code) and what error or bug you are encountering.

If it helps you to get started, strings in C/C++ are arrays of chars. You can compare an element of an array of chars to an individual char. Loops are very useful for this kind of thing.

so like:

if (ch=='a') cout << '/0';
else if (ch=='b') cout << '/0';
else if (ch=='c') cout << '/0';
etc.

until i have a statement for every letter? or is there easier way to actually search each letter, capitals also, where i don't have to type in so much?

ok so i have:

if(ch=='A' || ch=='a') cout << '/0';
else if(ch=='B' || ch=='b') cout << '/0';
else if(ch=='C' || ch=='c') cout << '/0';
etc.

all the way to Z, and it will display the whole sentence without stoppin on the first occurence of the letter. can i use a loop? imma try some loops right now..

Yes you have to use a loop otherwise it becomes tedious. You have also mis-interpreted the information in post 3.

Yes, you want to do a loop.

Assuming you've read in text to a string, and you have the character at which you want to stop, you can either loop through the string displaying a character at a time till you find the stopping char or you run out of string

- or -

As WaltP said, loop through the string and at any point you find that char in the string, assign to it '\0' (note that it's the backslash, not the forward one you've been using.) Then output the string, and output will halt at the place you put the '\0' , also known as the NULL terminator.

You have two easy options as to how you know when to halt the loop, I'll let you think about those for a bit.

Val

I guess there are almost as many ways to learn computer programming as there are computer programmers. A couple of the contrasting styles include taking advantage of standard, already developed material vs reinventing each standard, well developed material in the name of education and understanding. If you're a fan of the latter style then go to a tutorial and learn about loops and char types and strings like everyone else has suggested (for what it's worth, that's the style I prefer as well). But I have to say, if you're a fan of the former style then look up pointers and strchr(), or find()---if you're going to use STL strings.

thanks for all the help you guys have given me so far. i started working on a for loop, and im still having a couple of stupid debug errors with something. this assignment isnt due until next week, so im going to keep working on it and ill probably be back.

and, vmanes, i know that if i use an if statement inside the loop, i can terminate the loop and get out. unless im way off then ill be back.

Yes, there are a couple ways you can use an if condition to cause a loop to exit, but they are inelegant and generally recommended only for exceptional cases. The best solution is set it up so that the loop condition, and only the loop condition, is responsible for halting the loop.
Val

This article has been dead for over six months. Start a new discussion instead.