Hi I am in a bit of trouble.

what is nedded:

Reading a text file character-by-character, using the "get(char)" function.
Count the words in the file which contains "w" or "W". The possible delimiters are: '\t','n',' '(space).
Do not use arrays.

The codes:

MAIN.CPP

#include <iostream>
#include "enor.h"

using namespace std;


//Feladat: 	    Kiírjuk a szöveges fájlban a w betűt tartalmazó szavak számát
//Bemenő adatok:szöveges állomány
//Kimenő adatok:statndard output

int main()
{
string hol="input.txt";
int darabw=0;
    Enor t(hol);
    t.First();
    while (t.End())
    {
        t.Next();
    }

    cout<<darabw<<endl;
    return 0;
}

ENOR.CPP

#include "enor.h"

#include <iostream>

using namespace std;

bool wk;
bool nemlimit;
int darabw;
//Feladat: 		Felsoroló létrehozása.
//Bemenő adatok:-
//Kimenő adatok:alapértelmezett felsoroló
//Tevékenység:	Megnyitja a felsoroló hátterében levő szöveges állományt olvasásra

Enor::Enor(const string &str)
{
    f.open(str.c_str());
    if(f.fail()){
        cout << "Nem lehet megnyitni az inputfajlt!\n";
        exit(1);
    }
}

//Feladat: 		A felsoroló Next() művelete.
//Bemenő adatok:alapértelmezett felsoroló
//Kimenő adatok:alapértelmezett felsoroló
//Tevékenység:	Beolvassa a következő karaktert.

void Enor::Next()
{
    char ch;
    while(st==norm){
    vege = st == abnorm;
    if(!vege){

        f.get(ch);
        if ((ch=='w')|| ((ch=='W')&&(wk==false))) {nemlimit=false wk=true, ++darabw;}
        if ((ch==' ') || (ch== '\t') || (ch=='\n')) {nemlimit=false;}



}
    }}

//Feladat: 		A szöveg első karakterének megkeresése, annak feldolgozása.
//Bemenő adatok:alapértelmezett felsoroló
//Kimenő adatok:alapértelmezett felsoroló
//Tevékenység: Az első nem delimiter karakterig olvassa a szöveget.


void Enor::First()
{
        char ch;
        nemlimit=false;
        wk=false;
       while ((ch='\n') && (ch='\t') && (ch='\n') && (f.good()))
        {
            ch=f.get();
            cout<<"delimi"<<'\n';
        };
            {
            if ((ch=='w')|| (ch=='W')) {nemlimit=true; wk=true, ++darabw;}
            }
        if(!f.fail()){
        st = norm;;
    }
    else st = abnorm;
}

ENOR.h

#ifndef ENOR_H
#define ENOR_H

#include <fstream>
#include <string>
#include <sstream>

enum Status { abnorm, norm };




// sorok felsoroló típusa
//  Típusértékek:   felsoroló objektumok
//  Reprezentáció:  szöveges állomány adatfolyam objektuma
//
//  Műveletei:  felsoroló létrehozása
//              felsoroló műveletek
//              a szöveg következő sorának olvasása
class Enor{
    private:
        std::ifstream f;
        Status st;
        bool vege;
        void Read();

    public:
        Enor(const std::string &str);
        void First();
        void Next();
        bool End() const {return st;}
};

#endif

What I'm trying to do:

With t.First() I search for the first not delimiter character and read it. if it is w or W, then the counter goes up.

With t.Next()I read the next character, and if it is w and there wasnt a w before in the word (meaning that wk is false, and nemlimit is true).

Please help me!!

Recommended Answers

All 6 Replies

Ill try to help, but please put CODE tags around your code so I can read it.

Ill try to help, but please put CODE tags around your code so I can read it.

Sorry, i forgot, but narue has done it for me already.

From what I can tell your error might be that

while (t.End())

may never be false as it may only be abnorm or norm, which could be true or false. Try:

while (t.End()!=abnorm)

Of course you will need the End() function to return a Status, not a bool.

