11 Replies - 2984 Views - Last Post: 29 August 2009 - 10:43 PM Rate Topic: -----

#1 Lahben  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-May 09

Formatting String using the setw function

Post icon  Posted 29 August 2009 - 09:25 PM

Hi...am trying to format my double dimension string array using the setw function...so as to display a nicely formatted table...but nothing seems to work....

#include <iomanip>;

for(int i=0;i<ROW;i++)
	  {
		for(int j = 0; j<COLUMN;j++)
		  {
			cout<<Jobtitles[i][j] <<set(20);
		  }
			cout<<endl;
	  }
	 cout << endl;
}



** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Formatting String using the setw function

#2 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5182
  • View blog
  • Posts: 26,886
  • Joined: 10-May 07

Re: Formatting String using the setw function

Posted 29 August 2009 - 09:33 PM

COLUMN, ROW, & Jobtitles are not defined, as well as you have no main function.

Is this all the code that you have, or is there more?

You also have three closing brackets, & only two open.
Was This Post Helpful? 0
  • +
  • -

#3 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Formatting String using the setw function

Posted 29 August 2009 - 09:35 PM

you could try this:
int s;
for(int i=0;i<ROW;i++)
	  {
		s=2;
		for(int j = 0; j<COLUMN;j++)
		  {
		   s+=3;
			cout<<setw(5+s)<<Jobtitles[i][j];
		  }
			cout<<endl;
	  }
	 cout << endl;
}



I assume this is only part of your code.

This post has been edited by poncho4all: 29 August 2009 - 09:37 PM

Was This Post Helpful? 0
  • +
  • -

#4 Lahben  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-May 09

Re: Formatting String using the setw function

Posted 29 August 2009 - 09:54 PM

Thanks for the response...
But sorry tried the code...but it didn't work

Output looked something like this:
Jobtitle Salary
Trainee_Programmer15000
Programmer20000
Analyst_Programmer25000

Appreciate your help...

View Postponcho4all, on 29 Aug, 2009 - 08:35 PM, said:

you could try this:
int s;
for(int i=0;i<ROW;i++)
	  {
		s=2;
		for(int j = 0; j<COLUMN;j++)
		  {
		   s+=3;
			cout<<setw(5+s)<<Jobtitles[i][j];
		  }
			cout<<endl;
	  }
	 cout << endl;
}



I assume this is only part of your code.

Was This Post Helpful? 0
  • +
  • -

#5 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Formatting String using the setw function

Posted 29 August 2009 - 09:59 PM

Well play with the number to get the separation you need, thing is i didnt knew the lengh of your output.
Was This Post Helpful? 0
  • +
  • -

#6 Lahben  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-May 09

Re: Formatting String using the setw function

Posted 29 August 2009 - 10:04 PM

Sorry...I tried the second suggestion but it didn't worked...

By the way, how do we use the setw function to format string...

Please find attached my program...

View Postponcho4all, on 29 Aug, 2009 - 08:59 PM, said:

Well play with the number to get the separation you need, thing is i didnt knew the lengh of your output.

Was This Post Helpful? 0
  • +
  • -

#7 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Formatting String using the setw function

Posted 29 August 2009 - 10:11 PM

Can i see more of your code i think im not understanding exactly what you want if its gonna be outputted line by line try
cout<<setw(20)<<Jobtitles[i][j];

as you originaly had it but the setw()fuction has to be used before the output text.

In another subject im guessing JobTitles[here are the jobtitles?][and here the salaries?]

*off topic
The correct way to use setw() would be

cout<<JobTitles<<setw(20)<<Salary;


This would give an output of

Trainee_Programmer 15000

This post has been edited by poncho4all: 29 August 2009 - 10:17 PM

Was This Post Helpful? 0
  • +
  • -

#8 Lahben  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-May 09

Re: Formatting String using the setw function

Posted 29 August 2009 - 10:16 PM

Sorry....that didn't work as well...PLease find attached part of my program
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <iomanip>
#include <string>


const int MAXSIZE = 12; //Number of Staff
const int ROW = 7;
const int COLUMN = 2;

void print_salary_table(string [][COLUMN],int);
int salary_index(string);
void Read_Store_Table2(ifstream&,string [][COLUMN]);
int anuual_budget(string [][COLUMN],int&,int&,int&,int&,int&,int&,int&,string[][COLUMN]);
void Print_Annual_budget( int&,int&,int&,int&,int&,int&,int&,string[][COLUMN]);

