View Full Version : Need help translating code
franznietzsche
Jan30-05, 09:02 AM
I wrote an (small) AI script for twenty questions in perl, however i need the code translated into C++ (i ahve some knowledge of C++, but not enough to write this program). Thanx for the help:
#!/usr/bin/perl
use warnings;
$ans = 0;
$qnumber = 1;
while (($ans == 0)&& ($qnumber < 20)) {
print "Ask a question: \n";
$_ = <STDIN>;
if (/end/ && /vowel/ && (/everything/ || /anything/)){
print "You got it right!\n";
$ans = 1;
} elsif (/a$/ or /e$/ or /i$/ or /o$/ or /u$/ or /a\?$/ or /e\?$/ or /i\?$/ or /o\?$/ or /u\?$/ ) {
print "Yes\n";
} else {
print "No\n";
}
$qnumber ++ ;
if ($qnumber == 20){
print "You've run out of questions. Better luck next time!\n";
}
}
franznietzsche
Jan31-05, 02:05 AM
Or at least direct me to a resource that would cover everything needed to write this in C++?
ramollari
Jan31-05, 05:08 AM
Hi, you can view the C++ programming tutorial at this forum, or you can download "Thinking in C++" from Bruce Eckel at http://www.linuxguruz.org/ebooks/eckel/TICPP-2nd-ed-Vol-one.zip to cover the basics.
franznietzsche
Jan31-05, 11:49 AM
yeah the C++ tutorial here doesn't cover regular expressions and patterns, i already checked that. The code checks for a vowel at the end of whatever string is input into it.
noelhustler
Jan31-05, 12:29 PM
You don't need to use regular expressions in c in order to find substrings and characters.
You can use the c functions strstr and strchr.
By the way, the line in your perl program:
elsif (/a$/ or /e$/ or /i$/ or /o$/ or /u$/ or /a\?$/ or /e\?$/ or /i\?$/ or /o\?$/ or /u\?$/ ) {
would be better written:
elsif(/[aeiou]\??$/)
franznietzsche
Jan31-05, 12:46 PM
You don't need to use regular expressions in c in order to find substrings and characters.
You can use the c functions strstr and strchr.
By the way, the line in your perl program:
elsif (/a$/ or /e$/ or /i$/ or /o$/ or /u$/ or /a\?$/ or /e\?$/ or /i\?$/ or /o\?$/ or /u\?$/ ) {
would be better written:
elsif(/[aeiou]\??$/)
I'm rather new to perl, but yeah i have condensed it quite a bit from what i posted here.
I have a very limited knowledge of C++ (i can write a program to solve equations, but thats about all). HOw do you use those functions?
noelhustler
Jan31-05, 12:52 PM
strstr (http://www.cplusplus.com/ref/cstring/strstr.html)
and
strchr (http://www.cplusplus.com/ref/cstring/strchr.html)
You don't need to use c++ to write a program like that. You can stick with plain c.
most of the program is easy to change, but the checking for the expressions needs some more work. I do not know what the Perl expressions mean, especially (/everything/ || /anything/) seems mysterious to me, but anyway here is the C++ translation without the conditionals:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int ans = 0;
int qnumber = 1;
string question;
while (ans == 0 && qnumber < 20) {
cout << "Ask a question: " << endl;
cin >> question;
if (........){
cout << "You got it right!" << endl;
ans = 1;
} elsif (........) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
qnumber++;
if (qnumber == 20){
cout << "You've run out of questions. Better luck next time!" << endl;
}
}
}
The code checks for a vowel at the end of whatever string is input into it.
You can do that with:
char c = question[strlen(question) - 1];
if (c == 'a' || c == 'e' || c == 'u' || c == 'o' || c == 'i')
......
franznietzsche
Jan31-05, 06:20 PM
Well, if it helps, this is not class work.
It was a late night replacement for sleep (my insomnia is that bad). But i wanted to send it to a friend for him to toy around with, but i didn't know C++ well enough to write this (its the only language he's learned so far), so i had to write it in Perl.
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.