Translate Perl AI Script to C++ for 20 Questions: Expert Help Available

  • Thread starter Thread starter franznietzsche
  • Start date Start date
  • Tags Tags
    Code
Click For Summary

Discussion Overview

The discussion revolves around translating a Perl AI script for a twenty questions game into C++. Participants are exploring the necessary changes in syntax and logic required for the translation, as well as addressing specific programming concepts related to string handling and regular expressions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant shares their Perl script and requests help with translating it into C++, noting their limited knowledge of C++.
  • Another participant suggests resources for learning C++, including a tutorial and a book by Bruce Eckel.
  • Concerns are raised about the lack of coverage for regular expressions in the suggested C++ tutorial, with a focus on checking for vowels at the end of strings.
  • Some participants propose using C string functions like strstr and strchr instead of regular expressions for substring and character searches.
  • A participant attempts to provide a partial C++ translation of the Perl code, indicating that certain expressions in Perl are unclear to them.
  • Discussion includes a suggestion to simplify the vowel-checking logic in the translation.
  • One participant clarifies that the project is not for classwork but rather a personal project to share with a friend.

Areas of Agreement / Disagreement

Participants express differing views on the necessity and utility of regular expressions in C++ versus using simpler string functions. There is no consensus on the best approach to translating the specific Perl expressions.

Contextual Notes

Some participants express uncertainty about the meaning of specific Perl expressions, which may affect the translation process. The discussion highlights limitations in understanding the original script's logic and the implications for the C++ version.

Who May Find This Useful

Individuals interested in programming language translation, particularly from Perl to C++, as well as those looking to understand string manipulation techniques in both languages.

franznietzsche
Messages
1,523
Reaction score
6
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:

Code:
#!/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";
    }
}
 
Computer science news on Phys.org
Or at least direct me to a resource that would cover everything needed to write this in C++?
 
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.
 
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:

Code:
elsif (/a$/ or /e$/ or /i$/ or /o$/  or /u$/ or /a\?$/ or /e\?$/ or /i\?$/ or /o\?$/  or /u\?$/ ) {
would be better written:

Code:
elsif(/[aeiou]\??$/)
 
noelhustler said:
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:

Code:
elsif (/a$/ or /e$/ or /i$/ or /o$/  or /u$/ or /a\?$/ or /e\?$/ or /i\?$/ or /o\?$/  or /u\?$/ ) {
would be better written:

Code:
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 that's about all). HOw do you use those functions?
 
strstr
and
strchr

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:

Code:
#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;
    }
  }
}

franznietzsche said:
The code checks for a vowel at the end of whatever string is input into it.
You can do that with:
Code:
char c = question[strlen(question) - 1];

if (c == 'a' || c == 'e' || c == 'u' || c == 'o' || c == 'i')
  ...
 
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.
 

Similar threads

Replies
1
Views
2K
Replies
4
Views
3K
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
1
Views
4K
Replies
6
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 14 ·
Replies
14
Views
6K
  • · Replies 1 ·
Replies
1
Views
4K