CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2002
    Posts
    96

    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

  2. #2
    Join Date
    May 2002
    Location
    Nuremberg, Germany
    Posts
    34
    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...

  3. #3
    Join Date
    Mar 2002
    Posts
    96
    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

  4. #4
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    166
    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

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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;
    }

  6. #6
    Join Date
    Mar 2002
    Posts
    96
    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
  •  





Click Here to Expand Forum to Full Width

Featured