article

MasterConsole/MasterStream Tutorial for Beginners Part 2: Parallel between MasterStream,Pascal,and C

Email
Submitted on: 1/2/2015 10:21:00 AM
By: Jeremy McAnally (from psc cd)  
Level: Beginner
User Rating: By 2 Users
Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 1918
 
     This is the second part of my tutorial for MasterStream/MasterConsole. It will parallel MasterStream to Pascal and C to show you the similarities and differences.

This article has accompanying files
 
				

There is a first part to this tutorial you need to view before you read this part. To view it click here.

Now we will parallel a MasterStream program to its Pascal and C equivalent. My goal in this is to help you relate MasterStream syntax with another language you may know. If there is enough request I will put a Java version up here.

First...the MasterStream calculator code...

#include "masterstream.h"

void printmenu();

int mastermain(char* argument)

{

console << " Welcome to the example calculator program " << endl;

console << " written specificly for planetsourcecode.com " << endl;

char yourname[100];

console << " please enter your name: ";

console >> yourname;

console << "\n Hello " << yourname << "!" << endl;

bool go = true;

while( go == true )

{

printmenu();

int menu_item;

console >> menu_item;

switch(menu_item)

{

case 0:

console << "\nerror: please enter a valid menu item" << endl;

break;

case 1:

{

int num1;

int num2;

int val;

console << "\nplease enter the first digit: ";

console >> num1;

console << "\nplease enter the second digit: ";

console >> num2;

val = num1 + num2;

console << "\n the results... " << endl;

console << num1 << " + " << num2 << " = " << val << endl;

}

break;

case 2:

{

int num1;

int num2;

int val;

console << "\nplease enter the first digit: ";

console >> num1;

console << "\nplease enter the second digit: ";

console >> num2;

val = num1 - num2;

console << "\n the results... " << endl;

console << num1 << " - " << num2 << " = " << val << endl;

}

break;

case 3:

console.clear();

break;

case 4:

go = false;

break;

}

console << "\n";

}

console.pause();

return (0);

}

void printmenu()

{

console << "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ" << endl;

console << " MENU " << endl;

console << " (1) - Add " << endl;

console << " (2) - Sub " << endl;

console << " (3) - Flush " << endl;

console << " (4) - Exit " << endl;

console << "please enter item index: ";

}

Now for the Pascal version. Notice the difference and similarities...

program example;

var

yourname : string;

go : boolean;

menuitem : integer;

num1, num2 : integer;

x, val : integer;

procedure printmenu();

begin

writeln('ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ');

writeln(' MENU ');

writeln(' (1) - Add ');

writeln(' (2) - Sub ');

writeln(' (3) - Flush ');

writeln(' (4) - Exit ');

writeln(' please enter item index: ');

end;

begin

writeln(' Welcome to the example calculator program ');

