HI Guys,

i am trying to develop a c++ programm, that accept a path to a .txt file from the user to read it, make it lowercase, sort it and give the output in the concole. and some other functions later...

Now my problem is, that i dont know how to put my text from the string into an Array and make a lowercase function for it, and at least give the output in the console.

Thas my code:

#include <iostream>
#include <fstream>              				
#include <string>
#include <conio.h>
using namespace std;

int main(int argc, char* argv[])
{
	
	string line;                         
	string path;
        cout<<"here is the input of the path:\n\n";
	cin>>path;                           
	getchar();
	ifstream myfile (path.c_str());
	if (myfile.is_open())                                
	{
		while( getline( myfile, line)){    
		cout<<""<<endl;
		cout << line << endl;                
		}
		myfile.close();                            
	}

	else cout << "Unable to open file";         
	getchar();
	return 0;

}

Recommended Answers

All 10 Replies

to convert line to lower case transform(line.begin(),line.end(), tolower); To put the string in an array

vector<string> array;
...
array.push_back(line);

thank you very much, i did it like this and it works fine ;)

#include <cctype>
#include <algorithm>

.
.
transform(line.begin(), line.end(), line.begin(), (int(*)(int)) tolower);

So my other and last problem is now that i have to sort the inputed text from the .txt file ;(
Numbers and interpunctions like " , . : " have to be ignored. The sort Output have to catalog the first 2 letters of each word. In this group the output have to be in in alphanumeric code reversed order. double words have to be shown once with the frequency of occurrence.

Examble Output of a Text:

ag - ag
di - die
ei - eine
fi - firmenadresse, firma
ga - gate
ha - hamburg (2)
in - in
is - ist (2)
va - vanilla

Anyone know how i can realize this or give a tip how to start with coding, maybe with pointers and sort function??

THX

Member Avatar for iamthwee

Get out some paper and plan it first.

hmm maybe with vector classes??

I am hanging on this point, somebody a idea ??

thx

Member Avatar for iamthwee

In all honesty, we could show you the exact code to do this. But it would be pointless.

You must try it yourself first.

okay thats fair, i am tyring to handle by myself first.. thx

With the example text you posted you don't have to worry about the punctuation -- just sort the lines normally and they will all come out ok std::sort( array.begin(), array.end()) will do the trick, unless of course you are required to write your own unique sort algorithm.

@ Ancient Dragon

thanks very much for your help, i will try it...

i dont know how to write my own unique sort algo now, but i am searching my c++ lecture...I am new on the Area and its a test from a firm where i like to study...

thx again...

So maybe like this? but i dont get some positive result ;(

#include <cctype>			
#include <algorithm>
#include <iostream>
#include <fstream>              	
#include <string>
#include <conio.h>
#include <map>
using namespace std;
 
int main(int argc, char* argv[])
{

   string line;                          
   string path;  			     
   cout<<"Bitte geben Sie den Quellpfad der Text Datei an:\n\n";
   cin>>path;                           
   getchar();
 
   ifstream myfile (path.c_str());
   if (myfile.is_open())                 
     
      {
	while( getline( myfile, line)){
	transform(line.begin(), line.end(), line.begin(), (int(*)(int)) tolower);
	cout<<""<<endl;
	cout << line << endl;            
        }
	myfile.close();                  
}
 
	else cout << "Datei konnte nicht geoeffnet werden!";
	getchar();
	return 0;
 
}
 
	int sort() {
	typedef map<string,int> MapType;
	MapType count;
	string in;char c;
 
	while(cin.read(&c,1)) {
		if(isalpha(c))
			in+=c;
		else if(!in.empty()) {
			++count[in];
			in.clear();
		}
	}
 
	for(MapType::iterator i=count.begin(),j;i!=count.end();i=j) {
	char next_key[]={i->first[0],i->first.size() > 1 ? i->first[1]+1 : '_'};
	j=count.lower_bound(next_key);
	cout<<string(i->first,0,2)<<" - ";
	MapType::iterator k=j;--k;
	do {
	cout<<k->first;
	if(k->second>1)
	cout<<" ("<<k->second<<")";
	if(k!=i)
	cout<<", ";
	} while(k--!=i);
	cout<<endl;
	}
}

I can see why that doesn't compile -- I don't have a clue what its trying to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.