I have a text file with a few cars brands and some information for the cars. I want the cars together with the information for the cars to be sorted in alfabetic order.

Anyone can help me to get this working?

This is my code:

#include<fstream>
#include<iostream>
#include<string>
#include<conio.h>
#include<iostream>
using namespace std; 
struct dvd
{
char modell[30];
int sold;
int notsold;
int buy;
int sell;
};
int main( )
{
  dvd bilinfo[5]; //making a list of dvd
  char soldvektor[4];char notsoldvektor[4];char buyvektor[9];char sellvektor[9];
  char sort[30];
  int i, antal=0;
  ifstream lasa("textfile.txt ");
  
  if (!lasa){cout<<"Could not find the file:";}

  while( lasa.getline(bilinfo[antal].modell, 30)) //Reading the textfile
	{
	  lasa.getline(soldvektor,4);        
	  bilinfo[antal].sold = atoi(soldvektor);
	  lasa.getline(notsoldvektor,4);
	  bilinfo[antal].notsold = atoi(notsoldvektor);
	  lasa.getline(buyvektor,9);
	  bilinfo[antal].buy = atoi(buyvektor);
	  lasa.getline(sellvektor,9);
	  bilinfo[antal].sell = atoi(sellvektor);
	  antal++;
	}
  lasa.close();

for (i=0;i<antal-1;i++)  //Sorting the names
{
    for (int j=i+1; j < antal; j++) 
    {
       if ( strcmp(bilinfo[i].modell, bilinfo[j].modell) > 0)
       {
            dvd temp;
            strcmp(temp, bilinfo[i]);
            strcmp(bilinfo[i], bilinfo[j]);
            strcmp(bilinfo[j], temp);
       }
   }
}
      
  for(i=0;i<antal;i++) //Writing out
	{
	  cout<<"type of car: "<<bilinfo[i].modell<<endl;
	  cout<<"cars in store: "<<bilinfo[i].sold<<endl;
	  cout<<"sold cars: "<<bilinfo[i].notsold<<endl;
	  cout<<"purchase price: "<<bilinfo[i].buy<<endl;
	  cout<<"selling price: "<<bilinfo[i].sell<<endl<<endl;
	}
getch();
return 0;
}

This is how the text file looks like:

ford
6
3
40000
55000
volvo
10
4
70000
85000
chrysler
0
15
30000
50000
volkswagen
2
3
60000
75000
peugeot
20
0
45000
55000

This is how I want the program to write it out:

chrysler
0
15
30000
50000
ford
6
3
40000
55000
peugeot
20
0
45000
55000
volkswagen
2
3
60000
75000
volvo
10
4
70000
85000

std::map<std::string,dvd> car_info;

std::map will sort this array automatically based on the < operator for std::string

std::map["volvo"] = dvd(constructor values); // or whatever temporary dvd you make

How would I implement this to the code?
I'm sorry I have no experience at all working with std::string

Managed to get it working, thanks.

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.