How to end program with period?

I'm trying to write some program and I need it to end when a period or exclamation point is entered. I have to use while/if loops. I feel like I'm really close but I don't know what I'm doing wrong

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main ()
{

  char text;
  
  cout << "Enter text: " <<endl;   
  cin >> text;

  do
    {

    }while (text != '.' || text != '!');

  return (0);
}


so if I write a sentence and then a period/exclamation point it has to end. What should I do to fix this?

The instructions say:
'Use a while loop to read and count the characters in the input. Read in a character, check that the input character is not the period, ‘.’, nor the exclamation mark, ‘!’, and then enter the while loop.'

I'm not trying to get people to write this for me; it's a small part of some Scrabble program that will count points of inputted words/phrases.
Last edited on
You're really close.

you need a variable to keep track of the count, so declare a variable of type int and increment it everytime you read something in.

Also, it sounds like you shouldn't be using a do while loop but rather a simple while loop.
also you wan't && not ||

}while (text != '.' || text != '!');

}while (text != '.' && text != '!');
Last edited on
I though || meant OR. If either one is typed in, the program has to terminate
What does this mean:

'To read in a single character, use cin to read a value of type char'
|| does mean OR.

When text is '.': text != '.' is false, but text != '!' is true. false || true is true. The condition as you wrote it could never be false.

If you want to use ||, the condition should be: !(text == '.' || text == '!').
Last edited on
or means that one expression on either side of the || have to be true for the loop to continue. And && means that both expressions on either side of the && have to be true for the loop to continue.

in your code you wan't the loop to continue while text != '.' and text != '!' meaning both expressions text != '.' and text !='!' need to be true.

'To read in a single character, use cin to read a value of type char'


http://www.cplusplus.com/reference/istream/istream/get/?kw=cin.get
Last edited on
Ah ok thank you. How can I make it so that if I enter the period/exclamation point after say a phrase or word for it to terminate? Currently, it only works if that is the only thing I enter.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main()
{

	char character;

	cout << "Enter text: " << flush;
	cin.get(character);

	while (character != '.' && character != '!')
	{
		cout << character;
		cin.get(character);
	}

	cin.sync();
	cin.ignore();
	return 0;
}
What are you doing in the loop?
OOH nvm I figured that part out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main ()
{

  char text;
  int count = 0;
  
  cout << "Enter text: " << endl; 

  while (text != '.' && text != '!')
    {
    cin >> text;
     count++ ;
    };

  return (0);
}


This looks right, correct?
Kind of. What value does text hold when line 9 is first encountered?
letters
A variable of type char may hold one value at a time, therefore it does not contain "letters" when line 9 is first encountered. What letter does it contain? Have you specified it?
Topic archived. No new replies allowed.