Sunday, January 22, 2012

Console::Write()/WriteLine() with arguments

Modify from the exercise in Hello Visual C++ CLR Console Application using Microsoft Visual Studio 11 Developer Preview to demonstrate the use of Console::Write()/WriteLine() functions with arguments.

Console::Write()/WriteLine() with arguments

// CLRConsole.cpp : main project file.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
int number1 = 2;
float number2 = 1.23456f;

Console::WriteLine(L"Use Write()/WriteLine() without argument!");
Console::Write(L"Do you know? ");
Console::Write(number1);
Console::Write(L" + ");
Console::Write(number2);
Console::Write(L" = ");
Console::WriteLine(number1 + number2);
//The follow line have Error!
Console::WriteLine(L"Do you know? " + number1 + " + " + number2 + " = " + number1 + number2);

Console::WriteLine(L"");
Console::WriteLine(L"With arguments!");
Console::WriteLine(L"Do you know? {0} + {1} = {2}", number1, number2, number1 + number2);
Console::WriteLine(L"Do you know? {1} + {0} = {2}", number1, number2, number1 + number2);

Console::WriteLine(L"");
Console::WriteLine(L"You can also specify how much digits after decimal point:");
Console::WriteLine(L"Do you know? {0:F2} + {1:F2} = {2:F2}", number1, number2, number1 + number2);
Console::ReadLine();
return 0;
}


Related:
- More output format for Console::Write()/WriteLine()

No comments:

Post a Comment