Click to See Complete Forum and Search --> : Formatting output
shilpichaudhry
December 7th, 2002, 12:28 AM
Can anybody tell me how I can format my ouput in C++.
I want the output to be like this:
First name Last name
Mickey Mouse
Donald Duck
Steve Wonder
Thanks, Shilpi
Eisenbart
December 7th, 2002, 09:03 AM
What about the following:
void PrintNames( char* szFirst[], char* szLast[], int iCount )
{
for ( int i = 0; i < iCount; i++ )
{
cout << szFirst[i] << " " << szLast[i] << endl;
}
}
void main( void )
{
char* szFirst[] = { "Donald", "Mickey" };
char* szLast[] = { "Duck", "Mouse" };
PrintNames( szFirst, szLast, sizeof ( szFirst ) / sizeof ( char* ) );
}
It is assumed that sizeof ( szFirst ) is equal to sizeof ( szLast ).
I hope that helps...
shilpichaudhry
December 7th, 2002, 10:06 AM
Thanks for ur reply, but I am not sure whether that would halp because I have an array of structres and the first name and last name are two attributes of the structure., I need to print all the first names and last names.
Thanks, Shilpi
Mikey
December 7th, 2002, 10:35 AM
is that, what You want?
/////////////////
// data-structure
/////////////////
struct SInfo
{
char szFirstName[200];
char szLastName[200];
};
/////////////////
// print-func
/////////////////
void PrintOut(SInfo* poInfo, int iInfoCount)
{
for (int n = 0; n < iInfoCount; n++)
{
cout << poInfo[n].szFirstName << "\t" << poInfo[n].szLastName << "\n";
}
}
/////////////////
// main
/////////////////
int main(int argc, char* argv[])
{
const int INFOCOUNT = 3;
SInfo aoInfo =
{
{"Mickey", "Mouse"},
{"Donald", "Duck"},
{"Steve", "Wonder"}
};
PrintOut(aoInfo, INFOCOUNT); // formatted printing
return 0;
}
[i]Output:
Mickey Mouse
Donald Duck
Steve Wonder
Mikey ;)
Andreas Masur
December 7th, 2002, 10:48 AM
Originally posted by shilpichaudhry
Thanks for ur reply, but I am not sure whether that would halp because I have an array of structres and the first name and last name are two attributes of the structure., I need to print all the first names and last names.
Thanks, Shilpi
Well...Eisenbart showed you the basic principle...so it would not be much different for an array of structures...
#include <string>
#include <vector>
#include <iostream>
struct Name
{
std::string strFirst;
std::string strLast;
};
int main()
{
std::vector<Name> vecNames;
// Add names to vector etc.
// Print out names
for(std::vector<Name>::iterator iter = vecNames.begin();
iter != vecNames.end();
++iter)
std::cout << iter->strFirst << " " << iter->strLast << std::endl;
return 0;
}
shilpichaudhry
December 7th, 2002, 11:46 AM
Thanks for ur reply.
If I use "\t" or " ", this is the kind of output that I get.
First name Last name
Mickey Mouse
Donald Duck
Aan Ryan
I had tried that earlier,
Shilpi
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.