Im making a program to output a sentence where every other word is an asterik
like this: The dog is brown: would be The *** is ****. However my current code doesnt even output anything, it just receives the input, and i need help with the asterik part
How about: Ask the user to enter a sentence get the whole line to a string
construct an istringstream from the string
while istringstream can extract a word
print the word ignore everything until a space
i starts at -1, and then never changes, so not only are you accessing memory out of bounds, you are also accessing the same memory over and over, and setting it to different memory in such a way that the condition of it being equal to 0 is never true.
okay, say i remove the i variable entirely, and it is just while (str !=0) shouldn't it read through the string until it sees a 0, which would be at the end of whatever you entered? when i enter something in this case, it just continuously repeats
str is a stack-allocated array and will never be equal to 0. Thus, infinite loop.
Try this on for size:
1 2 3 4 5 6 7 8 9 10 11 12
for(unsigned i = 0; str[i] != 0; ++i)
{
boolean makestars = false;
if(str[i] == ' ')
{
//I'll let you write the correct line of code here involving makestars
}
elseif(makestars)
{
//I'll let you write the correct line of code here involving str[i]
}
}
Let's make an exercise. Put a comment in every line that tells what do you think that line is doing.
Then I will show you the correct syntax to accomplish that.
while ( str[i] < length ) //while str[i] is less than the length of the actual string of characters
{
while (str[i] != ' ')
cout << str; //should print the sentence until it sees a null character
if (str[i] ==' ') // if it sees a null character then print a star (although i want it to print the number of stars between the first null character until it sees another null)
cout << '*';
alright i got my program working but where i have if (i > length) is there any other way you guys could suggest to cut out of that loop to get it to output the proper text and stars? i mean it works but my specs say i should user a different method.