int main()
{

	  string Jobtitles[ROW][COLUMN];  //Table 1 declared as a double-dimension array
	  string Stafflist[MAXSIZE][COLUMN]; //Table 2
	  int index;//[MAXSIZE];
	  int i=0;
	  int counterT_P =0; //Trainee Programmer counter - to store the number of trainee programmers
	  int counterP =0;   //Programmer counter - to store the number of programmers
	  int counterA_P =0;	//Analyst Programmer counter - to store the number of analyst programmers
	  int counterS_A_P =0; //Senior Analyst Programmer counter - to store the number of senior analyst programmers
	  int counterD_A =0;	//Database Administrator  counter - to store the number of database administrator
	  int counterA_M =0;   //Assistant Manager counter - to store the number of assistant manager programmers
	  int counterM =0;	//Manager counter - to store the number of managers
	  int budget;

	  string str;

	  cout << "This program computes the annual budgetary report for\n"
		   << "IT South Pacific, a software company based in Suva.";
	  cout <<endl<<endl;
	  ifstream infile;


	  infile.open("E:\\Semester 2\\CS 112\\Assignment\\jobtitles.txt",ios::in);

	  if(infile.fail())
		 {
			cerr << "File not found!" << endl;//Check if file has been read correctly
		 }
	  else
		 {

			getline(infile,str);
		   while(infile>>ws && !infile.eof())
			{
				for(int i=0;i<ROW;i++)
				   {
					 for(int j = 0; j<COLUMN;j++)
						{
						  infile >> Jobtitles[i][j];
						}
				   }
				infile.close();
			}

		 }
		 print_salary_table(Jobtitles,ROW);
		 index = salary_index("Jobtitles");
		 Read_Store_Table2(infile,Stafflist);
		 budget =  anuual_budget( Stafflist,counterT_P,counterP,counterA_P,counterS_A_P,counterD_A,counterA_M,counterM,Jobtitles);
		 Print_Annual_budget ( counterT_P,counterP,counterA_P,counterS_A_P,counterD_A,counterA_M,counterM,Jobtitles);

	  system("PAUSE");
	  return 0;
}

void print_salary_table(string Jobtitles[][COLUMN],int ROW )
{
	//cout<<"Jobtitle"<<"\t" << "\t"<< "\t"<<"Salary\n";

	for(int i=0;i<ROW;i++)
	  {
		for(int j = 0; j<COLUMN;j++)
		  {
			cout<<setw(20)<<Jobtitles[i][j];
		  }
			cout<<endl;
	  }
	 cout << endl;
}


int salary_index(string Jobtitles)
{
  if(Jobtitles == "Trainee_Programmer")
   {
	   return  0;
   }
   else if(Jobtitles == "Programmer")
   {
	   return 1;
   }
   else if(Jobtitles == "Analyst_Programmer")
   {
		return  2;
   }
   else if(Jobtitles == "Senior_Analyst_Programmer")
   {
		return  3;
   }
   else if(Jobtitles == "Database_Administrator")
   {
		return  4;
   }
   else if(Jobtitles == "Assistant_Manager")
   {
		return  5;
   }
   else if(Jobtitles == "Manager")
   {
	   return  6;
   }

}

void Read_Store_Table2(ifstream&infile,string Stafflist[][COLUMN])
{
	 string str;
	 infile.open("E:\\Semester 2\\CS 112\\Assignment\\staffmembers.txt",ios::in);

	  if(infile.fail())
		 {
			cerr << "File not found!" << endl;
		 }
	  else
		 {

			getline(infile,str);

			while(infile >> ws && !infile.eof())
			  {
				 for(int i=0;i<MAXSIZE;i++)
				   {
					   for(int j = 0; j<COLUMN;j++)
						 {
						   infile >> Stafflist[i][j];
						 }
				   }
			  }
		 }
}



int anuual_budget(string Stafflist[][COLUMN],int& counterT_P,int& counterP,int& counterA_P,int& counterS_A_P,int& counterD_A,int& counterA_M,int& counterM,string Jobtitles[][COLUMN])
{

   for(int i=0; i<MAXSIZE;i++)
   {
	  if(Stafflist[i][1] == "Trainee_Programmer")//Each array element is compared to the positions and respective counters are updated
		 {
			counterT_P++;//Trainee Programmer counter is incremented whenever Trainee Programmer is taken into account
		 }
	  if(Stafflist[i][1] == "Programmer")
		 {
			counterP++; //Trainee Programmer counter is incremented whenever Trainee Programmer is taken into account
		 }
	  if(Stafflist[i][1] == "Analyst_Programmer")
		 {
			counterA_P++; //Analyst Programmer counter is incremented whenever Analyst Programmer is taken into account
		 }
	  if(Stafflist[i][1] == "Senior_Analyst_Programmer")
		 {
			counterS_A_P++;//Senior Analyst Programmer counter is incremented whenever Senior Analyst Programmer is taken into account
		 }
	  if(Stafflist[i][1] == "Database_Administrator")
		 {
			counterD_A++; //Database Administrator counter is incremented whenever Database Administrator is taken into account
		 }
	  if(Stafflist[i][1] == "Assistant_Manager")
		 {
			counterA_M++; //Assistant Manager counter is incremented whenever Assistant Manager is taken into account
		 }

	  if(Stafflist[i][1] == "Manager")
		{
			counterM++;  //Manager counter is incremented whenever a Manager is taken into account
		}
   }
}

