By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,696 Members | 1,486 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,696 IT Pros & Developers. It's quick & easy.

[c++]Question about setw

P: 47
Hello,

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

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

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.
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.

Expand|Select|Wrap|Line Numbers
  1. // exercise 2.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6.  
  7. using std::cout;
  8.  
  9. int main()
  10. {
  11.     int i;
  12.     for(i=0;i<=100;i++)
  13.         cout<<i<<"\t"<<i*i<<"\n";
  14.     system("PAUSE");
  15.     return 0;
  16. }
Thank you in advance.
Feb 11 '08 #1
Share this Question
Share on Google+
16 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
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..."
Feb 11 '08 #2

P: 47
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.
Feb 11 '08 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Feb 11 '08 #4

P: 47
I would insert it into a string if I know how to convert from string to int and from int to string for the first time.
A follow up question is to adapt it to another number.

The book hasn't shown that much yet.
Feb 11 '08 #5

P: 47
I know about atoi to convert from a string to a number, but I haven't found anything about the reverse.
Feb 11 '08 #6

Ganon11
Expert 2.5K+
P: 3,652
Well, the reverse of int atoi(char*) is simply char* itoa(int), but these are both legacy C functions that should be avoided in C++. A stringstream will act like a stream (that's cin and cout) that can take your number and spit out a string, or vice versa, like this:

Expand|Select|Wrap|Line Numbers
  1. int myInt = 1234;
  2. string myStr;
  3. stringstream SS;
  4. SS >> myInt; // Read a value into the stream
  5. SS << myStr; // Extract a value from the stream
  6. // Now myStr is "1234"
Feb 11 '08 #7

P: 47
Hello, sorry for the late response but I've been quite busy.

Which libraries & namespaces do I need for your example?

And I could be mistaken due to not having tested it yet and not having worked with streams yet but didn't you switch the >> and <<?

At first glance it looks to me like it should be
SS << myInt;
SS >> myStr;
Feb 14 '08 #8

weaknessforcats
Expert Mod 5K+
P: 9,197
At first glance it looks to me like it should be
SS << myInt;
SS >> myStr;
Yes it should. Just a typo.
Feb 15 '08 #9

Ganon11
Expert 2.5K+
P: 3,652
Yep. That's what I get for not actually having used the stringstream.
Feb 15 '08 #10

P: 47
Question, can a stringstream only be used once?
Because while the following code seems ok to me, setw2 also equals 3 while it should be 5.
I've run the program partially to before width2=.. to check where the error is and size seems to stay 100 while it should turn 100000.

Expand|Select|Wrap|Line Numbers
  1. // exercise 2.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <sstream>
  7. #include <iomanip>
  8.  
  9. using std::cout;        using std::stringstream;
  10. using std::string;        using std::setw;
  11.  
  12. int main()
  13. {
  14.     int i=0, max, width1, width2;
  15.     max=100;
  16.  
  17.     {stringstream store;
  18.     store<<max;
  19.     string size;
  20.     store>>size;
  21.     width1=size.length();
  22.     i=max*max;
  23.     store<<i;
  24.     store>>size;
  25.     width2=(size.length());
  26.     }
  27.  
  28.     for(i=0;i<=max;i++)
  29.         cout<<setw(width1)<<i<<setw(width2)<<i*i<<"\n";
  30.     system("PAUSE");
  31.     return 0;
  32. }
  33.  
Feb 17 '08 #11

Banfa
Expert Mod 5K+
P: 8,944
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.
That seems like a rather conveluted way to me when a simple for loop dividing by the base you are interested in until you read 0 would achieve the same thing

Expand|Select|Wrap|Line Numbers
  1. int CountDigits(int number unsigned base)
  2. {
  3.     int count;
  4.  
  5.     if (base == 0) // Prevent divide by 0
  6.     {
  7.         return 0;
  8.     }
  9.  
  10.     for(count=1; number; count++)
  11.     {
  12.         number /= base;
  13.     }
  14.  
  15.     return count;
  16. }
  17.  
Feb 17 '08 #12

weaknessforcats
Expert Mod 5K+
P: 9,197
Yes, that works but only for an int. If a double were entered, you will need to do something else to get the number of characters in the double.
Feb 17 '08 #13

P: 47
weaknessforcats am I doing something wrong in my code because I can't find anything about a stringstream only being capable of being used once.
Feb 17 '08 #14

100+
P: 542
Doesn`t `setw` set the width of the column to which the data is printed?; whereas \t sets the width of the column separating the 2 data columns.
e.g.
cout<<setw(3)<<i<<"\t"<<setw(5)<<i*i<<"\n"; //might keep the data columns tidier.
Feb 19 '08 #15

Banfa
Expert Mod 5K+
P: 8,944
weaknessforcats am I doing something wrong in my code because I can't find anything about a stringstream only being capable of being used once.
You can reuse a stringstream, however in doing so you need to reset it first before you try to use it call the method

stringstream::clear();

on your instance of the class before trying to reuse it.
Feb 19 '08 #16

Banfa
Expert Mod 5K+
P: 8,944
Doesn`t `setw` set the width of the column to which the data is printed?; whereas \t sets the width of the column separating the 2 data columns.
e.g.
cout<<setw(3)<<i<<"\t"<<setw(5)<<i*i<<"\n"; //might keep the data columns tidier.
\t does not set the width to anything, it is a character and how it is handled normally depends on the terminal to which you send it.

Commonly the terminal moves the output cursor to the next column multiple of 8.
Feb 19 '08 #17

Post your reply

Sign in to post your reply or Sign up for a free account.