|
-
December 7th, 2002, 01:28 AM
#1
Formatting output
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
-
December 7th, 2002, 10:03 AM
#2
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...
-
December 7th, 2002, 11:06 AM
#3
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
-
December 7th, 2002, 11:35 AM
#4
is that, what You want?
Code:
/////////////////
// 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[INFOCOUNT] =
{
{"Mickey", "Mouse"},
{"Donald", "Duck"},
{"Steve", "Wonder"}
};
PrintOut(aoInfo, INFOCOUNT); // formatted printing
return 0;
}
Output:
Mickey Mouse
Donald Duck
Steve Wonder
Mikey
-
December 7th, 2002, 11:48 AM
#5
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...
Code:
#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;
}
-
December 7th, 2002, 12:46 PM
#6
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|