2
int main()
{
  char sentence;
  int count;

  cout << "Enter sentence: ";
  cin >> sentence;

  count = 0;
  while ( sentence == 'b' || 'B' ) {
    count++;
  }

  cout << "Number of b's: " << count * 1 << endl;

  return 0;
}

The counting must also stop at all punctuation. I can't seem to get it to give me the correct count.

calf
  • 861
  • 2
  • 11
  • 23

4 Answers4

2

It's your while loop. The variable sentence is not changed inside the loop, so the loop may execute forever.

You may want to use std::string for a sentence and char for a character in the sentence.

Edit 1: Example

char letter;
cout << "Enter a sentence:\n";
while (cin >> letter)
{
  // Check for sentence termination characters.
  if ((letter == '\n') || (letter == '\r') || (letter == '.'))
  {
    break; // terminate the input loop.
  }
  // Put your letter processing code here.
} // End of while loop.
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • Hmm...I see. The problem, I think, is that we haven't gone over strings yet, so I'm not sure if I'm allowed to use them. – calf Oct 01 '13 at 23:51
  • You need to input one character at a time until the User types in an end-of-line character. – Thomas Matthews Oct 01 '13 at 23:53
  • Thomas, that seems similar to the instructions, actually. However, how would I check what the character is while the user is "`cin`-ing" the sentence? – calf Oct 01 '13 at 23:55
  • Compare your character input to '\n' or '\r' or have the user enter some sentinel character such as '.' – Thomas Matthews Oct 01 '13 at 23:57
  • Thomas, I'm very sorry...I'm having a hard time understanding how to implement that. – calf Oct 02 '13 at 00:04
  • Please note that the above program terminates on the period character only, and will not stop on "all punctuation" as the problem requires. Additionally, new line ('\n') and carriage return ('\r') will never be in the stream since they are omitted from console input. – MooseBoys Oct 02 '13 at 00:54
0

There are a couple suspicious points in your program:

  char sentence;
  cin >> sentence;

This looks like it would only be reading one character. You might want to getline() and save user input in a std::string

As for

  while ( sentence == b || B ) {

This wouldn’t even compile since b and B are undefined. Maybe it should be

  if ( cur_byte == ‘b' || cur_byte == ‘B’ )
     count++

where cur_byte is some properly maintained iterator inside your string

Enrico Granata
  • 3,303
  • 18
  • 25
  • Edited to include the 's around the b and B. – calf Oct 01 '13 at 23:52
  • You need to say sentence == ‘b’ || sentence == ‘B’, otherwise your || will always be true - ‘B’ is a non-zero value, which is logically true in C/C++, so you have a possibly-true thing ORed with an always true thing which gives you true, regardless of sentence == ‘b' – Enrico Granata Oct 01 '13 at 23:58
  • Ah, I see. I'll try it with the various suggestions here. – calf Oct 02 '13 at 00:08
0
#include <string>

Use string. string sentence; And create a for long as:

for(int i=0; i<sentence.length(); i++)
if(sentence[i] == b || B) count ++;

So simple ;) Good luck ;)

EDIT 1:
If you will only use while:

int count = sentence.length();
int count2 = 0;
while(count != 0)
{
if(sentence[count] == b||B) count2++
count--;
}

Good luck ;)

Zaur
  • 43
  • 1
  • 6
0
#include <iostream>
using namespace std;
int main()
{
    char c;
    int n = 0;
    cout << "Enter sentence: ";
    while (cin >> c && !ispunct(c)) if (tolower(c) == 'b') n++;
    cout << "Number of b's: " << n << endl;
    return 0;
}

Example:

Enter sentence: Two B or not two b, that is the question Bb.

Number of b's: 2

Community
  • 1
  • 1
MooseBoys
  • 6,641
  • 1
  • 19
  • 43