I "miss wrote" the name of the book ... sorry ahhaha accelerated hhahah why did I write advanced ... damn =S ahha

4-2. Write a program to calculate the squares of int values up to 100. The program should
write two columns: The first lists the value; the second contains the square of that value. Use
setw (described above) to manage the output so that the values line up in columns.

PS: the program is using doubles cuz the books asks you to do so in a latter exercise

My question is ... he asks us to use setw, how can I use it in a practical way? could someone use it in this code I'm posting to exemplify it to me? thx!!

any comments or criticisms about what I did are really welcome ... =D

#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>

using std::cout;		using std::endl;
using std::cin;			using std::vector;
using std::string;		using std::setw;
using std::streamsize;	using std::setprecision;

int main()
	vector<double> numbers;
	vector<string> num;
	cout << "Enter the values: ";
	double x;
	while(cin >> x){
	sort(numbers.begin(), numbers.end());

	vector<double>::size_type size = numbers.size();

	if(size == 0){
		cout << "You must enter at least one value." << endl;
		return 1;

	cout << "The squares of the numbers you entered are:" << endl;
	for(vector<double>::size_type i = 0; i != numbers.size(); ++i){
		if(numbers[i] <= 100){
			int amount = 0;
			if(numbers[i] < 10)
				amount = 3;
			if(numbers[i] >= 10 && numbers[i] < 100)
				amount = 2;
			if(numbers[i] == 100)
				amount = 1;
			double square = numbers[i]*numbers[i];
			string spaces(amount, ' ');

			streamsize prec = cout.precision();
				cout << setprecision(4) << numbers[i] << spaces
					<< square << endl;
			cout << numbers[i] << " The number must be under 100." << endl;
	return 0;

Edited by mitrious: n/a

7 Years
Discussion Span
Last Post by daviddoria
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.