Проблем с кода ми C++

respec7_m3

Registered
При изпълнението на следният програмен фрагмент ми се появява грешка:
http://store.picbg.net/pubpic/32/AF/aaf0bb47a9f732af.png

Не мога да разбера къде греша и защо препълвам double.
Код:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class TV
{
private:
	string model;
	int sizeOfScreen;
	double price;
public :
	TV()
	{
		this->model = "LG";
		this->sizeOfScreen = 32;
		this->price = 2500;
	}
	TV(string model, int sizeOfScreen, double price)
	{
		this->model = model;
		this->sizeOfScreen = sizeOfScreen;
		this->price = price;
	}
	~TV()
	{
	}

	string getModel()
	{
		return model;
	}
	void setModel(string m)
	{
		model = m;
	}

	int getsizeOfScreen()
	{
		return sizeOfScreen;
	}
	void setsizeOfScreen(int size)
	{
		sizeOfScreen = size;
	}

	double getPrice()
	{
		return price;
	}
	void setPrice(double p)
	{
		price = p;
	}

	void show()
	{
		cout<<"\nMarka: "<<model;
		cout<<"\nRazmer na ekrana "<<sizeOfScreen;
		cout<<"\nCena "<<price;
	}

	void init(string m,int size,double p)
	{
		model = m;
		sizeOfScreen = size;
		price = p;
	}

	bool isBig()
	{
		if(sizeOfScreen >= 32)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	void readKB()
	{
		cout<<endl;
		cout<<"Size of screen"<<endl;
		cin>>sizeOfScreen;
		cout<<"Price"<<endl;
		cin>>price;
		cout<<"Model"<<endl;
		cin>>model;
	}
};

int main()
{
	TV tvArray[5] = {TV(),
					TV("Sony", 37, 1000),
					TV("LG", 32, 2500),
					TV("Samsung", 42, 4500),
					TV("Philips", 60, 5000)};

    TV helperTV;
    TV tvNewArray[5];

    double averagePrice = 0;
	ofstream TVFile;
	ifstream TVFileRead;


	for(int i=0; i<5; i++)
	{
		tvArray[i].show();
	}

    helperTV=tvArray[0];

	for(int i = 0; i < 5; i++)
    {
        if(tvArray[i].getsizeOfScreen() > helperTV.getsizeOfScreen())
        {
            helperTV=tvArray[i];
        }

        averagePrice += tvArray[i].getPrice();
    }

    helperTV.show();
    averagePrice = averagePrice/5;
    cout << "\nAverage Price: " << averagePrice;
    TVFile.open("TVInfo.txt", ios::out);

    for(int i=0; i<5; i++)
    {
    TVFile.write((char *)&tvArray[i], sizeof(tvArray[i]));
    }
    TVFile.close();

    TVFileRead.open("TVInfo.txt", ios::in);
    for(int i=0; i<5; i++)
    {
        cout << TVFileRead.read((char *)&tvNewArray[i], sizeof(tvNewArray[i]));
    }
    TVFileRead.close();

    for(int i=0; i<5; i++)
    {
        tvNewArray[i].show();
    }

	return 1;
}
 
double винаги трябва да получава стойност с плаваща запетая.

PHP:
double i = 5;

ще ти върне грешка.

Ти още в конструктора подаваш стойност, която е целочислена. Вземи го това предвид. 2500 трябва да стане 2500.00
 
Revelation каза:
double винаги трябва да получава стойност с плаваща запетая.

PHP:
double i = 5;

ще ти върне грешка.

Ти още в конструктора подаваш стойност, която е целочислена. Вземи го това предвид. 2500 трябва да стане 2500.00

Защо тогава този код работи:
Код:
#include <iostream>

using namespace std;

int main()
{
    double avgprice = 0;
    double price1 = 13.12;
    double price2 = 12.32;
    avgprice = price1/price2;
    cout << avgprice << endl;
    return 1;
}
 
Моя е грешката. Бях останал с убеждението, че винаги очакваше да е с плаваща запетая.

Сега нямам инсталиран компилатор на компютъра, за да тествам.
 
Ще се радвам ако някой ми види грешката, защото аз се пуля вече няколко часа... Мога да си пренапиша кода, но не е това целта.
 
Проблемът е тук:

TVFileRead.open("TVInfo.txt", ios::in);
for(int i=0; i<5; i++)
{
cout << TVFileRead.read((char *)&tvNewArray, sizeof(tvNewArray));
}
TVFileRead.close();

ако се коментира, излиза ок. искаш да принтираш това, което връща istream::read... и успяваш де :)

П.С. открих проблема като коментирах всичко след cout << "Average price" и махах коментарите 1 по 1 на блоковете.
 
respec7_m3 каза:
Revelation каза:
При мен се дъни четенето на данните от файла. Обърни там внимание.
Не е там проблема.

