C++ 返回长数字串的问题

zhengweiyu 2005-06-28 11:45:47
编写一个足球售票的程序

输入文件(soldstatus.txt)
(票价) (出售票数)
250 5750
100 28000
50 35750
25 18750

期望输出(grossamount.txt)
Number of all tickets sold = 88250.
Gross amount = 6493750 .


实际输出(grossamount.txt)
Number of all tickets sold = 88250.
Gross amount = 0xffffffff .

我的源程序
======================================
/*
Filename: ex_3_11_03.cpp
Author: xexplorer
CtrateDate: 2005-06-15
Version: 1.0
Description: Football Tickets Calculator

ModifiedDate: yyyy-mm-dd
*/

/***************[ head file ]***************/
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
/***************[ declear variables ]***************/
double BTicketPrice = 0;
double STicketPrice = 0;
double PTicketPrice = 0;
double GTicketPrice = 0;
long double grossAmount = 0;
int noOfBTicketsSold = 0;
int noOfSTicketsSold = 0;
int noOfPTicketsSold = 0;
int noOfGTicketsSold = 0;
int noOfAllTicketsSold = 0;

/***************[ statments ]***************/

ifstream soldstatus;
ofstream grossamount;

soldstatus.open("soldstatus.txt");
grossamount.open("grossamount.txt");

cout<<"Processing data ... ..."<<endl;
soldstatus
>>BTicketPrice
>>noOfBTicketsSold
>>STicketPrice
>>noOfSTicketsSold
>>PTicketPrice
>>noOfPTicketsSold
>>GTicketPrice
>>noOfGTicketsSold;

noOfAllTicketsSold = noOfBTicketsSold +
noOfSTicketsSold +
noOfPTicketsSold +
noOfGTicketsSold;

grossAmount = BTicketPrice * noOfBTicketsSold +
STicketPrice * noOfSTicketsSold +
PTicketPrice * noOfPTicketsSold +
GTicketPrice * noOfGTicketsSold;


cout.setf(ios::fixed);
cout.setf(ios::showpoint);

grossamount
<<"Number of all tickets sold = "
<<setprecision(2)
<<setw(4)
<<noOfAllTicketsSold
<<"."
<<endl
<<"Gross amount = "
<<setprecision(2)
<<setw(4)
<<grossamount
<<"."
<<endl;


cout
<<"Number of all tickets sold = "
<<setprecision(2)
<<setw(4)
<<noOfAllTicketsSold
<<"."
<<endl
<<"Gross amount = "
<<setprecision(2)
<<setw(4)
<<grossamount
<<"."
<<endl;



soldstatus.close();
grossamount.close();

return 0;
}
======================================
为什么实际输出和期望输出不一致?
...全文
422 21 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhengweiyu 2005-07-02
  • 打赏
  • 举报
回复
原来是一个预定义的值

4096(D)=0x1000(H) => #define _IOSscientific

结贴给分
zhengweiyu 2005-07-01
  • 打赏
  • 举报
回复
自己顶
daseny 2005-07-01
  • 打赏
  • 举报
回复
from <xiosbase.h>

static const _Fmtflags skipws = (_Fmtflags)_IOSskipws;
static const _Fmtflags unitbuf = (_Fmtflags)_IOSunitbuf;
static const _Fmtflags uppercase = (_Fmtflags)_IOSuppercase;
static const _Fmtflags showbase = (_Fmtflags)_IOSshowbase;
static const _Fmtflags showpoint = (_Fmtflags)_IOSshowpoint;
static const _Fmtflags showpos = (_Fmtflags)_IOSshowpos;
static const _Fmtflags left = (_Fmtflags)_IOSleft;
static const _Fmtflags right = (_Fmtflags)_IOSright;
static const _Fmtflags internal = (_Fmtflags)_IOSinternal;
static const _Fmtflags dec = (_Fmtflags)_IOSdec;
static const _Fmtflags oct = (_Fmtflags)_IOSoct;
static const _Fmtflags hex = (_Fmtflags)_IOShex;
static const _Fmtflags scientific = (_Fmtflags)_IOSscientific;
static const _Fmtflags fixed = (_Fmtflags)_IOSfixed;
static const _Fmtflags boolalpha = (_Fmtflags)_IOSboolalpha;
static const _Fmtflags _Stdio = (_Fmtflags)_IOS_Stdio;
static const _Fmtflags adjustfield = (_Fmtflags)(_IOSleft| _IOSright | _IOSinternal);
static const _Fmtflags basefield = (_Fmtflags)(_IOSdec | _IOSoct | _IOShex);
static const _Fmtflags floatfield = (_Fmtflags)(_IOSscientific| _IOSfixed);



#define _IOSskipws 0x0001
#define _IOSunitbuf 0x0002
#define _IOSuppercase 0x0004
#define _IOSshowbase 0x0008
#define _IOSshowpoint 0x0010
#define _IOSshowpos 0x0020
#define _IOSleft 0x0040
#define _IOSright 0x0080
#define _IOSinternal 0x0100
#define _IOSdec 0x0200
#define _IOSoct 0x0400
#define _IOShex 0x0800
#define _IOSscientific 0x1000
#define _IOSfixed 0x2000
#define _IOSboolalpha 0x4000
#define _IOS_Stdio 0x8000

在vc里右击关键字选择转到定义就好了。
zhengweiyu 2005-06-30
  • 打赏
  • 举报
回复
fixed, to insert floating-point values in fixed-point format (with no exponent field).
是这里吗?

能不能具体说一下
setiosflags(4096)里面的4096代表什么可以吗?

换成其他的可不可以呢?
whatsouta 2005-06-30
  • 打赏
  • 举报
回复
路过
KenYuan2016 2005-06-30
  • 打赏
  • 举报
回复
贴这么多,懒得看。
贴核心部分就行了。
login__whf 2005-06-30
  • 打赏
  • 举报
回复
路过,顶一下!
daseny 2005-06-30
  • 打赏
  • 举报
回复
如果是iso文件,用虚拟光驱加载后安装;如果是压缩包,解压缩后安装。
如果你已将装过vc,安装后会自动链接上去,选中某个关键字,按F1就可以了。
否则在开始菜单里有MSDN的快捷方式,打开就跟在 http://msdn.microsoft.com 差不多。
如果你对上面任何词没有概念,自己去google搜索以下:)


ios_base::fmtflags

boolalpha, to insert or extract objects of type bool as names (such as true and false) rather than as numeric values.
dec, to insert or extract integer values in decimal format.
fixed, to insert floating-point values in fixed-point format (with no exponent field).
hex, to insert or extract integer values in hexadecimal format.
internal, to pad to a field width as needed by inserting fill characters at a point internal to a generated numeric field.
left, to pad to a field width as needed by inserting fill characters at the end of a generated field (left justification).
oct, to insert or extract integer values in octal format.
right, to pad to a field width as needed by inserting fill characters at the beginning of a generated field (right justification).
scientific, to insert floating-point values in scientific format (with an exponent field).
showbase, to insert a prefix that reveals the base of a generated integer field.
showpoint, to insert a decimal point unconditionally in a generated floating-point field.
showpos, to insert a plus sign in a nonnegative generated numeric field.
skipws, to skip leading white space before certain extractions.
unitbuf, to flush output after each insertion.
uppercase, to insert uppercase equivalents of lowercase letters in certain insertions.

In addition, several useful values are:

adjustfield, where internal | left | right
basefield, where dec | hex | oct
floatfield, where fixed | scientific
zhengweiyu 2005-06-30
  • 打赏
  • 举报
回复
自己顶一下
zsh6709 2005-06-29
  • 打赏
  • 举报
回复
楼上的,MSDN怎么用啊,我从网上下载的MSDN!不知道怎么用??请指教!
daseny 2005-06-29
  • 打赏
  • 举报
回复
setprecision和setw改大一点好了。
在MSDN中搜索setw会有很详细的例子。
zhengweiyu 2005-06-29
  • 打赏
  • 举报
回复
开玩笑吧?我说的是

Gross amount = $6493750.00



Gross amount = $6.5e+06.

之间的不同
oyljerry 2005-06-29
  • 打赏
  • 举报
回复

把这两句也写入文本文件撒
Processing data ... ...

Press any key to continue...
zhengweiyu 2005-06-29
  • 打赏
  • 举报
回复
说清楚一些好吗?
waynahu 2005-06-29
  • 打赏
  • 举报
回复
mark
zhengweiyu 2005-06-29
  • 打赏
  • 举报
回复
和setiosflags有关,改成下面的就ok

但是不清楚mask(格式标志位)是什么意思?setiosflags(4096)里面的4096是什么意思?

摘录
==================================
预定义的操纵算子
使用成员函数控制格式化输入输出时,每个函数调用需要写一条语句,尤其是它不能用在插入或提取运算符的表达式中,而使用操纵算子,则可以在插入和提取运算符的表达式中控制格式化输入和输出。在程序中使用操纵算字必须嵌入头文件iomanip.h


dec 十进制的输入输出
hex 十六进制的输入输出
oct 八进制的输入输出
ws 提取空白字符
ends 输出一个nul字符
endl 输出一个换行字符,同时刷新流
flush 刷新流
resetiosflags(long) 请除特定的格式标志位
setiosflags(long) 设置特定的格式标志位
setfill(char) 设置填充字符
setprecision(int) 设置输出浮点数的精确度
setw(int) 设置域宽格式变量
==================================

现在的程序
==================================
/*
Filename: ex_3_11_03.cpp
Author: xexplorer
CtrateDate: 2005-06-15
Version: 1.0
Description: Football Tickets Calculator

ModifiedDate: yyyy-mm-dd
*/

/***************[ head file ]***************/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
/***************[ declear variables ]***************/
float BTicketPrice = 0;
float STicketPrice = 0;
float PTicketPrice = 0;
float GTicketPrice = 0;
float grossAmount = 0;

int noOfBTicketsSold = 0;
int noOfSTicketsSold = 0;
int noOfPTicketsSold = 0;
int noOfGTicketsSold = 0;
int noOfAllTicketsSold = 0;

ifstream fs_sold;
ofstream fs_earn;

/***************[ statments ]***************/
fs_sold.open("ex_3_11_03_sold.txt");
fs_earn.open("ex_3_11_03_earn.txt");

cout<<"Processing data ... ..."<<endl;
fs_sold
>>BTicketPrice
>>noOfBTicketsSold
>>STicketPrice
>>noOfSTicketsSold
>>PTicketPrice
>>noOfPTicketsSold
>>GTicketPrice
>>noOfGTicketsSold;

noOfAllTicketsSold = noOfBTicketsSold +
noOfSTicketsSold +
noOfPTicketsSold +
noOfGTicketsSold;

grossAmount = BTicketPrice * noOfBTicketsSold +
STicketPrice * noOfSTicketsSold +
PTicketPrice * noOfPTicketsSold +
GTicketPrice * noOfGTicketsSold;


cout.setf(ios::fixed);
cout.setf(ios::showpoint);

fs_earn
<<"Number of all tickets sold = "
<<setprecision(2)
<<setw(4)
<<noOfAllTicketsSold
<<endl
<<"Gross amount = $"
<<setiosflags(4096) //加了但是不懂
<<setprecision(2)
<<setw(4)
<<grossAmount
<<endl;

cout
<<"Number of all tickets sold = "
<<setprecision(2)
<<setw(4)
<<noOfAllTicketsSold
<<endl
<<"Gross amount = $"
<<setprecision(2)
<<setw(4)
<<grossAmount
<<endl;



fs_sold.close();
fs_earn.close();

return 0;
}
==================================

zhengweiyu 2005-06-28
  • 打赏
  • 举报
回复
而输出的文本文件里面是这样显示的
=================================
Number of all tickets sold = 88250.
Gross amount = $6.5e+06.
=================================
zhengweiyu 2005-06-28
  • 打赏
  • 举报
回复
确实和命名有关,改成下面这样就可以了.
但是还有一个问题.

为什么终端里面是这样显示的.
=================================
Processing data ... ...
Number of all tickets sold = 88250
Gross amount = $6493750.00
Press any key to continue...
=================================

而输出的文本文件里面是这样显示的
=================================
Number of all tickets sold = 88250.
Gross amount = 6.5e+06.
=================================

有没有可能让文本文件里面的内容和终端里面的内容一样呢?

修改后的程序
=================================
/*
Filename: ex_3_11_03.cpp
Author: xexplorer
CtrateDate: 2005-06-15
Version: 1.0
Description: Football Tickets Calculator

ModifiedDate: yyyy-mm-dd
*/

/***************[ head file ]***************/
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
/***************[ declear variables ]***************/
double BTicketPrice = 0;
double STicketPrice = 0;
double PTicketPrice = 0;
double GTicketPrice = 0;
long double grossAmount = 0;
int noOfBTicketsSold = 0;
int noOfSTicketsSold = 0;
int noOfPTicketsSold = 0;
int noOfGTicketsSold = 0;
int noOfAllTicketsSold = 0;

/***************[ statments ]***************/

ifstream fs_sold;
ofstream fs_earn;

fs_sold.open("sold.txt");
fs_earn.open("earn.txt");

cout<<"Processing data ... ..."<<endl;
fs_sold
>>BTicketPrice
>>noOfBTicketsSold
>>STicketPrice
>>noOfSTicketsSold
>>PTicketPrice
>>noOfPTicketsSold
>>GTicketPrice
>>noOfGTicketsSold;

noOfAllTicketsSold = noOfBTicketsSold +
noOfSTicketsSold +
noOfPTicketsSold +
noOfGTicketsSold;

grossAmount = BTicketPrice * noOfBTicketsSold +
STicketPrice * noOfSTicketsSold +
PTicketPrice * noOfPTicketsSold +
GTicketPrice * noOfGTicketsSold;


cout.setf(ios::fixed);
cout.setf(ios::showpoint);

fs_earn
<<"Number of all tickets sold = "
<<setprecision(2)
<<setw(4)
<<noOfAllTicketsSold
<<endl
<<"Gross amount = $"
<<setprecision(2)
<<setw(4)
<<grossAmount
<<endl;


cout
<<"Number of all tickets sold = "
<<setprecision(2)
<<setw(4)
<<noOfAllTicketsSold
<<endl
<<"Gross amount = $"
<<setprecision(2)
<<setw(4)
<<grossAmount
<<endl;



fs_sold.close();
fs_earn.close();

return 0;
}
=================================

ysbcg 2005-06-28
  • 打赏
  • 举报
回复
呵呵 是啊 要用fs开头么
beyondtkl 2005-06-28
  • 打赏
  • 举报
回复
命名就有问题了。。。

居然有这样的命名:

grossamount & grossAmount
加载更多回复(1)

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