void Print_Annual_budget( int& counterT_P,int& counterP,int& counterA_P,int& counterS_A_P,int& counterD_A,int& counterA_M,int& counterM,string Jobtitles[][COLUMN])
{
	   cout<<endl;
	   int counter[7] ={ counterT_P,counterP,counterA_P,counterS_A_P,counterD_A,counterA_M,counterM};
	   int total[7] = {0}; //A 1-D array initialising total salary of each position to 0
	   cout << "Annual Budget..." << endl<<endl;
	   cout << "Job Titles" << "\t" << "\t" << "Total Salary"; //Table Header
	   cout << endl;
	   for(int i=0;i<7;i++)
	   {
		  total[i] = counter[i]  * atoi(Jobtitles[i][1].c_str());
		  cout<<Jobtitles[i][0]<<setw(20)<<total[i]<<endl;
	   }
	   cout << endl << endl;
}







View Postponcho4all, on 29 Aug, 2009 - 09:11 PM, said:

Can i see more of your code i think im not understanding exactly what you want if its gonna be outputted line by line try
cout<<setw(20)<<Jobtitles[i][j];

as you originaly had it but the setw()fuction has to be used before the output text.


*mod edit: added code tags: :code:
Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Formatting String using the setw function

Posted 29 August 2009 - 10:34 PM

Here is an example:
#include <iostream>
#include <iomanip>
#include <string>
#include <map>

using namespace std;

int main() {
	string cols[] = {"Item","Phobia","Meaning" };
	int colWidths[] = { 5, 30, 40 };
	map<string, string> dict;
	dict["Acrophobia"]="Fear of heights";
	dict["Anablephobia"]="Fear of looking up";
	dict["Barophobia"]="fear of gravity";
	dict["Caligynephobia"]="Fear of beautiful women";
	dict["Epistemophobia"]="Fear of knowledge.";
	dict["Googleophobia"]="Feat of looking something up on the internet";
	dict["Hadephobia"]="Fear of hell.";
	dict["Hexakosioihexekontahexaphobia"]="Fear of the number 666.";
	dict["Hippopotomonstrosesquipedaliophobia"]="Fear of long words.";
	dict["Logizomechanophobia"]="Fear of computers.";
	map<string, string>::iterator it;
	cout << setw(colWidths[0]) << left << cols[0] << "| " 
		 << setw(colWidths[1]) << left << cols[1] << "| "
		 << setw(colWidths[2]) << left << cols[2] << endl;
	cout << setfill('-') << setw(75) << "" << setfill(' ') << endl;
	int i = 0;
	for(it = dict.begin(); it != dict.end(); it++, i++) {
		cout << setw(colWidths[0]) << i << "| " 
			 << setw(colWidths[1]) << (*it).first << "| " 
			 << setw(colWidths[2]) << (*it).second << endl;
	}

	return 0;

}

Was This Post Helpful? 0
  • +
  • -

#10 Lahben  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-May 09

Re: Formatting String using the setw function

Posted 29 August 2009 - 10:37 PM

If you were using...the DevC++ 4.0 version...how would I declare the left(bolded)...Is it in this format...setiosflags(ios::left)???

View PostNickDMax, on 29 Aug, 2009 - 09:34 PM, said:

Here is an example:
#include <iostream>
#include <iomanip>
#include <string>
#include <map>

using namespace std;

int main() {
	string cols[] = {"Item","name","whatever" };
	int colWidths[] = { 5, 30, 40 };
	map<string, string> dict;
	dict["Acrophobia"]="Fear of heights";
	dict["Anablephobia"]="Fear of looking up";
	dict["Barophobia"]="fear of gravity";
	dict["Caligynephobia"]="Fear of beautiful women";
	dict["Epistemophobia"]="Fear of knowledge.";
	dict["Googleophobia"]="Fear of looking something up on the internet";
	dict["Hadephobia"]="Fear of hell.";
	dict["Hexakosioihexekontahexaphobia"]="Fear of the number 666.";
	dict["Hippopotomonstrosesquipedaliophobia"]="Fear of long words.";
	dict["Logizomechanophobia"]="Fear of computers.";
	map<string, string>::iterator it;
	cout << setw(colWidths[0]) << [b]left [/b] << cols[0] << " | " 
		 << setw(colWidths[1]) << [b]left [/b] << cols[1] << " | "
		 << setw(colWidths[2]) << [b]left [/b] << cols[2] << endl;
	int i = 0;
	for(it = dict.begin(); it != dict.end(); it++, i++) {
		cout << setw(colWidths[0]) << i << " | " 
			 << setw(colWidths[1]) << (*it).first << " | " 
			 << setw(colWidths[2]) << (*it).second << endl;
	}

	return 0;

}

Was This Post Helpful? 0
  • +
  • -

#11 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Formatting String using the setw function

Posted 29 August 2009 - 10:41 PM

mmm.....

[code]code goes here[/code]

You could try
cout<<setw(20)<<Jobtitles[i][j]<<" ";


Hope that makes it

[edit]
Say nick why is it that the standard way is not working in that code?
I use what i posted on my matrix and it works fine.
How is this different?

This post has been edited by poncho4all: 29 August 2009 - 10:50 PM

Was This Post Helpful? 0
  • +
  • -

#12 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Formatting String using the setw function

Posted 29 August 2009 - 10:43 PM

the left manipulator is part of the std namespace so all you have to do to declare it is include the "iomanip" header and then ensure you dereference the right namespace.

Perhaps I am not understanding what you are asking....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1