c++ 文件输出及cin.setf()和cin.precision()学习

本文介绍了C++中文件输出的基本概念,并详细探讨了`cin.setf()`和`cin.precision()`的使用,以及如何通过`iomanip`库进行更复杂的格式控制,包括设置基数、填充字符、小数精度和域宽等。通过示例代码展示了如何实现固定浮点显示、指数表示、左右对齐等效果。
摘要由CSDN通过智能技术生成

1.学习了文件输出后,看了c++ primer plus的例6.15.,有些不懂的注释出来,便于以后学习。




#include "stdafx.h"
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char automobile[50];
int year;
double a_price;
double b_price;


ofstream outFile;
outFile.open("E:\\grade2017.txt",ios::out); //和书上不同,规定了输出文件的路径




cout<<"enter the make and model of automobile"<<endl;
cin.getline(automobile,50);
cout<<"enter the model year"<<endl;
cin>>year;
cout<<"enter the original asking price:";
cin>>a_price;
b_price=a_price*0.9;


//利用cout在屏幕上显示出结果
cout<<fixed;
cout.precision(2);
cout.setf(ios_base::showpoint);
cout<<"make and model:"<<automobile<<endl;
cout<<"year:"<<year<<endl;
cout<<"was asking price:"<<a_price<<endl;
cout<<"Now asking price:"<<b_price<<endl;


//利用outFile输出文件
 
outFile<<fixed; //用一般形式输出字浮点数
outFile.precision(2);// 表示两位小数点输出
outFile.setf(ios_base::showpoint); //强制显示小数点输出
outFile<<"make and model:"<<automobile<<endl;
outFile<<"year:"<<year<<endl;
outFile<<"was asking price:"<<a_price<<endl;
outFile<<"Now asking price:"<<b_price<<endl;


//切记关闭文件
outFile.close();
return 0;


}


2.学习cout.self 和cout.precision()

这两个就是格式控制的~ostream成员函数里面的,也可以用输出流操作符来控制,都一样的~附给你一些看看~

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值