CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2016
    Posts
    20

    Number of middle symbols

    Hi there. I have a problem. I don't know how to count the number of the middle letters of the words in the sequence. I have to do it from a file and numbers counts as a word and any punctuation as a space. I have now only program which prints me a sequence, can someone help?

    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    #include <fstream>
    #include <string>
    using namespace std;
    const char FileName[] = "bandymas.txt";
    int main ()
    {
        string line;
        ifstream Myfile (FileName);
        int c;
    
        if (Myfile.is_open())
        {
            while( getline (seka, line))
            {
                cout<<line<<endl;
                c++;
            }
        }
        seka.close();
    
        return 0;
    }
    Last edited by 2kaud; November 5th, 2016 at 12:11 PM. Reason: added code tags

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Number of middle symbols

    When posting code, please use code tags. Go Advanced, select the formatted code and click '#'.

    As posted, there is a problem with the code. What is seka? Also you are using c without first initialising it - not a good idea!

    If there is more than 1 word per line, then getline() will obtain all of the words per line. You'll then need to parse this line into words. As punctuation counts as a space then using stream extraction won't work so you will need to examine the line read to obtain the words. Once you have the word then its easy to obtain the required number. I suggest you define a string that contains the characters to be treated as a space and use .find_first_of() to obtain the position of the next space. Then using .substr() the required word can be extracted. This is repeated until no more spaces in the line are found.

    See http://www.cplusplus.com/reference/string/string/ for info about the string class functions.
    Last edited by 2kaud; November 5th, 2016 at 12:14 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured