[c ++]关于setw的问题 [英] [c++]Question about setw

查看:110
本文介绍了[c ++]关于setw的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我正在用Andrew Koenig自学C ++加速C ++。


我有一个小的第73页上的练习问题。

Hello,

I''m teaching myself C++ with Accelerated C++ by Andrew Koenig.

I''m having a small problem with an exercise at page 73.


编写一个程序来计算最多100个int值的平方。程序应写两列:首先列出值;第二个包含该值的平方。使用setw(如上所述)来管理输出,以便值列在列中。
Write a program to calculate the squares of int values up to 100. The program should write two columns: The first list the value; the second contains the square of that value. Use setw(described above) to manage the output so that the values line op in columns.



现在我想知道我应该怎么做setw,最简单的方法似乎以某种方式得到我的计数器有多少个字符,然后将setw设置为第一部分那个长度+ 1。


我想知道的第二件事是:为什么要使用setw而不仅仅是\ t在这种情况下。


这是我用\t写的解决方案,但是我想用setw来解决它,因为那就是问题。

Now I was wondering how I should do that with setw, easiest way seems to somehow get how many characters my counter is, and then set setw for the first part to that length+1.

The second thing I''m wondering about is: why use setw and not just \t in this case.

This is what I wrote as solution with \t but I''d like to solve it with setw since that''s the question.

展开 | 选择 | Wrap | 行号

推荐答案

它说& 两个colunns所以我不知道为什么\t不行。


你在使用setw时遇到什么问题?


BTW:不需要程序结尾的系统(PAUSE)。使用Visual Studio.NET,使用Start without Debugging执行程序。程序将在main()的末尾暂停,并按按任意键继续...
It says "two colunns" so I don''t know why \t isn''t OK.

What was your problem in using setw??

BTW: system("PAUSE") at the end of your program is not required. With Visual Studio.NET, execute your program using "Start without Debugging". The program will pause at the end of main() with "Press any key to continue..."


我遇到的问题是确定整数的长度。


要么我使用左边的allign并使用最大计数器+1的数字的宽度。


或者我使用的是正确的最大结果数字的数字+ 1.


但我不知道如何获得这个数字的位数。
The problem I''m having is determening the length of the integer.

Either I use a left allign and use a width of the ammount of digits of the largest counters +1.

Or I use a right allign of the ammount of digits of the largest result + 1.

But I don''t know how to get how many digits the number is.

您是否考虑过在字符串流中插入整数并将其恢复为字符串?这样string :: size()就是整数中的字符数。
Have you considered inserting the integer in a stringstream and recovering it as a string?? That way string::size() would be the number of characters in the integer.


这篇关于[c ++]关于setw的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