Пытаюсь разобраться с манипуляторами на примере этой задачи
1. Написать программу - посчитать длину окружности. Входные данные поступают с
клавиатуры. Установить ширину поля 10 символов, установить точность 4 цифры,
заполнить вместо пробелов символом / с помощью функций и
манипуляторов.Предусмотреть обработку ошибок. Результат выводится на экран в
десятичной, шестнадцатеричной и восьмеричной системе.
2. Проверить какие флаги потока вывода установлены и заменить попарно на
десятичные на шестнадцатиричные (и наоборот) левое выравнивание на правое
выравнивание (и наоборот) Проверить результат. Добавить в программу два своих
манипулятора. Один выводит сообщение, другой устанавливает
шестнадцатеричный вывод и символ заполнения $.
# include <iostream.h>
# include <iomanip.h>
const float pi=3.14;
int main() {
float l;
float r;
// number ONE
cout.width(10);
cout.precision(4);
cout.fill('/');
cout << "radius pls";;
// for (float r=0; cin.rdstate != '_good'; ++i);
cin >> r;
if (cin.rdstate != "_good") {
cout << " Input Error"
}
l=2*pi*r;
cout << "leghth of circle is";
cout << dec << l << /n;
cout << oct << l << /n;
cout << hex << l << endl;
// NUMBER TWO
long fl;
fl= cout.flags();
cout << "Now flag position: "<< fl<<"\n";
ostream& privet(ostream &stream)
{
stream << 'privet';
return stream;
}
ostream& hexdollar(ostream &stream)
{
stream.setf(ios::hex);
stream << fill('$');
return stream;
}
cout << privet << /n;
cout << hexdollar << 17 << endl;
return 0;
}
Ты "перемешал" оба задания в одну кашу... Отдели одно от другого. Вот так выглядит первое:
# include <iostream.h>(на GCC работает)
# include <iomanip.h>
const float pi = 3.14;
int main() {
float r;
// number ONE
cout.fill('/');
cout << "radius pls ";
cin >> r;
if (!cin.good()) {
cout << " Input Error"; return -1;
}
float l = 2 * pi * r;
cout << "appr. length of circle is ";
cout << setw(10) << dec << (int)l << "\n"; // dec/oct/hex влияют только на целые числа
cout << setw(10) << oct << (int)l << "\n";
cout << setw(10) << hex << (int)l << endl;
return 0;
}
Спасибо, Volvo
Программа 2, выравнивание по правому краю не происходит, почему? (MSV6) Как вообще работают флаги?
# include <iostream.h>
# include <iomanip.h>
ostream& privet(ostream &stream)
{
stream << "privet";
return stream;
}
ostream& hexdollar(ostream &stream)
{
stream.setf(ios::hex);
stream.fill('$');
return stream;
}
int main() {
long fl;
cout.setf(0x0004);
cout.setf(0x0010);
fl= cout.flags();
cout << "Now flag position: "<< fl<<"\n";
if (fl=20){
cout.unsetf(0x0004);
cout.unsetf(0x0010);
cout.setf(0x0002);
cout.setf(0x0040);
}
fl= cout.flags();
cout << "Now flag position: "<< fl<<"\n";
cout << privet << "\n";
cout << hexdollar << 17 << endl;
return 0;
}
#include <iostream> // не *.H-файлНе надо пользоваться безликими числами. Гораздо удобнее использовать именованные константы и перечисления.
#include <iomanip>
using namespace std;
ostream& privet(ostream &stream)
{
stream << "privet";
return stream;
}
ostream& hexdollar(ostream &stream)
{
stream.setf(ios::hex);
stream.fill('$');
return stream;
}
int main() {
long fl;
cout.setf(ios_base::right | ios_base::dec);
cout << setw(15) << privet << endl; // Проверка выравнивания вправо
fl = cout.flags();
cout << "Now flag position: "<< fl<<"\n";
if (fl == 20){ // Здесь - сравнение, а не присваивание !!!
cout.unsetf(ios_base::right | ios_base::dec);
cout.setf(ios_base::left | ios_base::hex);
}
fl = cout.flags();
cout << "Now flag position: "<< fl<<"\n";
cout << privet << "\n";
cout << hexdollar << 17 << endl;
return 0;
}
#include <iostream> // не *.H-файл ххххххххххххххххПОЧЕМУ?
#include <iomanip>
using namespace std; ххххххххххххххххххххх это зачем?
ostream& privet(ostream &stream)
{
stream << "privet";
return stream;
}
ostream& hexdollar(ostream &stream)
{
stream.setf(ios::hex);
stream.fill('$');
return stream;
}
int main() {
long fl;
cout.setf(ios_base::right | ios_base::dec);
cout << setw(15) << privet << endl; // Проверка выравнивания вправо
fl = cout.flags();
cout << "Now flag position: "<< fl<<"\n";
if (fl == 20){ // Здесь - сравнение, а не присваивание !!! xxxxD
cout.unsetf(ios_base::right | ios_base::dec);
cout.setf(ios_base::left | ios_base::hex);
}
fl = cout.flags();
cout << "Now flag position: "<< fl<<"\n";
cout << privet << "\n";
cout << hexdollar << 17 << endl;
return 0;
}
, а сразу сказать компилятору, что ты будешь использовать именно пространство имен std...
std::cout.setf(std::ios_base::right | std::ios_base::dec);
std::cout << std::setw(15) << privet << std::endl; // Проверка выравнивания вправо
Achat Misoprostol France
Keflex How To Take
Why not settling on games that is fun and at the same time your earning. Well itll make suspense because of the game as well but dude just try it and it gave me hope while pandemic is real rn. https://timesofnewspaper.com/effects-of-gambling-on-the-economy/