URGENT!!! Write a program to input a sentence, one word at a time, terminated by one of these three characters: . ! ? etc

I have a question. But I don't know how to do all of it. I got this so far. I dont know how to make it end with more than just ! and I dont know how to make it count the words and biggest and smallest and such.
#include <iostream>
#include <string>
using namespace std;

int main()
{
string sentence;
int len;
cout << "Enter a sentence, terminated by !, ., or ?";
getline (cin, sentence, '!');
len = sentence.length();

cout << sentence << endl;
cout << "NUmber of letters entered"<< len;
return 0;
}
Here is the question
Write a program to input a sentence, one word at a time, terminated by one of these three
characters: . ! ? Then, output the number of words in the sentence, the average word length,
and the longest word in the sentence (for ties, display the last one). For simplicity, assume that the
only punctuation marks within the sentence are one of the two characters , ; where each
punctuation mark is separated from the preceding word by a space.
Note: The user may enter the sentence over more than one input line.
Here is an example of what output should look like from running your program (user input is shown
in bold):
Enter a sentence:
Alas , there was no more fuel ; we began
losing altitude rapidly !
Number of words = 11
Average word length = 4.54545
Longest word: altitude
Hints:
• Use a string class variable to read in each word and each punctuation mark. Since each
word and each punctuation mark is separated by a space, use cin to read in the words and
punctuation marks.
• Use the string variable’s length() function to determine the length of a word (see the
example program length.cpp, in the part 5 folder of my program examples).
• Use relational operators to determine when punctuation has been read.
Last edited on
a question for my coding class But I don't know how to do all of it

Ok, have you read here?
http://www.cplusplus.com/forum/beginner/1/
The title says: “read before posting”.
Let me quote from it:
Don't post homework questions
Programmers are good at spotting homework questions; most of us have done them ourselves. Those questions are for you to work out, so that you will learn from the experience. It is OK to ask for hints, but not for entire solutions.


- - -
Hints:
• Use a string class variable to read in each word and each punctuation mark. Since each word and each punctuation mark is separated by a space, use cin to read in the words and punctuation marks.

I got this so far

getline (cin, sentence, '!');

What about taking into any consideration the hints you’ve already got before asking for more?
getline (cin, sentence, '!');

^^ This is not correct.
the user types in "no bang here." ... valid, as it ends in a '.' but your code does not stop reading because the user did not type !

the approach is wrong.

I think they want a loop, possible this kind of formatting:
do
cin >> word;
total/whatever /statistics stuff here
while(word[word.length()-2] != '.' || ...); //look at the last letter of the word for the 3 symbols to know when to quit.

one of the things you need to learn early on is difference between << and getline and when to use each, the risks of mixing the two carelessly (see thousands of posts here on that one), and how to do basic string processing. The last letter in the word can be fetched a variety of ways, the one I gave here is crude but easily understandable.
Last edited on
the last letter would be word[word.length()-1] or simply word.back()
heh, I C. I corrected for a terminal zero, and there isnt one on string, of course... I am not sure where I managed to come up with whatever I did there. Sorry!

Last edited on
> I corrected for a terminal zero, and there isnt one on string,
I would say that for having a constant .c_str(), std::string must be zero terminated (perhaps no for an empty string)
still, .size() or .length() don't count that extra character
hold up everyone. Thanks for replying so quickly. could you guys show me like in code for what it should look like?
cin >> s;
if(s.back() == '.' || s.back() == '?' || s.back()=='!') //if the last letter is one of the 3
{
int ct = 0; //we will count the non space letters.
for(auto i : s) //for all the letters
if(i != ' ') ct++; //if not a space, count them
}
I got the error 'std::string' has no member named back.
#include <iostream>
#include <string>
using namespace std;

int main()
{
string s, sentence;
int i;
cout << "Enter a sentence, terminated by !, ., or ?";
cin>> s;
if(s.back() == '.' || s.back() == '?'|| s.back() == '!');
{int ct = 0;
for(auto i : s);
if(i != ' ') c++;
i=sentence.length();---scratch this we havent gotten to fixing this
cout << sentence << endl;
cout << "Number of letters entered"<< i;
}
return 0;
}
Could you please solve the problem in a c++shell and then I can ask questions about parts I still don't understand?
Last edited on
here. I fixed a couple of little things. It counts ! as a letter because we only ignored spaces, but you can clean up the little things. isalpha() may be helpful there.

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

int main()
{
string s, sentence;
int i;
cout << "Enter a sentence, terminated by !, ., or ?";
//cin>> s;
getline(cin,s);
if(s.back() == '.' || s.back() == '?'|| s.back() == '!');
{int ct = 0;
for(auto i : s)
if(i != ' ') ct++;
//i=sentence.length();---scratch this we havent gotten to fixing this
cout << s<< endl;
cout << "Number of letters entered "<< ct;
}
return 0;

}


some of your trouble is you are reading a word at a time with << instead of a whole line (getline). I fixed this.

back() is c++2011 or later compilers. If you have a 10 year old compiler, it will throw the error you said. You should get something new, if this happens. You may also just need a flag to enable 2011 additions (in which case, use at least the 2017 flag to get everything else while you are at it).
Last edited on
Ok thanks. How would we then have it count number of words instead of letters but then be able to cout the longest word and how many letters it has and average the word lengths?
you break it up at the spaces and count. iterate all the letters in the string, count things (word length, num words) and stow things for later (longest word and its length). when done looking at all the letters, you have all the data... longest word, its length, number of words, total length of all words, divide to average...

I already iterated all the letters in the string in a loop for you. Extend that with more variables to do this stuff.
Last edited on
this is line 13 14 and 15 I just have to change variables for each thing? or is it just line 16 that I make copies of. and would I have to make another variable like s for each of them
Last edited on
http://cpp.sh/6yqsm
Should we use this code to save each works length into a variable?
I give up this is due tomorrow and I haven't figured it out.
Stop putting URGENT!!! in front of your posts you doo-doo head.
Last edited on
Ok buddy.
I put Urgent in front of two of them not the third dumdum.
Last edited on
Registered users can post here. Sign in or register to post.