From what I can tell your error might be that

while (t.End())

may never be false as it may only be abnorm or norm, which could be true or false. Try:

while (t.End()!=abnorm)

Of course you will need the End() function to return a Status, not a bool.

Would it be okay, if there weren't a Status, but End() would look like this:

bool End() const {return f.eof();}
};

and in Main it would be:

while (t.End())
    {
        t.Next();
    }

hi i am in a bit of trouble.

What is nedded:

Reading a text file character-by-character, using the "get(char)" function.
Count the words in the file which contains "w" or "w". The possible delimiters are: '\t','n',' '(space).
Do not use arrays.

The codes:

main.cpp

#include <iostream>
#include "enor.h"

using namespace std;


//feladat: 	    Kiírjuk a szöveges fájlban a w betűt tartalmazó szavak számát
//bemenő adatok:szöveges állomány
//kimenő adatok:statndard output

int main()
{
string hol="input.txt";
int darabw=0;
    enor t(hol);
    t.first();
    while (t.end())
    {
        t.next();
    }

    cout<<darabw<<endl;
    return 0;
}

enor.cpp

#include "enor.h"

#include <iostream>

using namespace std;

bool wk;
bool nemlimit;
int darabw;
//feladat: 		Felsoroló létrehozása.
//bemenő adatok:-
//kimenő adatok:alapértelmezett felsoroló
//tevékenység:	Megnyitja a felsoroló hátterében levő szöveges állományt olvasásra

enor::enor(const string &str)
{
    f.open(str.c_str());
    if(f.fail()){
        cout << "nem lehet megnyitni az inputfajlt!\n";
        exit(1);
    }
}

//feladat: 		A felsoroló next() művelete.
//bemenő adatok:alapértelmezett felsoroló
//kimenő adatok:alapértelmezett felsoroló
//tevékenység:	Beolvassa a következő karaktert.

Void enor::next()
{
    char ch;
    while(st==norm){
    vege = st == abnorm;
    if(!vege){

        f.get(ch);
        if ((ch=='w')|| ((ch=='w')&&(wk==false))) {nemlimit=false wk=true, ++darabw;}
        if ((ch==' ') || (ch== '\t') || (ch=='\n')) {nemlimit=false;}



}
    }}

//feladat: 		A szöveg első karakterének megkeresése, annak feldolgozása.
//bemenő adatok:alapértelmezett felsoroló
//kimenő adatok:alapértelmezett felsoroló
//tevékenység: Az első nem delimiter karakterig olvassa a szöveget.


Void enor::first()
{
        char ch;
        nemlimit=false;
        wk=false;
       while ((ch='\n') && (ch='\t') && (ch='\n') && (f.good()))
        {
            ch=f.get();
            cout<<"delimi"<<'\n';
        };
            {
            if ((ch=='w')|| (ch=='w')) {nemlimit=true; wk=true, ++darabw;}
            }
        if(!f.fail()){
        st = norm;;
    }
    else st = abnorm;
}

enor.h

#ifndef enor_h
#define enor_h

#include <fstream>
#include <string>
#include <sstream>

enum status { abnorm, norm };




// sorok felsoroló típusa
//  típusértékek:   Felsoroló objektumok
//  reprezentáció:  Szöveges állomány adatfolyam objektuma
//
//  műveletei:  Felsoroló létrehozása
//              felsoroló műveletek
//              a szöveg következő sorának olvasása
class enor{
    private:
        Std::ifstream f;
        status st;
        bool vege;
        void read();

    public:
        Enor(const std::string &str);
        void first();
        void next();
        bool end() const {return st;}
};

#endif

what i'm trying to do:

With t.first() i search for the first not delimiter character and read it. If it is w or w, then the counter goes up.

With t.next()i read the next character, and if it is w and there wasnt a w before in the word (meaning that wk is false, and nemlimit is true).

Please help me!!

please someone help!!!!

Calm down... the answer to your question is to TRY IT! if you try it you will find that either it works... or it doesn't.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.