#include <iostream>
#include <string>
using namespace std;
#include <process.h> //for exit()
const max=80;
////////////////////////////////////////////////////////////////
class publication
{
private:
char title[max];
float coast;
public:
void getdata()
{
cout << "Title: "; cin.get(title,max);
cout << "Coast: "; cin >> coast;
}
void putdata()const
{
cout << "Title: " << title << endl;
cout << "Coast: " << coast << endl;
}
};
class book: public publication
{
private:
int page;
public:
void getdata()
{
publication::getdata();
cout << "Page: "; cin >> page;
}
void putdata()const
{
publication::putdata();
cout << "Page: " << page << endl;
}
};
class type: public publication
{
private:
float rec;
public:
void getdata()
{
publication::getdata();
cout << "Type: "; cin >> rec;
}
void putdata()const
{
publication::putdata();
cout << "Type: " << rec;
}
};
////////////////////////////////////////////////////////////////
int main()
{
book one;
type two;
one.getdata();
two.getdata();
one.putdata();
two.putdata();
cout << endl;
return 0;
}
Необьяснимое поведение cin.get()
Есть 2 версии кода, первая работает вторая работает неправильно:
Вариант№1
Код:
Вариант2
Код:
// ex9_1.cpp
// publication class and derived classes
#include <iostream>
#include <string>
using namespace std;
////////////////////////////////////////////////////////////////
class publication // base class
{
private:
string title;
float price;
public:
void getdata()
{
cout << "\nEnter title: "; cin >> title;
cout << "Enter price: "; cin >> price;
}
void putdata() const
{
cout << "\nTitle: " << title;
cout << "\nPrice: " << price;
}
};
////////////////////////////////////////////////////////////////
class book : private publication // derived class
{
private:
int pages;
public:
void getdata()
{
publication::getdata();
cout << "Enter number of pages: "; cin >> pages;
}
void putdata() const
{
publication::putdata();
cout << "\nPages: " << pages;
}
};
////////////////////////////////////////////////////////////////
class tape : private publication // derived class
{
private:
float time;
public:
void getdata()
{
publication::getdata();
cout << "Enter playing time: "; cin >> time;
}
void putdata() const
{
publication::putdata();
cout << "\nPlaying time: " << time;
}
};
////////////////////////////////////////////////////////////////
int main()
{
book book1; // define publications
tape tape1;
book1.getdata(); // get data for them
tape1.getdata();
book1.putdata(); // display their data
tape1.putdata();
cout << endl;
return 0;
}
// publication class and derived classes
#include <iostream>
#include <string>
using namespace std;
////////////////////////////////////////////////////////////////
class publication // base class
{
private:
string title;
float price;
public:
void getdata()
{
cout << "\nEnter title: "; cin >> title;
cout << "Enter price: "; cin >> price;
}
void putdata() const
{
cout << "\nTitle: " << title;
cout << "\nPrice: " << price;
}
};
////////////////////////////////////////////////////////////////
class book : private publication // derived class
{
private:
int pages;
public:
void getdata()
{
publication::getdata();
cout << "Enter number of pages: "; cin >> pages;
}
void putdata() const
{
publication::putdata();
cout << "\nPages: " << pages;
}
};
////////////////////////////////////////////////////////////////
class tape : private publication // derived class
{
private:
float time;
public:
void getdata()
{
publication::getdata();
cout << "Enter playing time: "; cin >> time;
}
void putdata() const
{
publication::putdata();
cout << "\nPlaying time: " << time;
}
};
////////////////////////////////////////////////////////////////
int main()
{
book book1; // define publications
tape tape1;
book1.getdata(); // get data for them
tape1.getdata();
book1.putdata(); // display their data
tape1.putdata();
cout << endl;
return 0;
}
Разница в том что в первом варианте переменная title объявляется как массив символов - char title[max] совместно с cin.get(title,max),
а во втором случае как строковая переменная string title.
В итоге первый вариант работет некорректно...
Почему так происходит?
string title;
getline(cin, title , '\n')