writeln(' written specificly for planetsourcecode.com " );

writeln(' please enter your name: ";

read(yourname);

writeln;

writeln('Hello ', yourname, "!";

go := true;

while go = true do

begin

printmenu;

readln(menuitem);

case menuitem of

1: begin

writeln;

writeln('please enter the first digit: ');

read(num1);

writeln;

writeln('please enter the second digit: ');

read(num2);

val := num1 + num2;

writeln;

writeln('the results... ');

writeln(num1:1, ' + ':3, num2:1,' = ':3, val:1);

end;

2: begin

writeln;

writeln('please enter the first digit ');

read(num1);

writeln;

writeln('please enter the second digit: ');

read(num2);

val := num1 - num2;

writeln;

writeln('the results... ');

writeln(num1:1, ' - ':3, num2:1, ' = ':3, val:1);

end;

3: begin

for x:=x to 50 do

writeln;

end;

4: go := false;

Otherwise begin

writeln;

writeln('error: please enter a valid menu item');

end;

end;

writeln;

end;

end.

...and finally C. The one closest to MasterStream. Basically, because all MasterStream is using modified C/C++ commands and functions. Notice the extreme similarities...

#include
#include

void printmenu();





void main()

{

char yourname[100];
int go = 1;

printf("Welcome to the example calculator program ");

printf("written specificly for planetsourcecode.com ");



printf("please enter your name: ");

scanf("%s", yourname);

printf("\n Hello %s!\n", yourname);





while( go == 1 )

{
char smenu_item[100];
int menu_item = 0;

printmenu();


scanf("%s", smenu_item);
menu_item = atoi(smenu_item);

switch(menu_item)

{

case 0:

printf("\nerror: please enter a valid menu item");

break;

case 1:

{

int num1;

int num2;

int val;

char inputdata[100];

printf("\nplease enter the first digit: ");

scanf("%s", inputdata);
num1 = atoi(inputdata);

printf("\nplease enter the second digit: ");

scanf("%s", inputdata);
num2 = atoi(inputdata);

val = num1 + num2;

printf("\n the results... ");

printf(" %i + %i = %i", num1, num2, val);

}

break;

case 2:

{
int num1;

int num2;

int val;

char inputdata[100];

printf("\nplease enter the first digit: ");

scanf("%s", inputdata);
num1 = atoi(inputdata);

printf("\nplease enter the second digit: ");

scanf("%s", inputdata);
num2 = atoi(inputdata);

val = num1 - num2;

printf("\n the results... ");

printf(" %i - %i = %i", num1, num2, val);


}

break;

case 3:

system("cls");

break;

case 4:

go = 0;

break;

}

printf("\n");

}



}



void printmenu()

{

printf("\nZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ\n");

printf(" MENU \n" );

printf(" (1) - Add \n" );

printf(" (2) - Sub \n" );

printf(" (3) - Flush \n" );

printf(" (4) - Exit \n" );

printf("please enter item index: ");

}



Now we'll take these apart, comparing all three to each other.

#include "masterstream.h"

void printmenu();

int mastermain(char* argument)

{

This is the typical MasterStream code to include the header and initialize the main program.

program example;

var

yourname : string;

go : boolean;

menuitem : integer;

num1, num2 : integer;

x, val : integer;

In the case of Pascal, you define all of your variables.

#include
#include

void printmenu();





void main()

{

In the case of C, you include the correct headers, setup your functions and main program, and then, initialize the main program.

console << " Welcome to the example calculator program " << endl;

console << " written specificly for planetsourcecode.com " << endl;

char yourname[100];

console << " please enter your name: ";

console >> yourname;

console << "\n Hello " << yourname << "!" << endl;

This portion displays a welcome message. Then it creates a character/string type variable named yourname with a fieldwidth of 100. Then it prompts you for your name and creates a use input for you to do so.

writeln(' Welcome to the example calculator program ');

writeln(' written specificly for planetsourcecode.com ' );

writeln(' please enter your name: ');

read(yourname);

writeln;

writeln('Hello ', yourname:1, '!');

This also does the same thing, only in Pascal the string "yourname" was declared in the variable declaration section at the beginning of the code.

char yourname[100];
int go = 1;

printf("Welcome to the example calculator program ");

printf("written specificly for planetsourcecode.com ");



printf("please enter your name: ");

scanf("%s", yourname);

printf("\n Hello %s!", yourname);

The C version works very similarly to the MasterStream version. The reason for this is that MasterStream is a batch executor for C/C++ programs within MasterConsole. So, in turn they will use some of the same techniques.

bool go = true;

while( go == true )

{

Creates a boolean variable "go" and sets its default value to true. Begins a while loop with the conditional being while go is true.

go := true;

while go = true do

begin

Sets the boolean value of go to true and begins a While...Do loop.

while( go == 1 )

{

Creates a while loop with the conditional while go is equal to one.

printmenu();

int menu_item;

console >> menu_item;

switch(menu_item)

{

This calls the function to print the menu. Then, creates a variable of integer type named "menu_item". Then it creates a user input to get a value for menu_item. Then it begins a case select for menu_item.

printmenu;

readln(menuitem);

case menuitem of

This calls the procedure to print the menu. Then it reads in a value for the earlier declared menuitem (_ is not allowed in Pascal variables by most versions of Pascal). Then it begins a case statement to select a case of menuitem.

char smenu_item[100];
int menu_item = 0;

printmenu();


scanf("%s", smenu_item);
menu_item = atoi(smenu_item);

switch(menu_item)

{

First a character/string type variable named smenu_item is created. Then an integer type variable named menu_item is created. After that, the program calls the function to print the menu. Then it creates a user input to get a value for smenu_item. Then it sets menu_item equal to the ascii-to-integer value of smenu_item. This sets up for the error checking. Then it egins a case select for menu_item. Here you can see one of the syntactical differences between MasterStream and C. Although, with the updated version of MasterStream you could just as easily create streams like this using the Console.printf and Console.scanf commands.

case 0:

console << "\nerror: please enter a valid menu item" << endl;

break;

If it is an invalid menu choice, then display an error message.

In Pascal, the value 0 is not returned. This is contained in the Otherwise/Else statement shown later in this explanation.

case 0:

printf("\nerror: please enter a valid menu item");

break;

If an invalid choice is made, display an error message.

case 1:

{

int num1;

int num2;

int val;

console << "\nplease enter the first digit: ";

console >> num1;

console << "\nplease enter the second digit: ";

console >> num2;

val = num1 + num2;

console << "\n the results... " << endl;

console << num1 << " + " << num2 << " = " << val << endl;

}

If you choose menu option "1", then the addition portion is intitated. At the beginning of the case code, three variables of integer type are created, num1, num2, and val respectively. Then a prompt for the first number is displayed. The next line creates a user input for the first number to add. This value is stored in num1. This process of prompt/input is duplicated for the second number, only the value is not stored in num1, but num2. Then val is set equal to num1 + num2. The next two lines display the expression and the solution, like "2 + 3 = 5".

1: begin

writeln;

writeln('please enter the first digit: ');

read(num1);

writeln;

writeln('please enter the second digit: ');

read(num2);

val := num1 + num2;

writeln;

writeln('the results... ');

writeln(num1:1, ' + ':3, num2:1,' = ':3, val:1);

end;

If you choose menu option "1", then the addition function is initiated. At the beginning of the case code, a prompt for the first number is displayed. The next line creates a user input for the first number to add. This value is stored in num1. This process of prompt/input is duplicated for the second number, only the value is not stored in num1, but num2. Then val is set equal to num1 + num2. The next two lines display the expression and the solution, like "2 + 3 = 5". function is initiated. At the beginning of the case code, a prompt for the first number is displayed. The next line creates a user input for the first number to add. This value is stored in num1. This process of prompt/input is duplicated for the second number, only the value is not stored in num1, but num2. Then val is set equal to num1 + num2. The next two lines display the expression and the solution, like "2 + 3 = 5".

case 1:

{

int num1;

int num2;

int val;

char inputdata[100];

printf("\nplease enter the first digit: ");

scanf("%s", inputdata);
num1 = atoi(inputdata);

printf("\nplease enter the second digit: ");

scanf("%s", inputdata);
num2 = atoi(inputdata);

val = num1 + num2;

printf("\n the results... ");

printf(" %i + %i = %i", num1, num2, val);

}

break;

If you choose menu option "1", then the addition portion is intitated. At the beginning of the case code, three variables of integer type and one of character/string type are created, num1, num2, and val respectively. Then a prompt for the first number is displayed. The next line creates a user input for the first number to add. The next line sets num1 equal to the ascii-to-integer value of the value of inputdata. This process of prompt/input is duplicated for the second number, only the value is not stored in num1, but num2. Then val is set equal to num1 + num2. The next two lines display the expression and the solution, like "2 + 3 = 5".

case 2:

{

int num1;

int num2;

int val;

console << "\nplease enter the first digit: ";

console >> num1;

console << "\nplease enter the second digit: ";

console >> num2;

val = num1 - num2;

console << "\n the results... " << endl;

console << num1 << " - " << num2 << " = " << val << endl;

}

If you choose menu option "2", then the subtraction portion is intitated. At the beginning of the case code, three variables of integer type are created, num1, num2, and val respectively. Then a prompt for the first number is displayed. The next line creates a user input for the first number to add. This value is stored in num1. This process of prompt/input is duplicated for the second number, only the value is not stored in num1, but num2. Then val is set equal to num1 - num2. The next two lines display the expression and the solution, like "2 - 3 = 5".

2: begin

writeln;

writeln('please enter the first digit" ');

read(num1);

writeln;

writeln('please enter the second digit: ');

read(num2);

val := num1 - num2;

writeln;

writeln('the results... ');

writeln(num1:1, ' - ':3, num2:1, ' = ':3, val:1);

end;

If you chose menu option "2", then the subtraction portion is initiated. Again, like in addition, in the beginning three variables of integer type and one of string/character type are created called num1, num2, val, and inputdata respectively. Then a prompt is created for the first number. A user input is created for the first number. Then num1 is set equal to the ascii-to-integer value of inputdata. The prompt/input is again duplicated for the second number, but instead of num1, it is stored in num2. Then val is set equal to num1 - num2. Then the next two lines display the expression and the results, like "5 - 2 = 3".

case 2:

{
int num1;

int num2;

int val;

char inputdata[100];

printf("\nplease enter the first digit: ");

scanf("%s", inputdata);
num1 = atoi(inputdata);

printf("\nplease enter the second digit: ");

scanf("%s", inputdata);
num2 = atoi(inputdata);

val = num1 - num2;

printf("\n the results... ");

printf(" %i - %i = %i", num1, num2, val);


}

break;

If you chose menu option "2", then the subtraction portion is initiated. Again, like in addition, in the beginning three variables of integer type are created called num1, num2, and val respectively. Then a prompt is created for the first number. A user input is created for the first number. This value is stored in num1. The prompt/input is again duplicated for the second number, but instead of num1, it is stored in num2. Then val is set equal to num1 - num2. Then the next two lines display the expression and the results, like "5 - 2 = 3".

case 3:

console.clear();

break;

If menu option "3" is chosen then clear the console.

3: begin

for x:=x to 50 do

writeln;

end;

If menu option "3" is chosen, then clear the console screen. Some versions of Pascal don't support CLS (namely ThinkPascal and Turbo Pascal), so I had to do the best I could. If you have a better method, then please e-mail it to me!

case 3:

system("cls");

break;

The C version is uses a system command to clear the screen.

case 4:

go = false;

break;

}

If menu option "4" is chosen then exit.

4: go := false;

Otherwise begin

writeln;

writeln('error: please enter a valid menu item');

end;

If menu option "4" is chosen then exit. This shows the otherwise option, used if an invalid menu choice (as in picking an item other than the ones declared) is made. It will display an error message. This is similar to the Case (0) in C/C++.

case 4:

go = 0;

break;

}

If menu option "4" is chosen then exit.

console << "\n";

}

console.pause();

return (0);

}

This will print a blank line then pause the console for you to look at the program. Then it will end the program and return nothing.

writeln;

end;

end.

Print a blank line and exit.

printf("\n");

}



return (0);

}

Print a blank line, and return nothing and end the program.

void printmenu()

{

console << "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ" << endl;

console << " MENU " << endl;

console << " (1) - Add " << endl;

console << " (2) - Sub " << endl;

console << " (3) - Flush " << endl;

console << " (4) - Exit " << endl;

console << "please enter item index: ";

}

This is the printmenu function. It prints the menu for choices into the MasterConsole window. It merely prints the text to MasterConsole. Structuring the progam this way makes it easier to read and the function recallable from any portion of the program.

procedure printmenu();

begin

writeln('ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ');

writeln(' MENU ');

writeln(' (1) - Add ');

writeln(' (2) - Sub ');

writeln(' (3) - Flush ');

writeln(' (4) - Exit ');

writeln(' please enter item index: ');

end;

This is the printmenu procedure. It prints the menu for choices into the system console. It merely prints the text. Structuring the progam this way makes it easier to read and the procedure recallable from any portion of the program. procedure. It prints the menu for choices into the system console. It merely prints the text. Structuring the progam this way makes it easier to read and the procedure recallable from any portion of the program.

void printmenu()

void printmenu()

{

printf("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ" << );

printf(" MENU " << );

printf(" (1) - Add " << );

printf(" (2) - Sub " << );

printf(" (3) - Flush " << );

printf(" (4) - Exit " << );

printf("please enter item index: ";

}

This is the printmenu function. It prints the menu for choices into the system console. Structuring the progam this way makes it easier to read and the function recallable from any portion of the program. It merely prints the text to the system console.

Well, this is the end of the tutorial to date. I will be updating and hopefully come out with a tutorial that will explain the source to MasterStream and MasterConsole.

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.