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

C++ JAVA
Post Reply
respec7_m3
Нов
Нов
Posts: 170
Joined: Wed Jun 30, 2010 4:42 pm

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

Post by respec7_m3 » Mon Oct 12, 2015 5:36 pm

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

Не мога да разбера къде греша и защо препълвам double.

Code: Select all

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

class TV
&#123;
private&#58;
	string model;
	int sizeOfScreen;
	double price;
public &#58;
	TV&#40;&#41;
	&#123;
		this->model = "LG";
		this->sizeOfScreen = 32;
		this->price = 2500;
	&#125;
	TV&#40;string model, int sizeOfScreen, double price&#41;
	&#123;
		this->model = model;
		this->sizeOfScreen = sizeOfScreen;
		this->price = price;
	&#125;
	~TV&#40;&#41;
	&#123;
	&#125;

	string getModel&#40;&#41;
	&#123;
		return model;
	&#125;
	void setModel&#40;string m&#41;
	&#123;
		model = m;
	&#125;

	int getsizeOfScreen&#40;&#41;
	&#123;
		return sizeOfScreen;
	&#125;
	void setsizeOfScreen&#40;int size&#41;
	&#123;
		sizeOfScreen = size;
	&#125;

	double getPrice&#40;&#41;
	&#123;
		return price;
	&#125;
	void setPrice&#40;double p&#41;
	&#123;
		price = p;
	&#125;

	void show&#40;&#41;
	&#123;
		cout<<"\nMarka&#58; "<<model;
		cout<<"\nRazmer na ekrana "<<sizeOfScreen;
		cout<<"\nCena "<<price;
	&#125;

	void init&#40;string m,int size,double p&#41;
	&#123;
		model = m;
		sizeOfScreen = size;
		price = p;
	&#125;

	bool isBig&#40;&#41;
	&#123;
		if&#40;sizeOfScreen >= 32&#41;
		&#123;
			return true;
		&#125;
		else
		&#123;
			return false;
		&#125;
	&#125;

	void readKB&#40;&#41;
	&#123;
		cout<<endl;
		cout<<"Size of screen"<<endl;
		cin>>sizeOfScreen;
		cout<<"Price"<<endl;
		cin>>price;
		cout<<"Model"<<endl;
		cin>>model;
	&#125;
&#125;;

int main&#40;&#41;
&#123;
	TV tvArray&#91;5&#93; = &#123;TV&#40;&#41;,
					TV&#40;"Sony", 37, 1000&#41;,
					TV&#40;"LG", 32, 2500&#41;,
					TV&#40;"Samsung", 42, 4500&#41;,
					TV&#40;"Philips", 60, 5000&#41;&#125;;

    TV helperTV;
    TV tvNewArray&#91;5&#93;;

    double averagePrice = 0;
	ofstream TVFile;
	ifstream TVFileRead;


	for&#40;int i=0; i<5; i++&#41;
	&#123;
		tvArray&#91;i&#93;.show&#40;&#41;;
	&#125;

    helperTV=tvArray&#91;0&#93;;

	for&#40;int i = 0; i < 5; i++&#41;
    &#123;
        if&#40;tvArray&#91;i&#93;.getsizeOfScreen&#40;&#41; > helperTV.getsizeOfScreen&#40;&#41;&#41;
        &#123;
            helperTV=tvArray&#91;i&#93;;
        &#125;

        averagePrice += tvArray&#91;i&#93;.getPrice&#40;&#41;;
    &#125;

    helperTV.show&#40;&#41;;
    averagePrice = averagePrice/5;
    cout << "\nAverage Price&#58; " << averagePrice;
    TVFile.open&#40;"TVInfo.txt", ios&#58;&#58;out&#41;;

    for&#40;int i=0; i<5; i++&#41;
    &#123;
    TVFile.write&#40;&#40;char *&#41;&tvArray&#91;i&#93;, sizeof&#40;tvArray&#91;i&#93;&#41;&#41;;
    &#125;
    TVFile.close&#40;&#41;;

    TVFileRead.open&#40;"TVInfo.txt", ios&#58;&#58;in&#41;;
    for&#40;int i=0; i<5; i++&#41;
    &#123;
        cout << TVFileRead.read&#40;&#40;char *&#41;&tvNewArray&#91;i&#93;, sizeof&#40;tvNewArray&#91;i&#93;&#41;&#41;;
    &#125;
    TVFileRead.close&#40;&#41;;

    for&#40;int i=0; i<5; i++&#41;
    &#123;
        tvNewArray&#91;i&#93;.show&#40;&#41;;
    &#125;

	return 1;
&#125;

User avatar
Revelation
Web-tourist
Web-tourist
Posts: 891
Joined: Sun Mar 24, 2013 1:23 pm
Answers: 63

Post by Revelation » Mon Oct 12, 2015 5:50 pm

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

[php]
double i = 5;
[/php]

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

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

respec7_m3
Нов
Нов
Posts: 170
Joined: Wed Jun 30, 2010 4:42 pm

Post by respec7_m3 » Mon Oct 12, 2015 5:57 pm

Revelation wrote:double винаги трябва да получава стойност с плаваща запетая.

[php]
double i = 5;
[/php]

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

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

Code: Select all

#include <iostream>

using namespace std;

int main&#40;&#41;
&#123;
    double avgprice = 0;
    double price1 = 13.12;
    double price2 = 12.32;
    avgprice = price1/price2;
    cout << avgprice << endl;
    return 1;
&#125;

User avatar
Revelation
Web-tourist
Web-tourist
Posts: 891
Joined: Sun Mar 24, 2013 1:23 pm
Answers: 63

Post by Revelation » Mon Oct 12, 2015 6:24 pm

Моя е грешката. Бях останал с убеждението, че винаги очакваше да е с плаваща запетая.

Сега нямам инсталиран компилатор на компютъра, за да тествам.

respec7_m3
Нов
Нов
Posts: 170
Joined: Wed Jun 30, 2010 4:42 pm

Post by respec7_m3 » Mon Oct 12, 2015 6:48 pm

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

User avatar
Revelation
Web-tourist
Web-tourist
Posts: 891
Joined: Sun Mar 24, 2013 1:23 pm
Answers: 63

Post by Revelation » Mon Oct 12, 2015 7:06 pm

При мен се дъни четенето на данните от файла. Обърни там внимание.

respec7_m3
Нов
Нов
Posts: 170
Joined: Wed Jun 30, 2010 4:42 pm

Post by respec7_m3 » Mon Oct 12, 2015 7:09 pm

Revelation wrote:При мен се дъни четенето на данните от файла. Обърни там внимание.
Не е там проблема.

User avatar
djman
Гуру
Гуру
Posts: 2801
Joined: Sat Sep 12, 2009 8:07 am
Answers: 107

Post by djman » Mon Oct 12, 2015 7:14 pm

Проблемът е тук:

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 на блоковете.

User avatar
Revelation
Web-tourist
Web-tourist
Posts: 891
Joined: Sun Mar 24, 2013 1:23 pm
Answers: 63

Post by Revelation » Mon Oct 12, 2015 7:31 pm

respec7_m3 wrote:
Revelation wrote:При мен се дъни четенето на данните от файла. Обърни там внимание.
Не е там проблема.
[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.show();
}

helperTV=tvArray[0];

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

averagePrice += tvArray.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, sizeof(tvArray));
}
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.show();
}

return 1;
}
[/php]

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

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

User avatar
Revelation
Web-tourist
Web-tourist
Posts: 891
Joined: Sun Mar 24, 2013 1:23 pm
Answers: 63

Post by Revelation » Mon Oct 12, 2015 7:52 pm

Последен вариант с писане и четене.

[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.show();
}

helperTV=tvArray[0];

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

averagePrice += tvArray.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;
}
[/php]

Post Reply