Thread: Little Help With Strings

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    3

    Little Help With Strings

    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!

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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
    This task is probably simpler than you think it might be. Basically, you keep getting (i.e. a loop!) a line of input using "getline". After each "getline", you check if the string ends with (or contains, or whatever your method) the delimiter "END", probably case-sensitive. If the input ends with this keyword, then you stop looping, otherwise you append this new line of input to your main "chunk" of input, and continue getting another line.

    Once you are out of this loop, you have one giant piece of text (in a "string"). From there you can do whatever you need to do, hopefully it will be straightforward. I imagine the next step is to compute the cost of each word. To do this, you know that a "word" is any string of text follow by a/one or more space(s). So start at character 0 of the string, and find the first " ". Now you have two indices (position 0 and position N, where the first space is found). From this you can find the length of that word, and therefore compute the cost of it. You keep doing this, starting at index (N+1), which is one more than where the previous space was found. Of course there are other methods of doing this, such as "tokenizing", which might be easier to understand.

    Anyway, its always good to be ahead and be wanting more work/exercises. Keep it up.

    EDIT: I just wanted to make this note (my opinion): when working on these exercises, do not worry about the output and making it look pretty, as in the required output in your exercise specification. Worry about how to get that information, as I described above. After that, you have everything you need, and can do anything with it. The output is trivial, so dont ever get confused or worried about it.
    Last edited by nadroj; 01-24-2010 at 12:49 PM.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    3
    Thanks so much for the reply, haha, appreciate it.

    As much as possible I'd like to work on the code myself but I'm seriously a C++ noob, and I'm practically jumping lessons in tackling the string library, because I haven't really mastered this yet.

    So, to know as much as possible without giving away the code completely, could you give me the codes I should use? Specifically for the string library? Same as well with implementing multiline text, I know how to get input but, the word "delimiter" is new to me...haha...Hope you can explain to me some of those things and the codes they're involve in (our textbook is really vague when it comes to these things so it really doesn't help out a lot)

    I imagine the word letter count involves jumping from space to space, but how do I actually count the number of characters within and store it to either int oversized or int regular? using a "for" loop to check each one? Haha, this is the part that really confuses me...

    Or, if you're willing, a sample code that has the concepts of this code, not the actual code itself, would be helpful, haha...Thanks....
    Last edited by MarkBadong; 01-24-2010 at 06:44 PM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    In my previous post above I never even looked at your code. I just took a quick glance over it and notice a few errors (which of course is going to happen when we all start out). Read this very short example of how to use getline. Its pretty straightforward on how to use it. Cprogramming.com FAQ > Obtain a string from the user (C++) (of course read the C++ not C section!).

    Next, if you dont understand loops (for, while) then learn the basics of those first (they are actually straightforward, also), and do not continue with this problem until you have learned those.

    Next, you need to know how to find something in a string, like finding the string "END" in your string that you used with "getline" to get user input. A "delimiter" is basically a special character that means, say, "Ok, this is the end of my sentence". For example, to terminate a sentence, we use the period (".") as the delimiter. For your input string, you are told that the delimiter is "END". So first you read a line of input. Next, search that string for "END". If the input contained this delimiter, you know that the user is done inputting text. If it does not contain "END", then get more input. You can read this, about the "find" function for strings: find - C++ Reference, there is an example at the bottom also.

    Counting the words and all that stuff you can tackle after you get this done. Dont overwhelm yourself by looking at the "big picture"--just do it incrementally, one step at a time, where each next step builds on the other (just like "real" steps on stairs).

    Next: if you dont know about arrays, learn those. Otherwise you will be confused.

    Here is some basic code/pseudocode/English of the solution:
    Code:
    // declare "main" string, named, say, "strMain"
    
    // start an "infinite" loop (i.e. a loop without a condition)
    {
      //declare string named "strTemp"
      // use "getline" to read input, save it to "strTemp"
    
      // search "strTemp" for the string "END" and:
        // if it contains "END", then copy all characters of "strTemp" from index 0 to wherever "E" (as in "END") starts, and append these characters to "strMain", then "break" out of loop
       // otherwise the string doesnt contain "END" and you just append the entire string
    }
    
    // now "strMain" is one giant string, containing all lines of input from the user
    For "appending", read about the string operator here (example at bottom): operator+= - C++ Reference.


    The "if" and "otherwise" part might be confusing. Lets start with an empty string variable "strMain". Suppose the input (variable "strTemp") is
    Code:
    abc\n
    This first line of input doesnt contain "END", so we simply append all of "strTemp" to "strMain" so that "strMain" is now "abc\n". Suppose the next line of input is "def\n", and the same thing happens so "strMain" is now "abc\ndef\n". Now suppose the input is "abENDcd\n". As you can see this contains the delimiter. We find that "END" starts at index 2, of this new line of input stored in "strTemp", so append all characters of "strTemp" from indices 0 to 1, inclusive, to "strMain" (so we copy characters 'a' and 'b'). So that "strMain" now looks like "abc\ndef\nab" (with the implicit terminating character '\0', of course). Since we found "END", we break out of the loop. Now we have one big string and can do whatever calculations with it (for you to figure out!).

    There is also a FAQ entry on this site for "tokenizing", so you can look at that too when you get to that part.

    Anyway, good luck!

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    3
    Nadroj, I can't thank you enough!

    I managed to do it!

    I finished the code!

    I'll post the code soon...

    Thanks so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM