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.";
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.
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.
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.";