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. |