hi, you all. I am a beginner c++ programmer.
My teacher, has given me, this exercise in c++ language.
file input: names
output program:
this is my code:
but it do not run very well.....
My teacher, has given me, this exercise in c++ language.
Read from a file the names, print them and their position. So, print, how many times, a name is presents into a file.
Carlo
Antonio
Marco
Antonio
Carlo
Carlo
Marco
Antonio
Marco
Antonio
Carlo
Carlo
Marco
list names:
Carlo 0
Antonio 1
Marco 2
Antonio 3
Carlo 4
Carlo 5
Marco 6
Carlo is present: 3 times
Antonio is present: 2 times
Marco is present: 2 times
Carlo 0
Antonio 1
Marco 2
Antonio 3
Carlo 4
Carlo 5
Marco 6
Carlo is present: 3 times
Antonio is present: 2 times
Marco is present: 2 times
Code:
#include<iostream> #include<fstream> #include<string.h> using namespace std; int main() { ifstream file; ifstream f; char name[1024],name_new[1024]; int i,pos=0,occ=0; file.open("names",ios::in); while(!file.eof()) { file>>name; if(name[1024]!='\n') { cout<<name<<"\t"<<pos<<endl; pos++; } } cout<<endl; file.close(); file.open("names",ios::in); f.open("names",ios::in); for(i=0;i<pos;i++) { file>>name; for(i=0;i<pos;i++) { f>>name_new; if(strcmp(name,name_new)==0) { occ++; } } cout<<name<<"\t"<<occ<<endl; } file.close(); f.close(); return 0; }
Comment