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

  • Thread starter franznietzsche
  • Start date
  • Tags
    Code
In summary, the person wrote an AI script for 20 questions in perl, but they need it translated into C++. They contacted someone for help and they provided a summary of the content.
  • #1
franznietzsche
1,504
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
  • #2
Or at least direct me to a resource that would cover everything needed to write this in C++?
 
  • #4
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.
 
  • #5
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]\??$/)
 
  • #6
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?
 
  • #7
strstr
and
strchr

You don't need to use c++ to write a program like that. You can stick with plain c.
 
  • #8
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')
  ...
 
  • #9
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.
 

1. What is code translation?

Code translation is the process of converting computer programming code written in one language into another language. This is often done in cases where a program needs to be translated into multiple languages to reach a wider audience. It is also commonly used when a programmer needs to switch between languages for a project or when working with code written by someone else.

2. Why is code translation necessary?

Code translation is necessary for various reasons. It allows a program to be used by people who speak different languages, making it more accessible. It also allows programmers to work with code written in different languages and collaborate more effectively. Additionally, code translation is useful when a company expands its market and needs to reach a global audience.

3. How is code translation done?

Code translation can be done manually or with the help of automated tools. Manual translation involves a programmer reading and understanding the code in one language and then writing it in another language. Automated tools, on the other hand, use translation algorithms to convert the code from one language to another. Some tools also offer the option to customize the translation for better accuracy.

4. What challenges are involved in code translation?

Code translation can be challenging, especially when done manually. It requires a deep understanding of both languages and can be time-consuming. Automated tools also have their limitations and may not always produce accurate translations. Additionally, some languages have different syntax and structures, making translation more complicated.

5. Are there any best practices for code translation?

Yes, there are some best practices that can help ensure a successful code translation. These include using comments and documentation in the original code, keeping the original code well-organized, and avoiding language-specific terms and idioms. It is also essential to thoroughly test the translated code to check for any errors or bugs that may have arisen during the translation process.

Similar threads

  • Programming and Computer Science
Replies
1
Views
951
  • Advanced Physics Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Programming and Computer Science
Replies
2
Views
5K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top