Home

C++/CLI Data Reading

 

Introduction

So far, we have used the Console class' Write() and WriteLine() to display things on the screen. While Write() is used to display something on the screen, you can use Read() to get a value from the user. To use it, the name of a variable can be assigned to it. The formula used is:

VariableName = Console::Read();

This simply means that, when the user types something and presses Enter, what the user had typed would be given (the word is assigned) to the variable specified on the left side of the assignment operator.

Read() doesn't always have to assign its value to a variable. For example, it can be used on its own line, which simply means that the user is expected to type something but the value typed by the user would not be used for any significant purpose.

Besides Read(), you can use ReadLine(). Like WriteLine(), after performing its assignment, ReadLine() sends the caret to the next line. Otherwise, it plays the same role as Read().

Practical LearningPractical Learning: Introducing Data Reading

  1. To start a new program, launch Microsoft Visual C++ 2005
  2. On the main menu, click File -> New -> Project...
  3. On the left side, make sure that Visual C++ is selected. In the Templates list, click CLR Empty Project
  4. In the Name box, replace the string with RealEstate6 and click OK
  5. On the main menu, click Project -> Add New Item...
  6. To create a header file, in the Templates list, click Header File (.h)
  7. Set the Name to Property and click Add
  8. Complete the file as follows:
     
    #pragma once
    using namespace System;
    
    namespace RealEstate
    {
        public ref class CProperty
        {
        public:
            long PropertyNumber;
            String ^ Address;
            String ^ City;
            String ^ State;
            String ^ ZIPCode;
            int Bedrooms;
            float Bathrooms;
            int YearBuilt;
            double MarketValue;
        };
    }
  9. To create another header file, on the main menu, click Project -> Add New Item...
  10. In the Templates list, make sure Header File (.h) is selected.
    Set the Name to House and press Enter
  11. Complete the file as follows:
     
    #pragma once
    
    #include "Property.h"
    
    using namespace System;
    
    namespace RealEstate
    {
        public ref class CHouse : public CProperty
        {
        public:
            Byte Stories;
            int  Condition;
            unsigned NumberOfGarageSpaces;
        };
    }
  12. To create one more header file, on the main menu, click Project -> Add New Item...
  13. In the Templates list, make sure Header File (.h) is selected.
    Set the Name to SingleFamily and click Add
  14. Complete the file as follows:
     
    #pragma once
    #include "House.h"
    
    using namespace System;
    
    namespace RealEstate
    {
        public ref class CSingleFamily : public CHouse
        {
        public:
            __wchar_t Style;
            double LotSizeInSqFt;
        };
    }
  15. To create a source file, on the main menu, click Project -> Add New Item...
  16. In the Templates list, click C++ File (.cpp)
  17. In the New box, type Exercise and click Add
  18. In the empty file, type:
     
    #include "SingleFamily.h"
    using namespace RealEstate;
    
    int main()
    {
        CSingleFamily ^ home = gcnew CSingleFamily;    
    
        Console::WriteLine();
        return 0;
    }
  19. To execute the application, on the main menu, click Debug -> Start Without Debugging
  20. Click Yes 
  21. Close the DOS window

String Value Request

In most assignments of your programs, you will not know the value of a string when writing your application. For example, you may want the user to provide such a string. To request a string (or any of the variables we will see in this lesson), you can call Read() or ReadLine() and assign it to the name of the variable whose value you want to retrieve. Here is an example:

using namespace System;

int main()
{
    String ^ FirstName;

    Console::Write(L"Enter First Name: ");
    FirstName = Console::ReadLine();

    Console::WriteLine();

    return 0;
}

Character and Number Request

Everything the user types is a string and the compiler may not analyze it without your asking it to do so. Therefore, if you want to get a number from the user, first request a string. Here is an example:

using namespace System;

int main()
{
    int Number;
    String ^ strNumber;

    strNumber = Console::ReadLine();

    Console::WriteLine();

    return 0;
}

After getting the string, you must convert it to a number. To perform this conversion, each data type of the .NET Framework provides a mechanism called Parse. To use Parse, type the data type, followed by the :: operator, followed by Parse, and followed by parentheses. In the parentheses of Parse, type the string that you got from the user. Here is an example:

using namespace System;

int main()
{
    int Number;
    String ^strNumber;

    strNumber = Console::ReadLine();
    Number = int::Parse(strNumber);

    Console::WriteLine();

    return 0;
}

An advanced (more complicated) but faster way to do this is to type Console::ReadLine() in the parentheses of Parse(). This has the same effect. Here is an example:

using namespace System;

int main()
{
    int Number;
	
    Number = int::Parse(Console::ReadLine());

    return 0;
}

