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;
}