Thread: Code Translation

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    71

    Exclamation Code Translation

    Hi,
    I desperately need advice on planning a program that performs Pascal-to-C++ code conversion. I'm including a sample Pascal program with its C++ equivalent. You don't need to know Pascal - if you can help me plan a program that'll be able to convert this sample code, I'll be more than satisfied.
    Leaving out the reading-from-file and writing-to-new-file parts of it, (just knowing that it'll convert line by line) how should the program be structured? (How should the conversion be divided, what should each division do?)

    I will be forever thankful to anyone who helps me with this program!

    Linette


    Pascal code:
    Code:
    program pas (input, output); 
    uses    crt;
    var
            avg: real;
            count, sum, grd, n : integer;
    
            procedure readint (lo,hi : integer; var x: integer);
    	var       nothing : char;    {here for no reason}
            begin
                 readln(x);
                 while (x<lo) or (x>hi) do
                 begin
                   writeln (x, ' is not an acceptable grade.');
                   write ('Enter a grade: ');
                   readln (x);
                 end;
            end;
    
    begin
         clrscr;
         write ('How many grades? ');
         readln (n);
         writeln;
         sum := 0;
         for count := 1 to n do
         begin
              write ('Enter a grade: ');
              readint (0,100,grd);
              sum := sum + grd;
         end;
         avg := sum / n;
         writeln;
         writeln ('The total is ',sum);
         writeln ('The average is ',avg:5:2);  {don't worry about the formatting yet}
         readkey;
    end.
    C++ equivalent:
    Code:
    #include <iostream.h>
    #include <conio.h>
    
    void readint (double lo, double hi, double &x) {
    	char nothing; /* here for no reason */
    	cin >> x;
    	while ( (x<lo) || (x>hi) ) {
    		cout << x << " is not an acceptable grade." << endl;
    		cout << "Enter a grade: ";
    		cin >> x;
    	}
    	return;
    }
    
    void main () {
    	double avg;
    	int count;
    	double sum;
    	double grd;
    	int n;
    	clrscr ();
    	cout << "How many grades? ";
    	cin >> n;
    	cout << "\n";
    	sum = 0;
    	for (count=1; count<=n; count++) {
    		cout << "Enter a grade: ";
    		readint (0,100,grd);
    		sum = sum + grd;
    	}
    	avg = sum / n;
    	cout << "\n";
    	cout << "The total is " << sum << endl;
    	cout << "The average is " << avg << endl;  /*don't worry about the formatting yet*/
    	return;
    }
    Last edited by Linette; 03-29-2002 at 08:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM