-
Need Help
Hello all I have a few problems with my code
Main.CPP
Code:
int main()
{
string hol="input.txt";
int darabw=0;
Enor t(hol);
t.First();
cout<<darabw;
t.Next();
cout<<darabw<<endl;
return 0;
}
ENOR.CPP
Code:
#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;
int i=0;
i=i+1,
f.get(ch);
if ((ch=='w')|| ((ch=='W')&& (wk==false))) {nemlimit=true; wk=true; ++darabw;}
if ((ch==' ') || (ch== '\t') || (ch=='\n')) {nemlimit=false; wk=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;
f.get(ch);
while(((ch==' ') || (ch=='\t') || (ch=='\n')) && (f.good()))
{
ch=f.get();
cout<<"delimit"<<'\n';
};
cout<<endl<<ch;
{
if ((ch=='w')|| (ch=='W'))
{
nemlimit=true;
wk=true;
++darabw;
cout<<darabw;
}
}
}
The First() works fine. One problem: when in main() I use cout<<darabw, it gives 0 with my input file, when it should give 1. It gives 1 if I write it at the end of First.
-
There are two variables named darabw. I think you are confused about which of them is being updated.
-
Thanks, but what should i do?
I have to use it, to write what I need to, and otherwise it sais:
main.cpp|24|error: `darabw' was not declared in this scope|
So what i want to ask is, where should i put "int darabw"
-
Do you have an enor.h file? darabw can be declared there.
Code:
#ifndef ENOR.H
#define ENOR.H
extern int darabw;
#endif
Then darabw would be defined in enor.cpp like it is now, and main.cpp can include enor.h to use it.
-
It's not working, if I do that, than : main.cpp|24|undefined reference to `_darabw' error is what i get.
-
How are you building these files?
-
Code:
#ifndef ENOR_H
#define ENOR_H
#include <fstream>
#include <string>
#include <sstream>
// 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
extern int darabw;
class Enor{
private:
std::ifstream f;
void Read();
public:
Enor(const std::string &str);
void First();
void Next();
bool End() {return f.eof();}
};
#endif
This is enor.h
This is enor.cpp
Code:
#include "enor.h"
#include <iostream>
using namespace std;
bool nemlimit;
bool wk;
//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;
int i=0;
i=i+1,
f.get(ch);
if ((ch=='w')|| ((ch=='W')&& (wk==false))) {nemlimit=true; wk=true; ++darabw;}
if ((ch==' ') || (ch== '\t') || (ch=='\n')) {nemlimit=false; wk=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;
darabw=0;
nemlimit=false;
wk=false;
f.get(ch);
while(((ch==' ') || (ch=='\t') || (ch=='\n')) && (f.good()))
{
ch=f.get();
cout<<"delimit"<<'\n';
};
cout<<endl<<ch;
{
if ((ch=='w')|| (ch=='W'))
{
nemlimit=true;
wk=true;
++darabw;
cout<<darabw;
}
}
}
And this is main.cpp
Code:
#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";
Enor t(hol);
t.First();
cout<<darabw;
t.Next();
cout<<darabw<<endl;
return 0;
}
-
You removed the definition of darabw from enor.cpp. Now it does not exist anywhere at all. Add it back to enor.cpp like in your first post.
-
-
Second problem
I am sorry to say, that that was only one half of my problems.
Next() doesn't even run once, why is it so?
-
Does your file begin with a 'w' or 'W' character?
-
The Input file: wewwew wert sddft ass
The program is at the first 'w' right now.
-
Then I do not know why Next() does not appear to run. Have you run your code through a debugger?
-
and wk is true right now. which says that we had already found a w or W in this word.
-
Oh, I see. In First() you set wk to true, but the case for darabw to increment in Next() depends on wk being false.