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.
- // exercise 2.cpp : Defines the entry point for the console application.
-
//
-
-
#include "stdafx.h"
-
#include <iostream>
-
-
using std::cout;
-
-
int main()
-
{
-
int i;
-
for(i=0;i<=100;i++)
-
cout<<i<<"\t"<<i*i<<"\n";
-
system("PAUSE");
-
return 0;
-
}
Thank you in advance.