Here's the deal. I need program to read from text txt file and then write numbers into one file and letters into the other...
I figured how to read and write to file (using iterators) but I can't figure out how to make the program sort numbers from letters.

#include "stdafx.h"
#include <iostream>
#include <ostream>
#include <fstream>
#include <list>       using list container is required
#include <string>
using namespace std;

template <typename T>
class Zavd{
int n;
list <T> data; char c;
public:
    Zavd(int n1=0);
    void  Read (); 
    void Write();
    void Numbers();
    void Letters();
    //~Zavd();
};
template <typename T> Zavd <T>::Zavd(int n1)
{n=n1;
}
template <typename T> void Zavd <T>::Write() 

{
ofstream flout("Out.txt");
flout<<"Output:\n";

list <char>::iterator p1;
p1=data.begin();
for (p1=data.begin(); p1!=data.end(); p1++)
{
    flout<<" "<<*p1<<endl; }

flout<<"\n";

flout.close();
}


template <typename T> void Zavd <T>::Read()
{
ifstream flin("In.txt");

flin>>n;
for  (int i=0; i<n; i++)
{flin>>c;
data.push_back(c);
}

//data.pop_front();
flin.close();
}
//template <typename T> Zavd <T> ::~Zavd

template <typename T> void Zavd<T>::Numbers()
{int i;
list<char>::iterator p2;
//data.sort();


{cout<<"number"<<endl;}
for(p2=data.begin(); p2!=data.end(); p2++)
{
    {if( data.back()==4)
{cout<<"number"<<endl;
    } }

    cout<<*p2<<endl;
}

}
int _tmain(int argc, _TCHAR* argv[])
{
Zavd <char> A;
A.Read();
A.Numbers();
A.Write();


system ("pause");
    return 0;
}

Usage of list container is required.
any suggestions??

Recommended Answers

All 4 Replies

**PLEASE USE CODE TAGS**

If you sort the list, using "data.sort()". It will put all the characters in order of their binary representation which you can get from the ASCII table. You will notice that all numbers are between character #48 and #57. Any character before or after is a "letter" (or punctuation or something else). So all you need to do is print all the characters in order as letters until you find the '0' character, than print the numbers until you hit any character greater than '9' and then you can just print the rest as letters.

PLEASE USE CODE TAGS

If you sort the list, using data.sort(). It will put all the characters in order of their binary representation which you can get from the ASCII table. You will notice that all numbers are between character #48 and #57. Any character before or after is a "letter" (or punctuation or something else). So all you need to do is print all the characters in order as letters until you find the '0' character, than print the numbers until you hit any character greater than '9' and then you can just print the rest as letters.

OK I get it.
But how do I access characters within the container?

like

for (p2=data.begin(); p2!=data.end(); p2++)
{
    if (data.sort()=='48')

won't work...

do I need lile an extra array?

OK I get it.
But how do I access characters within the container?

like

for (p2=data.begin(); p2!=data.end(); p2++)
{
    if (data.sort()=='48')

won't work...

do I need lile an extra array?

OH!!
I think I got it!

Thanks for your answer,

Here's the working programm...
just in case somebody would need it........

#include "stdafx.h"
#include <iostream>
#include <ostream>
#include <fstream>
#include <list>
#include <string>
using namespace std;

template <typename T>
class Zavd{
int n;
list <T> data; char c; char **mas;
public:
    Zavd(int n1=0);
    void  Read (); 
    void Write();
    void Numbers();
    void Letters();
    //~Zavd();
};
template <typename T> Zavd <T>::Zavd(int n1)
{n=n1;
}
template <typename T> void Zavd <T>::Write() 

{
ofstream flout("Out.txt");
flout<<"Output:\n";

list <char>::iterator p1;
p1=data.begin();
for (p1=data.begin(); p1!=data.end(); p1++)
{
    flout<<" "<<*p1<<endl; }

flout<<"\n";

flout.close();
}


template <typename T> void Zavd <T>::Read()
{
ifstream flin("In.txt");

flin>>n;
for  (int i=0; i<n; i++)
{flin>>c;
data.push_back(c);
}

//data.pop_front();
flin.close();
}
//template <typename T> Zavd <T> ::~Zavd


template <typename T> void Zavd<T>::Numbers()
{int i; int nomer;
ofstream flout("Numbers.txt");
flout<<"Output:\n";

list<char>::iterator p2;
p2=data.begin();
data.sort();
    cout<<"cufru"<<endl;
for(p2=data.begin(); p2!=data.end(); p2++)
{
    if ((*p2>=48)&& (*p2<=57))
    {
        cout<<*p2<<endl;
        flout<<" "<<*p2<<endl; 
        flout<<"\n";}
//flout.close();
}
}

template <typename T> void Zavd<T>::Letters()
{int i; int nomer;
ofstream flout("Letters.txt");
flout<<"Output:\n";
flout<<"Bykvu:"<<endl;
list<char>::iterator p3;
p3=data.begin();
data.sort();
cout<<"<<<<<<<<<<<>>>>>>>>>"<<endl;
cout<<"bykvu"<<endl;

for(p3=data.begin(); p3!=data.end(); p3++)
{
    if ( (*p3>57) )
    {   cout<<*p3<<endl;
        flout<<" "<<*p3<<endl; 
        flout<<"\n";}
//flout.close();
}

}

int _tmain(int argc, _TCHAR* argv[])
{
//cout<<
Zavd <char> A;
A.Read();
A.Numbers();
A.Letters();
A.Write();
system ("pause");
    return 0;
}
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.