Herzlich Willkommen, lieber Gast!
  Sie befinden sich hier:

  Forum » C / C++ (ANSI-Standard) » counting repeated symbols in .txt file - fails,...please help

Forum | Hilfe | Team | Links | Impressum | > Suche < | Mitglieder | Registrieren | Einloggen
  Quicklinks: MSDN-Online || STL || clib Reference Grundlagen || Literatur || E-Books || Zubehör || > F.A.Q. < || Downloads   

Autor Thread - Seiten: > 1 <
000
24.04.2005, 15:52 Uhr
~DumbInside
Gast


Hello! Newbie in C++ needs help!
I have to write a program, that counts how many times do these symbols (==, !=, <=, > repeat in .txt document. So I wrote a code, but it calculates wrong,..it fails if the data.txt consists symbols like =='=' it counts it two == , but it shuould count just one...


C++:
#include <conio.h>
#include <fstream>
#include <iostream>

using namespace std;
int main()
{
   cout<<"Program counts how many times repeats == !=  <= >= in data.txt document "<<endl;
   char ch;
   int sk1,sk2,sk3,sk4;
   sk1=sk2=sk3=sk4=0;
   ifstream file("data.txt");


   if(!file)
   {
       cout<<"File not found!";
   }
   else
   {
      while(!file.eof())
      {


         //==
            file.get(ch);
             if(ch=='=')
             {
               file.get(ch);
               if(ch=='=');
                 sk1++;
             }

         //!=
             else if(ch=='!')
             {
                 file.get(ch);
                 if(ch=='=');
                   sk2++;
             }
         //<=
             else if(ch=='<')
             {
                 file.get(ch);
                 if(ch=='=');
                   sk3++;
             }
         //>=
             else if(ch=='>')
             {
                 file.get(ch);
                 if(ch=='=');
                   sk4++;
             }
   }
       cout<<endl;
       cout<<"Symbols '==' repeat "<<sk1<<" times!"<<endl;
       cout<<"Symbols '!=' repeat "<<sk2<<" times!"<<endl;
       cout<<"Symbols '<=' repeat "<<sk3<<" times!"<<endl;
       cout<<"Symbols '>=' repeat "<<sk4<<" times!"<<endl;
}
   file.close();
   getch();
   return 0;
}



Thank you!

mod edit: use the CPP Tags

Dieser Post wurde am 24.04.2005 um 16:58 Uhr von Pablo editiert.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
001
24.04.2005, 16:32 Uhr
CaesarCortezz
minderer Student


if you want to count the "==" symbols you have to add a whitespace after these symbol.

like this:


C++:
if(ch=='== ')



or like you do:

C++:
file.get(ch);
if(ch=='=')
{
file.get(ch);
if(ch=='=');
file.get(ch);
if(ch==' ');
sk1++;
}



just a suggestion
--
Thus spake the master programmer:

``When the program is being tested, it is too late to make design changes.''

Dieser Post wurde am 24.04.2005 um 16:32 Uhr von CaesarCortezz editiert.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
002
24.04.2005, 17:50 Uhr
~DumbInside
Gast


thanks for idea,...but the problem still exsists,....

=='=' counts as 2 == symbols
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
003
24.04.2005, 20:40 Uhr
CaesarCortezz
minderer Student


Just one question about your text file. are there whitespaces after the symbols you search for?
if yes i would suggest to use the string library. then you could read in one whole word like == and look for single ='s in another way.
hope my english was not too poor ^^
--
Thus spake the master programmer:

``When the program is being tested, it is too late to make design changes.''
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
004
24.04.2005, 20:46 Uhr
FloSoft
Medialer Over-Flow
(Administrator)


i would try to use following:

search for a =, if the next character is a =, increment ==count. if not, increment =count.
--
class God : public ChuckNorris { };

Dieser Post wurde am 24.04.2005 um 20:46 Uhr von FloSoft editiert.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
005
24.04.2005, 20:58 Uhr
0xdeadbeef
Gott
(Operator)


I'd do it like this:

C++:
#ifdef WIN32
#include <cstdlib>
#endif

#include <fstream>
#include <iostream>

using namespace std;

namespace {
  char const *const symbols[] = { "==", "!=", "<=", ">=" };
  int const num_symbols = sizeof(symbols) / sizeof(symbols[0]);
}

int main() {
  char c[2];
  int sk[4] = { 0 };
  ifstream file("data.txt");

  if(!file) {
    cout << "File not found!" << endl;
    return 1;
  }

  file.get(c[0]);
  for(int ix = 1; file.get(c[ix]); ix = 1 - ix) {
    for(int i = 0; i < num_symbols; ++i) {
      if(c[ix] == symbols[i][1] && c[1 - ix] == symbols[i][0]) {
        ++sk[i];
        ix = 1 - ix;
        file.get(c[ix]);
        break;
      }
    }
  }

  cout << endl;
  for(int i = 0; i < num_symbols; ++i) {
    cout << "Symbols '" << symbols[i] << "' repeat " << sk[i] << " times!" << endl;
  }

#ifdef WIN32
  system("pause");
#endif

  return 0;
}


The arrays make accessing easier, the code smaller and thus more maintainable. The algorithm basically gets two characters from the stream, then compares them to the symbols. If a match is found, the next character is skipped (this eliminates your === problem), and the loop continues with the characters after that.
--
Einfachheit ist Voraussetzung für Zuverlässigkeit.
-- Edsger Wybe Dijkstra

Dieser Post wurde am 24.04.2005 um 21:04 Uhr von 0xdeadbeef editiert.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
006
24.04.2005, 23:24 Uhr
~DumbInside
Gast


Thank you all guys, you really helped me alot,...
I like the idea about arrays,..

Thank you again

U R the greatest
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
Seiten: > 1 <     [ C / C++ (ANSI-Standard) ]  


ThWBoard 2.73 FloSoft-Edition
© by Paul Baecher & Felix Gonschorek (www.thwboard.de)

Anpassungen des Forums
© by Flo-Soft (www.flo-soft.de)

Sie sind Besucher: