alert30
30-03-2016, 02:57
Входные данные не выдает ошибок, только сама программа не понимает, что ему еще нужно. Есть причина, что я в структуре shop приписал product* z[10];, но как тогда приписать в main что-то вроде "x.z=...", чтобы вместо ... написать функцию, которая возвращает значение структуры с размером массива и сделать так, чтобы программа поняла, что готова к использованию на консоле.
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <windows.h>
#include <fstream>
#include <algorithm>
#include <sys/stat.h>
using namespace std;
ofstream fWrite;
ifstream fRead;
struct product{
string seller;
string name;
float amount;
int price;
int dateofsale;
};
struct shop{
int m;
product* z[10]; //!
shop():m(4){};
};
void write(product *);
void show(product *);
void del_last(shop &);
void add_new(shop &, product &);
void readFile(shop &, product &y, ifstream&, const string&);
void writeFile(shop, ofstream&, const string&);
bool fileExist(const string&);
bool isEmpty(ifstream&);
int main(){
system("color 0f");
setlocale(0,"");
shop x;
product y;
string n;
//
x.z[0]->seller="Golev";
x.z[0]->name="Coca-Cola";
x.z[0]->amount=1;
x.z[0]->price=200;
x.z[0]->dateofsale=2612016;
x.z[1]->seller="Isaev";
x.z[1]->name="Fanta";
x.z[1]->amount=1;
x.z[1]->price=220;
x.z[1]->dateofsale=2012016;
x.z[2]->seller="Belenova";
x.z[2]->name="7UP";
x.z[2]->amount=1;
x.z[2]->price=230;
x.z[2]->dateofsale=2112016;
x.z[3]->seller="Nurova";
x.z[3]->name="Sprite";
x.z[3]->amount=1;
x.z[3]->price=250;
x.z[3]->dateofsale=1722016;
while (n!="exit"){
cout<<"Введите команду"<<endl;
getline(cin, n);
transform(n.begin(), n.end(), n.begin(), ::tolower);
if (n=="help"){
cout<<endl;
cout<<"SHOW\t= Показ вывода"<<endl;
cout<<"READ\t= Считывать из файла"<<endl;
cout<<"SAVE\t= Сохранить данные в файл"<<endl;
cout<<"ADD\t= Добавить товар"<<endl;
cout<<"DELL\t= Удалить последнего товара из корзины"<<endl;
cout<<"EXIT\t= Выход"<<endl;
cout<<endl;
}
else if (n=="show"){
for(int i=0; i<x.m; i++){
show(x.z[i]);
cout<<endl;
}
}
else if (n=="read"){
fRead.close();
string name;
cout<<"Введите имя файла: ";
getline(cin, name);
readFile(x, y, fRead, name);
}
else if (n=="save"){
fRead.close();
string name;
cout<<"Сохранить как: ";
getline(cin, name);
writeFile(x, fWrite, name);
}
else if (n=="add"){
add_new(x,y);
}
else if (n=="dell"){
del_last(x);
}
else{
cout<<"Список команд = help"<<endl;
}
}
cout<<"Программа закрыта"<<endl;
for(int i=0; i<x.m; i++){
delete x.z[i];
}
system("pause");
return 0;
}
void write(product *x){
cout<<"Продавец\n-----> ";
cin>>x->seller;
cout<<"Наименование\n-----> ";
cin>>x->name;
cout<<"Количество\n-----> ";
cin>>x->amount;
cout<<"Цена\n-----> ";
cin>>x->price;
cout<<"Дата продажи\n-----> ";
cin>>x->dateofsale;
}
void show(product *x){
cout<<"Продавец: \t"<<x->seller<<endl;
cout<<"Наименование: \t"<<x->name<<endl;
cout<<"Количество: \t"<<x->amount<<endl;
cout<<"Цена: \t"<<x->price<<" руб"<<endl;
cout<<"Дата продажи: \t"<<x->dateofsale<<endl;
}
void del_last(shop &x){
if (x.m>0){
cout<<"Идет удаление..."<<endl;
x.m=x.m-1;
}
else{
cout<<"К удалению список пуст"<<endl;
}
}
void add_new(shop &x, product &y){
write(&y);
x.z[x.m]=new product (y);
x.m=x.m+1;
}
void readFile(shop &x, product &y, ifstream& read, const string& n){
if (fileExist(n)==false){
cerr<<"Файл '"<<n<<"' не существует"<<endl;
return;
}
read.open(n.c_str(), ios::out|ios::app);
while(true){
read>>x.m;
x.z[x.m]=new product[x.m];
if (read.eof()){
break;
}
for(int i=0; i<x.m; i++){
read>>y.seller>>y.name>>y.amount>>y.price>>y.dateofsale;
x.z[i]=new product (y);
}
}
}
void writeFile(shop x, ofstream& write, const string& n ){
write.open(n.c_str());
if (!(write.is_open())){
cerr<<"Невозможно сохранить файл '"<<n<<"'"<<endl;
return;
}
write<<x.m<<endl;
for (int i=0; i<x.m; i++){
write<<x.z[i]->seller<<"\t"<<x.z[i]->name<<"\t"<<x.z[i]->amount<<"\t"<<x.z[i]->price<<"\t"<<x.z[i]->dateofsale<<endl;
}
cout<<"Файл сохранен как '"<<n<<"'"<<endl;
write.close();
}
bool fileExist(const string& name){
struct stat buf;
if (stat(name.c_str(), &buf) != -1){
return true;
}
return false;
}
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <windows.h>
#include <fstream>
#include <algorithm>
#include <sys/stat.h>
using namespace std;
ofstream fWrite;
ifstream fRead;
struct product{
string seller;
string name;
float amount;
int price;
int dateofsale;
};
struct shop{
int m;
product* z[10]; //!
shop():m(4){};
};
void write(product *);
void show(product *);
void del_last(shop &);
void add_new(shop &, product &);
void readFile(shop &, product &y, ifstream&, const string&);
void writeFile(shop, ofstream&, const string&);
bool fileExist(const string&);
bool isEmpty(ifstream&);
int main(){
system("color 0f");
setlocale(0,"");
shop x;
product y;
string n;
//
x.z[0]->seller="Golev";
x.z[0]->name="Coca-Cola";
x.z[0]->amount=1;
x.z[0]->price=200;
x.z[0]->dateofsale=2612016;
x.z[1]->seller="Isaev";
x.z[1]->name="Fanta";
x.z[1]->amount=1;
x.z[1]->price=220;
x.z[1]->dateofsale=2012016;
x.z[2]->seller="Belenova";
x.z[2]->name="7UP";
x.z[2]->amount=1;
x.z[2]->price=230;
x.z[2]->dateofsale=2112016;
x.z[3]->seller="Nurova";
x.z[3]->name="Sprite";
x.z[3]->amount=1;
x.z[3]->price=250;
x.z[3]->dateofsale=1722016;
while (n!="exit"){
cout<<"Введите команду"<<endl;
getline(cin, n);
transform(n.begin(), n.end(), n.begin(), ::tolower);
if (n=="help"){
cout<<endl;
cout<<"SHOW\t= Показ вывода"<<endl;
cout<<"READ\t= Считывать из файла"<<endl;
cout<<"SAVE\t= Сохранить данные в файл"<<endl;
cout<<"ADD\t= Добавить товар"<<endl;
cout<<"DELL\t= Удалить последнего товара из корзины"<<endl;
cout<<"EXIT\t= Выход"<<endl;
cout<<endl;
}
else if (n=="show"){
for(int i=0; i<x.m; i++){
show(x.z[i]);
cout<<endl;
}
}
else if (n=="read"){
fRead.close();
string name;
cout<<"Введите имя файла: ";
getline(cin, name);
readFile(x, y, fRead, name);
}
else if (n=="save"){
fRead.close();
string name;
cout<<"Сохранить как: ";
getline(cin, name);
writeFile(x, fWrite, name);
}
else if (n=="add"){
add_new(x,y);
}
else if (n=="dell"){
del_last(x);
}
else{
cout<<"Список команд = help"<<endl;
}
}
cout<<"Программа закрыта"<<endl;
for(int i=0; i<x.m; i++){
delete x.z[i];
}
system("pause");
return 0;
}
void write(product *x){
cout<<"Продавец\n-----> ";
cin>>x->seller;
cout<<"Наименование\n-----> ";
cin>>x->name;
cout<<"Количество\n-----> ";
cin>>x->amount;
cout<<"Цена\n-----> ";
cin>>x->price;
cout<<"Дата продажи\n-----> ";
cin>>x->dateofsale;
}
void show(product *x){
cout<<"Продавец: \t"<<x->seller<<endl;
cout<<"Наименование: \t"<<x->name<<endl;
cout<<"Количество: \t"<<x->amount<<endl;
cout<<"Цена: \t"<<x->price<<" руб"<<endl;
cout<<"Дата продажи: \t"<<x->dateofsale<<endl;
}
void del_last(shop &x){
if (x.m>0){
cout<<"Идет удаление..."<<endl;
x.m=x.m-1;
}
else{
cout<<"К удалению список пуст"<<endl;
}
}
void add_new(shop &x, product &y){
write(&y);
x.z[x.m]=new product (y);
x.m=x.m+1;
}
void readFile(shop &x, product &y, ifstream& read, const string& n){
if (fileExist(n)==false){
cerr<<"Файл '"<<n<<"' не существует"<<endl;
return;
}
read.open(n.c_str(), ios::out|ios::app);
while(true){
read>>x.m;
x.z[x.m]=new product[x.m];
if (read.eof()){
break;
}
for(int i=0; i<x.m; i++){
read>>y.seller>>y.name>>y.amount>>y.price>>y.dateofsale;
x.z[i]=new product (y);
}
}
}
void writeFile(shop x, ofstream& write, const string& n ){
write.open(n.c_str());
if (!(write.is_open())){
cerr<<"Невозможно сохранить файл '"<<n<<"'"<<endl;
return;
}
write<<x.m<<endl;
for (int i=0; i<x.m; i++){
write<<x.z[i]->seller<<"\t"<<x.z[i]->name<<"\t"<<x.z[i]->amount<<"\t"<<x.z[i]->price<<"\t"<<x.z[i]->dateofsale<<endl;
}
cout<<"Файл сохранен как '"<<n<<"'"<<endl;
write.close();
}
bool fileExist(const string& name){
struct stat buf;
if (stat(name.c_str(), &buf) != -1){
return true;
}
return false;
}