Hey everyone,
it's my first thread here...I'm taking up C++ programming and am very much a beginner...But I want to learn more...
My tutor gave me several assignments and I was able to answer them all so far, however he told me to start trying some of the programs in my handbook and this is one that stumped me...
"Input to a program represents the text of a telegram. The input consists of one or more lines containing a number of words each separated by a number of spaces. The unique word ‘END’ terminates the input. Produce a bill for this telegram with each word costing 10 cents and an eight letters long. The output is to appear as:
Number of words : 23
Number of normal-sized words : 19 at 10 is 1.90
Number of oversized words : 4 at 15 is 0.60
Total : 2.50"
I've been working on a code but so far it's all messed up...
I use the string library, and tried the getline(cin, mystring) code...
I don't know how to make multiple lines of strings yet, let alone end the input if the word END is written, because as far as I am in c++ I only know so far that a string input ends if enter is pressed...
For counting the number of words, I've tried using this code:
Code:
#include<iostream>
#include<string>
using namespace std;
int main()
{
getline(cin, teleg);
int l = teleg.length();
int oversized = 0;
int regular = 0;
for (int n=8; n < l; n++)
{
if (teleg[n] != ' ' and teleg[n+1] = ' ')
//check if character is not a space and is followed by a space, it means this is the last letter of a word, so far starting with character 8 of string, until I can add 8 spaces before the string, which I don't know how to do yet....
{if (teleg[n-1] != ' ' && (teleg[n-#] until) teleg[n-8] != ' ')
{oversized++;}
//check to see if the first 8 characters before the end of the word are not spaces, if so then it means it's oversized and will add to oversized count
{if (teleg[n-1] == ' ' || (teleg[n-#] until) teleg[n-8] == ' ')
{regular++;}
//check if any of the characters before the end of the word is a space, then it adds to regular word count
This is what I've been thinking of so far, but I can't get it to work properly, it does count 8+ character words but not the less than 8 ones, and it still can't take multiple lines, and I'm not sure how I end input with the word END...
Any help would be appreciated, I want to finish this code so I can be prepared for the coming codes...Thanks!