topical media & game development

talk show tell print

basic-program-code-04-Ex4-17.c

? / basic-program-code-04-Ex4-17.c


  // Ex4_17.cpp : main project file.
  // Creating a custom format string
  
  include <stdafx.h>
  
  using namespace System;
  
  int main(array<System::String ^> ^args)
  {
    array<int>^ values = { 2, 456, 23, -46, 34211, 456, 5609, 112098,
      234, -76504, 341, 6788, -909121, 99, 10};
    String^ formatStr1 = "{0,";          // 1st half of format string
    String^ formatStr2 = "}";            // 2nd half of format string
    String^ number;                      // Stores a number as a string
  
    // Find the length of the maximum length value string
    int maxLength = 0;                   // Holds the maximum length found
    for each(int value in values)
    {
      number = "" + value;               // Create string from value
      if(maxLength<number->Length)
        maxLength = number->Length;
    }
  
    // Create the format string to be used for output
    String^ format = formatStr1 + (maxLength+1) + formatStr2;
  
    // Output the values
    int numberPerLine = 3;
    for(int i = 0 ; i< values->Length ; i++)
    {
      Console::Write(format, values[i]);
      if((i+1)\%numberPerLine == 0)
        Console::WriteLine();
    }
      return 0;
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.