Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

counting repeated symbols in .txt file - fails,...please help

Status
Not open for further replies.

meInside

Technical User
Apr 24, 2005
1
LV
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, and I can`t find a mistake,...please help!


#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! :)
 
> if(ch=='=');
Yeah, watch those ; at the ends of those statements.
These are (at the moment) just expensive no-ops.

Please use the [tt][ignore]
Code:
[/ignore][/tt]
tags when posting code.

--
 
have you also made sure that there are no white spaces between the characters in the text file?

ie == is not the same as = =
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top