PHP:
	#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class TV
{
private:
   string model;
   int sizeOfScreen;
   double price;
public :
   TV()
   {
      this->model = "LG";
      this->sizeOfScreen = 32;
      this->price = 2500;
   }
   TV(string model, int sizeOfScreen, double price)
   {
      this->model = model;
      this->sizeOfScreen = sizeOfScreen;
      this->price = price;
   }
   ~TV()
   {
   }

   string getModel()
   {
      return model;
   }
   void setModel(string m)
   {
      model = m;
   }

   int getsizeOfScreen()
   {
      return sizeOfScreen;
   }
   void setsizeOfScreen(int size)
   {
      sizeOfScreen = size;
   }

   double getPrice()
   {
      return this->price;
   }
   void setPrice(double p)
   {
      this->price = p;
   }

   void show()
   {
      cout<<"\nMarka: "<<this->model;
      cout<<"\nRazmer na ekrana "<<this->sizeOfScreen;
      cout<<"\nCena "<<this->price;
   }

   void init(string m,int size,double p)
   {
      this->model = m;
      this->sizeOfScreen = size;
      this->price = p;
   }

   bool isBig()
   {
      if(sizeOfScreen >= 32)
      {
         return true;
      }
      else
      {
         return false;
      }
   }

   void readKB()
   {
      cout<<endl;
      cout<<"Size of screen"<<endl;
      cin>>sizeOfScreen;
      cout<<"Price"<<endl;
      cin>>price;
      cout<<"Model"<<endl;
      cin>>model;
   }
};

int main()
{
   TV tvArray[5] = {TV("Sa", 43, 2330),
               TV("Sony", 37, 1000),
               TV("LG", 32, 2500),
               TV("Samsung", 42, 4500),
               TV("Philips", 60, 5000)};

    TV helperTV;
    TV tvNewArray[5];

    double averagePrice = 0;
   ofstream TVFile;
   ifstream TVFileRead;
   streampos size;
   char * memblock;


   for(int i=0; i<5; i++)
   {
      tvArray[i].show();
   }

    helperTV=tvArray[0];

   for(int i = 0; i < 5; i++)
    {
        if(tvArray[i].getsizeOfScreen() > helperTV.getsizeOfScreen())
        {
            helperTV=tvArray[i];
        }

        averagePrice += tvArray[i].getPrice();
    }

    helperTV.show();
    averagePrice = averagePrice/5;
    cout << "\nAverage Price: " << averagePrice;
    TVFile.open("TVInfo.txt", ios::in);

    for(int i=0; i<5; i++)
    {
        TVFile.write((char *)&tvArray[i], sizeof(tvArray[i]));
    }
    TVFile.close();

    TVFileRead.open("TVInfo.txt", ios::in);
    size = TVFileRead.tellg();
    memblock = new char [size];
    for(int i=0; i<5; i++)
    {
        //TVFileRead.seekg (0, ios::beg);
        TVFileRead.read(memblock, size);
    }
    TVFileRead.close();

     cout << memblock << endl;
    
    delete[] memblock;

    for(int i=0; i<5; i++)
    {
        tvNewArray[i].show();
    }

   return 1;
}

Казах ти да се насочиш натам, но ти не та не... Прочети повече за file потоците.

След това си викай информацията от memblock. Функцията ти пази данните в паметта, не ги извежда веднага.
 
Последен вариант с писане и четене.

PHP:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class TV
{
private:
   string model;
   int sizeOfScreen;
   double price;
public :
   TV()
   {
      this->model = "LG";
      this->sizeOfScreen = 32;
      this->price = 2500;
   }
   TV(string model, int sizeOfScreen, double price)
   {
      this->model = model;
      this->sizeOfScreen = sizeOfScreen;
      this->price = price;
   }
   ~TV()
   {
   }

   string getModel()
   {
      return model;
   }
   void setModel(string m)
   {
      model = m;
   }

   int getsizeOfScreen()
   {
      return sizeOfScreen;
   }
   void setsizeOfScreen(int size)
   {
      sizeOfScreen = size;
   }

   double getPrice()
   {
      return this->price;
   }
   void setPrice(double p)
   {
      this->price = p;
   }

   void show()
   {
      cout<<"\nMarka: "<<this->model;
      cout<<"\nRazmer na ekrana "<<this->sizeOfScreen;
      cout<<"\nCena "<<this->price;
   }

   void init(string m,int size,double p)
   {
      this->model = m;
      this->sizeOfScreen = size;
      this->price = p;
   }

   bool isBig()
   {
      if(sizeOfScreen >= 32)
      {
         return true;
      }
      else
      {
         return false;
      }
   }

   void readKB()
   {
      cout<<endl;
      cout<<"Size of screen"<<endl;
      cin>>sizeOfScreen;
      cout<<"Price"<<endl;
      cin>>price;
      cout<<"Model"<<endl;
      cin>>model;
   }
};

int main()
{
   TV tvArray[5] = {TV("Sa", 43, 2330),
               TV("Sony", 37, 1000),
               TV("LG", 32, 2500),
               TV("Samsung", 42, 4500),
               TV("Philips", 60, 5000)};

    TV helperTV;
    TV tvNewArray[5];

    double averagePrice = 0;
   ofstream TVFile;
   ifstream TVFileRead;
   char memblock[100];


   for(int i=0; i<5; i++)
   {
      tvArray[i].show();
   }

    helperTV=tvArray[0];

   for(int i = 0; i < 5; i++)
    {
        if(tvArray[i].getsizeOfScreen() > helperTV.getsizeOfScreen())
        {
            helperTV=tvArray[i];
        }

        averagePrice += tvArray[i].getPrice();
    }

    averagePrice = averagePrice/5;
    cout << "\nAverage Price: " << averagePrice << endl;
    TVFile.open("TVInfo.txt");
    TVFile << averagePrice << '\n';
    TVFile.close();
    
    TVFileRead.open("TVInfo.txt");
    TVFileRead >> memblock;
    cout << memblock << endl;
    TVFileRead.close();

   return 1;
}
 

Горе