lundi 7 décembre 2015

Translating word to ubbi (I finished some of it and need to do more)

I am asking for help on my code here is the objective : Translate into Ubbi translator If the user chooses the option 1, s/he is asked to input a word and then the word is translated to Ubbi dubbi and displayed.

If the user chooses the option 2, s/he is asked to input a name of a file and the words from the file are read into an array of strings, one word per string. You can assume that there will be at most 100 words in the file.

The option 3 will translate each word from the array of strings into Ubbi dubbi. The array of strings will then contain the translated words from the file.

The option 4, will write the content of the array of words to a file. The name of the file should be input from the user. Note that you might not have the same white space in your translated file as in the original file, which is OK. Also, it is not your responsibility to check if the user translated the words before writing them in the file.

[code]

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <limits>
#include <cctype>
#include <algorithm>

using namespace std;

const int max_size_words=100;
bool replay_check(int);

string read_file(string &fileName);

int selected_option(){
    int choice;
    cout <<"            "<< endl<<endl;
    cout <<"            "<<  " __________________________ " <<endl;
    cout <<"            "<<  "|   0 - Quit the program   |" << endl;
    cout <<"            "<<  "|   1 - Encode word        |" << endl;
    cout <<"            "<<  "|   2 - Read file          |" << endl;
    cout <<"            "<<  "|   3 - Encode file        |" << endl;
    cout <<"            "<<  "|   4 - Write to a file    |" << endl;
    cout <<"            "<<  "|                          |" << endl;
    cout <<"            "<<  "| Please select the number |" << endl;
    cout <<"            "<<  "|           (0-4)          |" << endl;
    cout <<"            "<<  "|__________________________|" << endl << endl;

    cin>>choice;

    return choice;

}
bool is_one_of( char c, std::string char_set ) { return char_set.find(c) != std::string::npos ; }

string add_prefix( std::string str, std::string char_set, std::string prefix )
{
    std::string result ;

    for( std::size_t i = 0 ; i < str.size() ; ++i ) // for each character in the string
    {
        char this_char = str[i] ;

        if( is_one_of( this_char, char_set ) ) /* if it is a vowel */ result += prefix ; // add prefix
        result += this_char ; // append the character
    }

    return result ;
}

void greeting_mssg(){

    cout <<"        "<<  "******************************************" << endl;
    cout <<"        "<<  "*                                        *" << endl;
    cout <<"        "<<  "*  Welcome to the Ubi Dubbi translator!  *" << endl;
    cout <<"        "<<  "*                                        *" << endl;
    cout <<"        "<<  "******************************************" << endl;
}

string enter_file_name();
string encode_file();
int main(){

int choice;

ofstream outData;
const string vowels = "aeiouAEIOU" ;
const string prefix = "ub" ;

greeting_mssg();
  do
    {
        int i=0;
        choice = selected_option();
    switch(choice)
    {

  case 0:
            cout << "********"<< "Thank you for using Ubi Dubbi translator.Goodbye!"<<"**********"<<endl;

            break;


   case 1:
        {

        string word;

        system("cls");
                cout << "Please enter a word for Ubi Dubbi translating :"<< endl;
                cin >>word;
                cout<<"your translated word is:"<<add_prefix( word, vowels, prefix );
                break;
        }
   case 2:
{       enter_file_name();
}
    case 3:
{
     ifstream inData;
    string fileName;
    int i=0;
        system("cls");
        read_file(fileName);
        inData.open(fileName.c_str());// opens the file with the users input
        //outData.open(fileName.c_str());



        while(inData.fail())
    {
            inData.clear();
            cout<<"Incorrect filename, please enter again";
            cin>>fileName;

            inData.open(fileName.c_str());
    }
        while (!inData.eof() && !inData.fail() && i<100)
    {
            string filewords[100];
            for (int i=0;i<100;i++)
            {
                inData>>filewords[i];
            }
    }
                inData>>filewords[i];

        for (int j=0;j<i;j++)
        {
          filewords[j] =add_prefix(filewords,vowels,prefix)
        }
        inData.close();
        cin.clear();
        cin.ignore();


}



    }

    }


    }



 while(choice!=0);

 return 0;

}

string read_file(string& fileName) //option2
{
        //ofstream outData;
        cin.clear();
        cout << "Enter the data file Name : " << endl;//asks user to input filename
        cin >> fileName; //inputs user input into fileName
        fileName = fileName+".txt";
        cout << "reading file now : "<< fileName <<endl;


        return fileName;
}




 string add_prefix1( std::string str[], std::string char_set, std::string prefix )
{
    std::string result ;

    for( std::size_t i = 0 ; i < str[].size() ; ++i ) // for each character in the string
    {
        char this_char = str[i] ;

        if( is_one_of( this_char, char_set ) ) /* if it is a vowel */ result += prefix ; // add prefix
        result += this_char ; // append the character
    }

    return result ;
}

[/code]

I don't know how to do number 3 to encode the file and translate into ubbi. Although I did on case 3 but it seems like doesn't work.

please help it is tomorrow . Please Please

Aucun commentaire:

Enregistrer un commentaire