I have problem completing a program and I would be really really thankful if someone could help me with this .

This is my code so far:

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

void registret(void);
void lista1(void);
void lista2(void);
void search(void);
void sortering(void);
void addcar(void);
void sell(void);

struct bil
{
	char modell[30];
	char color[15];
	int saleprice;
	int retailprice;
	int sold;
	int instock;
};

int main()
{
	lista1();
	return 0;
}

void registret(void)
{
    char  vekt[50][99];
	ifstream  lasa("bilreg.txt");

	int antal=0;
	while( lasa.getline(vekt[antal], 99))
	{
		antal++;
	}
	for( int i=0;i<antal;i++)
		cout<<vekt[i]<<endl;
	cout<<endl;
	_getch();
}

void lista1(void)
{
	int val;
 cout<<"0. Avsluta "<<endl;
 cout<<"1. Se registret "<<endl;
 cout<<"2. S\x94ka/Sortera "<<endl;
 cout<<"3. S\x84lj"<<endl;
 cout<<"4. L\x84gg till ny bilmodell"<<endl;
 cout<<"Ange ditt val: ";
 cin>>val;
 cout<<endl;

	switch(val)
	{
	case (0): cout<<"Programmet avslutas!"<<endl;
		break;
	case (1): registret();
		break;
	case (2): lista2();
		break;
	case (3): sell();
		break;
	case (4): addcar();
		break;
	default: cout<<"Valet finns ej"<<endl;
	}
}

void lista2(void)
{
int val2;
cout<<"21. S\x94ka"<<endl;
cout<<"22. Sortera"<<endl;
cout<<"23. Se registret"<<endl;
cout<<"24. \x86ter till huvudmenyn"<<endl;
cout<<"Ange ditt val: ";
cin>>val2;
cout<<endl;

switch(val2)
{
case (21): search();
		break;
case (22): sortering();
		break;
case (23): registret();
		break;
case (24): lista1();
		break;
default: cout<<"Valet finns ej"<<endl;

_getch();
}
}

void search(void)
{
   cin.ignore();
   char  vekt[30];
   char namn[30];
   bool ok=false; 
   cout <<"Ange en modell: ";
   cin.getline(namn,30);

   ifstream   lasa("bilreg.txt");
   if (! lasa)
   {
	   cout<<"kunde inte hitta filen:";
   }
 
   while( lasa.getline(vekt, 30))
   {
             
        if (strcmp (vekt,namn) == 0)
          {
	      ok=true;
                  break;
          }

   }
   if (ok)
         cout<<"Modellen finns i registret!"<<endl;
   else
         cout<<"Modellen finns inte med i registret!"<<endl;	

   lasa.close();

}

void sortering()
{

}

void addcar(void)
{
 cin.ignore();
 bil reg;

  
   ofstream utskrift("bilreg.txt", ios::app);
   if(!utskrift)
   {
      cout<<"Filen kunde inte hittas"<<endl;
   }

   for(int i=0;i<1;i++)

     { 
        cout<<"Ange modell:" ;
        cin.getline(reg.modell,30);
		cout<<"Ange f\x84rg: ";
		cin.getline(reg.color,15);
        cout<<"Ange antal i lager :";
        cin>> reg.instock;
		cout<<"Ange antal s\x86lda: ";
		cin>>reg.sold;
		cout<<"Ange s\x84ljpris: ";
		cin>> reg.saleprice;
		cout<<"Ange ink\x94pspris: ";
		cin>> reg.retailprice;
		cin.get();
		cout<<endl;
     utskrift<< reg.modell <<" ";
	 utskrift<< reg.color <<" ";
     utskrift<< reg.instock<<" ";
	 utskrift<< reg.sold<<" ";
	 utskrift<< reg.saleprice<<" ";
	 utskrift<< reg.retailprice<<endl;
	 utskrift<<endl;
    }
   utskrift.close();
}

void sell(void)
{

}

Im stuck at

void sortering()
{

}

Im supposted to make a function where the program sort the list in bilreg.txt
that at the moment looks something like this:
Honda White 2 4 300000 200000
Cleo Black 1 7 1000000 900000
Ferrari Red 3 5 1200000 1000000

and it should sort it alphabetically, like this:
Cleo Black 1 7 1000000 900000
Ferrari Red 3 5 1200000 1000000
Honda White 2 4 300000 200000

Im also stuck at

void sell(void)
{

}

Where you should be able to sell the cars in the bilreg.txt register
and when you do, the register should change so that the instock line gets -1 and the sold line gets +1

Please can someone help me with this?

Recommended Answers

All 2 Replies

It seems your sort function is in ascending order, so this should do:

string test[] = {("Testing sorting strings"),
                 ("Pray this works")};
int array_size = sizeof(test) / sizeof(test[0]);//To find the number of elements in the array
for(int i = 0; i<array_size;i++){
if(test[i][0] > test[i+1][0]){
cout<<test[i]<<endl;
}
else {
cout<<test[i+1]<<endl;
}
}

@tkud the code you posted will access an out of bounds index in the if statement.

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.