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>
usingnamespace 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.
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'
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.
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?