Counting underscores until a period

I am very new to this and need lots of help.
I need to count the underscores in a sentence until the user enters a period.
without using ARRAYS.

I put a guess as to how to do this, any suggestions are appreciated. thanks

#include<iostream>
using std::cout;
using std::cin;
char ch;

int main()
{
// write a program to read characters input by the user
// until the user enters a period.
// Output the number of underscores the user entered.
// Do not use arrays in your solution.


int underscores=0;
char chr;

cout << "Enter sentence end with a period.";
cin >> underscores;


while (chr!= EOF; chr!=underscores; chr!= ".")
{
getchar(ch);

if(chr=='_')
{
++underscores;
else
(chr!'.')
}
cout << "there were " << underscores << " in the sentence.";

return 0;
}

Why is your else inside the if? I think you meant this:
1
2
3
4
5
} //end of the if(chr=='_')
else if(chr == '.')
{
//your cout statement
}
Last edited on
I will change that. I probably meant a lot of things. I usally don't have such a hard time, but this one it really throwing me.
Also, looking at your code again, it makes less sense...the while loop conditions especially don't make sense; comparing chr to EOF and underscores doesn't make sense really, and it especially doesn't make sense when you have't initialized chr for the first loop. And what's with the cin >> underscores? You know that "____" doesn't translate to an integer number very well, let alone that that is what you're using to count the number of underscores...?

The way to solve this is quite simple, here is what to do from program start to program end:
Initialize a character variable to something other than an underscore or period.
Initialize an unsigned integer variable to 0.
Make a while loop that checks for the character variable to be different than a period.
In the while loop, cin >> to the character variable, and if it's an underscore, increment the unsigned integer variable.
After the while loop, print out the value of the unsigned integer variable and then use cin.sync(); cin.ignore(); to keep the console open until you press enter.
ok, its back to the drawing boards for me. Thanks for the info.
I don't know what this like mean's

Make a while loop that checks for the character variable to be different than a period.

Sorry, I feel like an idiot. I should know this but i've only been doing this for a few weeks.

This is what I have and I know some of it its wrong

int underscores=0;
char count=0;

cout << "Enter sentence end with a period.";
cin >> underscores;


while (underscores>=count; ++count);
cout << ++count;
cin.sync();
cin.ignore();


cout << "There were " << count << " underscores in the sentence.";

return 0;
}

Remove cin >> underscores;.
underscores is your counter for the number of underscores, so you shouldn't be inputting to it.

Rename count to something like ch (I will refer to it as such for the rest of this post) as it is not a counter, it is the character you will take for input.

On your while loop: "Make a while loop that checks for the character variable to be different than a period." means that your while loop should look like this: while(ch != '.')

Remove cout << ++count; and replace it with the correct code: "In the while loop, cin >> to the character variable, and if it's an underscore, increment the unsigned integer variable." Thus, you need an if statement that checks if ch is an underscore, and then inside the if statement, you need to increment your underscores variable. This means you will need {} for your while loop.

And the cin.sync(); cin.ignore(); go after the output.
Last edited on
I understand the the "ch" is a character. I didn't want you to think that I missed that fact. I did put it as the _ but wasn't sure if I could still do that or if that was wrong or is it just any character? I do realize the actual counting is happening with the integer underscores.

int underscores=0;
char ch=0;

cout << "Enter sentence end with a period.";

while (ch != '.');

while (cin>>ch);

If (ch=='_'; ch==underscores; ++ch);

cin.sync();
cin.ignore();

cout << "There were " << underscores << " underscores in the sentence.";

return 0;
}
I should have moved the 2 cins after my last cout statement at the end there.
THANK YOU FOR YOUR HELP!!!!! AND ALL YOUR PATIENCE!!!!!

I wanted to give the final correct code if anyone needed it:


char input;
int underscores=0;
do
{
cin >> input;

if ( input == '_' )
++underscores;
}
while ( input != '.' );

cout << "You entered " << underscores << " underscores";

return 0;

Last edited on
Topic archived. No new replies allowed.