Practical LearningPractical Learning: Requesting Data

  1. To request different values from the user, change the contents of the Exercise.cpp file as follows:
     
    #include <iostream>
    #include "SingleFamily.h"
    
    using namespace std;
    using namespace RealEstate;
    
    int main()
    {
        String ^ strTitle1 = L"=//= Altair Realty =//=";
        String ^ strTitle2 = L"-=- Properties Inventory -=-";
        CSingleFamily ^ home = gcnew CSingleFamily;    
    
        Console::WriteLine(strTitle1);
        Console::WriteLine(strTitle2);
    Console::WriteLine("To create a listing, enter the following information");
        Console::Write("Property #:    ");
        home->PropertyNumber = long::Parse(Console::ReadLine());
        Console::WriteLine("Property Condition");
        Console::WriteLine("0. Unknown");
        Console::WriteLine("1. Excellent");
        Console::WriteLine("2. Good (may need minor repair");
        Console::WriteLine("3. Acceptable (needs major repair)");
        Console::WriteLine("4. Even (land is more important)");
        Console::Write("Your Choice:    ");
        home->Condition = int::Parse(Console::ReadLine());
        Console::Write("Street Address: ");
        home->Address = Console::ReadLine();
        Console::Write("City:           ");
        home->City = Console::ReadLine();
        Console::Write("State:          ");
        home->State = Console::ReadLine();
        Console::Write("ZIP Code:       ");
        home->ZIPCode = Console::ReadLine();
        Console::Write("Bedrooms:       ");
        home->Bedrooms = int::Parse(Console::ReadLine());
        Console::Write("Bathrooms:      ");
        home->Bathrooms = float::Parse(Console::ReadLine());
        Console::Write("Stories:        ");
        home->Stories = int::Parse(Console::ReadLine());
        Console::Write("Year Built: ");
        home->YearBuilt = int::Parse(Console::ReadLine());
        Console::WriteLine("Style");
        Console::WriteLine("U. Unknown");
        Console::WriteLine("S. Split Level");
        Console::WriteLine("C. Colonial");
        Console::WriteLine("G. Georgial");
        Console::Write("Your Choice:    ");
        home->Style = __wchar_t::Parse(Console::ReadLine());
        Console::Write("Garage Spaces:  ");
        home->NumberOfGarageSpaces = int::Parse(Console::ReadLine());
        Console::Write("Total Lot Size: ");
        home->LotSizeInSqFt = double::Parse(Console::ReadLine());
        Console::Write("Market Value:   ");
        home->MarketValue = double::Parse(Console::ReadLine());
      
        system("cls");
    
        Console::WriteLine(strTitle1);
        Console::WriteLine(strTitle2);
        Console::WriteLine("=//= Property Listing =//=");
        Console::Write("Property #:   ");
        Console::WriteLine(home->PropertyNumber);
        Console::Write("Address:      ");
        Console::WriteLine(home->Address);
        Console::Write("              ");
        Console::Write(home->City);
        Console::Write(", ");
        Console::Write(home->State);
        Console::Write(" ");
        Console::WriteLine(home->ZIPCode);
        Console::Write("Condition:    ");
        Console::WriteLine(home->Condition);
        Console::Write("Style:        ");
        Console::WriteLine(home->Style);
        Console::Write("Bedrooms:     ");
        Console::WriteLine(home->Bedrooms);
        Console::Write("Bathrooms:    ");
        Console::WriteLine(home->Bathrooms);
        Console::Write("Stories:      ");
        Console::WriteLine(home->Stories);
        Console::Write("Year Built:   ");
        Console::WriteLine(home->YearBuilt);
        Console::Write("Garage Spaces:  ");
        Console::WriteLine(home->NumberOfGarageSpaces);
        Console::Write("Lot Size:     ");
        Console::WriteLine(home->LotSizeInSqFt);
        Console::Write("Market Value: ");
        Console::WriteLine(home->MarketValue);
    
        Console::WriteLine();
        return 0;
    }
  2. Execute the application to test it. Here is an example:
     
    =//= Altair Realty =//=
    -=- Properties Inventory -=-
    =//= Property Listing =//=
    Property #:   802400
    Address:      8244 Ramses Avenue
                  College Park, MD 20707
    Condition:    1
    Style:        C
    Bedrooms:     4
    Bathrooms:    3.5
    Stories:      3
    Year Built:   1988
    Garage Spaces:  2
    Lot Size:     7144.64
    Market Value: 650845
    
    Press any key to continue . . .
  3. Close the DOS window

 

 

Previous Copyright © 2006 FunctionX, Inc. Next