i want to create a program that translates a sentance in english to another language

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #31
    So we should be something like this......

    [code=cpp]
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;

    int main () {
    string line;
    string english;
    int pos = string::npos; //initialise to invalid

    cout << "Type the english words here, then press enter. Make sure to copy the words correctly. If you need to spell check use www.google.com" ;
    cin >> english;

    ifstream myFile ("c:\\eAh.txt") ;
    if (myFile.is_open ())
    {
    while (! myFile.eof() )
    {
    getline (myFile,line);
    pos = line.find(engli sh);
    if(pos != string::pos)
    {
    cout <<"Word in line "<< line;
    if(pos == 0)
    {
    cout << " Word in first position on line ";
    }
    }
    }
    myFile.close();
    }

    else cout << "Unable to open file";

    }
    [/code]
    (I think this may be case sensitive, so apple != Apple

    Comment

    • jerger
      New Member
      • May 2007
      • 77

      #32
      cool, but how can i stop it once it works?

      i mean... if it finds fa, it should stop printing out words that have fa in it like fart (sorry im using this in my dictionary first thing that came to my head)... any way to pause it, end the if statement and then move on?


      i did this...
      myFile.close();

      this might actually help. is there a way that i could run the loop, based on the letter of the word to open the dictionary that starts with that word? (like the a dictionary), then close, go on to the next word? have mini dictionaries? or should i have one big dictionary and open/close it alot? its only going to have less then 5000-8000 words

      also... how can i get it to loop until all the words are translated? like 5 words in a sentance using this one dictionary (not multiple)... i'm going to try some ideas but i just like keeping a memo here for myself and your help

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

        #33
        You would modify the loop by adding an extra flag:
        [code=cpp]
        bool found = false;
        while ((! myFile.eof()) && (!found) )
        {
        getline (myFile,line);
        pos = line.find(engli sh);
        if(pos != string::pos)
        {
        cout <<"Word in line "<< line;
        if(pos == 0)
        {
        found = true; // This will break the loop
        cout << " Word in first position on line ";
        }
        }
        }
        [/code]

        Comment

        • jerger
          New Member
          • May 2007
          • 77

          #34
          i might have to load the input into an array and then, translated it between spaces? in a loop?

          my next goal is to translate two words in the dictionary, rather then one! woot

          my code works for multiple words, i got it to stop crashing. however, once it translates the first word, it needs to move onto the second. the only way i can think of doing this is to either put the string into an array, then find the next space or find some way to skip to the space each time it loops... so now to be lost again haahah


          current code, i had to comment stuff to stop errors, but stopped crashing for multiple words:


          #include <iostream>
          #include <fstream>
          #include <string>
          using namespace std;


          int main () {
          string line;
          string english;
          int pos = string::npos; //initialise to invalid


          cout << "Type the english words here, then press enter. Make sure to copy the words correctly. If you need to spell check use www.google.com" ;
          cin >> english;

          ifstream myFile ("c:\\eAh.txt") ;
          while (english != ".")
          if (myFile.is_open ())

          {

          while (! myFile.eof() )

          {

          getline (myFile,line);
          pos=line.find(e nglish);
          if(pos != string::npos)

          {

          cout <<"Trans"<< line;
          if(pos == 0)

          {

          cout<< endl;
          cout<<"sucks"<< endl;
          myFile.close();
          // cout << " Word in first position on line "<< endl;
          }
          }
          }

          myFile.close();

          //my crazy while for endl, trying to do multiple words
          //else cout << "Unable to open file";
          }
          cin>>english;
          }

          Comment

          • DeMan
            Top Contributor
            • Nov 2006
            • 1799

            #35
            Originally posted by jerger
            i might have to load the input into an array and then, translated it between spaces? in a loop?
            There are alternatives... ....
            If a sentence is entered as
            "icecream is cool"
            then the variable "english"
            will contain the whole sentence....
            if you use "english.fi nd(" ")" to search for spaces, then you can cut each word into a temporary variable, process it, and move on to the next word.

            You may like to use english.substri ng(int ffrom, int length)
            eg if you have
            [code=cpp]
            string s = "abcdefgh";
            s=s.substring(2 , 4);
            [/code]
            s now equals "cdef"

            Comment

            • jerger
              New Member
              • May 2007
              • 77

              #36
              with the array, how would it know where a new place starts or ends? maybe find the space, and then put those positions into the array?

              the first example looks easier, but how does the loop know to go to the next space?

              maybe the temp variable is a good idea, how does c++ keep track of temp variables?

              man i feel like your doing all the work and im doing all the panicking hahaha

              Comment

              • DeMan
                Top Contributor
                • Nov 2006
                • 1799

                #37
                consider this type of idea:
                [code=cpp]
                string english = "it was somwhere in the country in a land of rock and scrub";
                english = english + " "; //guarantees a space in the sentence
                int pos; string tempword;
                int pointer =0;
                bool finished = false;
                while(!finished )
                {
                pos = english.find(" ");
                tempword=englis h.substring(poi nter, pos);
                english = english.substri ng(pos+1, english.length( )); //remove word from sentence
                /* Process tempword here like we did english previously */
                if(english.leng th() == 0)
                {
                finished = true;
                }
                }
                [/code]

                You may like to add some checking that our substrings do not breach the bounds of the string......

                Comment

                • jerger
                  New Member
                  • May 2007
                  • 77

                  #38
                  string english, can we have it use cin input?

                  Comment

                  • DeMan
                    Top Contributor
                    • Nov 2006
                    • 1799

                    #39
                    I think then you need to use getline (and have a maximum length for english)

                    eg
                    [code=cpp]
                    const char SIZE = 128;
                    char english[SIZE]
                    cin.getline(eng lish, SIZE);
                    [/code]

                    Comment

                    • jerger
                      New Member
                      • May 2007
                      • 77

                      #40
                      the char idea i couldnt get to work, im so close on your previous post though with the substrings... only one error

                      "40 C:\hmong day 3 final.cpp 'struct std::string' has no member named 'substring' "


                      this is referring to:

                      english = english.substri ng(pos+1, english.length( )); //remove word from sentence

                      Comment

                      • DeMan
                        Top Contributor
                        • Nov 2006
                        • 1799

                        #41
                        sorry!!!! My fault! try "substr" instead

                        Comment

                        • jerger
                          New Member
                          • May 2007
                          • 77

                          #42
                          woot that solved it! I need to read closely lol, i forgot you created a tempword for me! then things made sense. i actually like jumped from my chair when it compiled... woot! thanks mate.

                          #include <iostream>
                          #include <fstream>
                          #include <string>
                          using namespace std;


                          int main () {
                          string line;
                          //string substring;

                          char SIZE = 128;

                          string english;


                          //int pos = string::npos; //initialise to invalid





                          cout << "Type the english words here, then press enter. Make sure to copy the words correctly. If you need to spell check use www.google.com" ;
                          cin>>english;
                          #
                          english = english + " "; //guarantees a space in the sentence
                          #
                          int pos; string tempword;
                          #
                          int pointer =0;
                          #
                          bool finished = false;
                          #
                          while(!finished )
                          #
                          {
                          #
                          pos = english.find(" ");
                          #
                          tempword=englis h.substr(pointe r, pos);
                          #
                          english = english.substr( pos+1, english.length( )); //remove word from sentence
                          #
                          /* Process tempword here like we did english previously */
                          ifstream myFile ("c:\\eAh.txt") ;
                          while (tempword != ".")
                          if (myFile.is_open ())


                          {

                          while (! myFile.eof() )

                          {

                          getline (myFile,line);

                          pos=line.find(t empword);
                          if(pos != string::npos)

                          {

                          cout <<"Trans"<< line;
                          if(pos == 0)

                          {

                          cout<< endl;
                          cout<<"sucks"<< endl;
                          // myFile.close();
                          cout << " Word in first position on line "<< endl;

                          #
                          if(english.leng th() == 0)
                          #
                          {
                          #
                          finished = true;
                          #
                          }
                          #
                          }


                          }
                          cin>>english;
                          }
                          }//extra
                          }//extra 2
                          }//extra 3
                          }//extra 4

                          Comment

                          • DeMan
                            Top Contributor
                            • Nov 2006
                            • 1799

                            #43
                            Glad to help, hope it actually works too!

                            incidently, if you highlight code when you write it and then hit the "#" symbol, it will wrap code tags around it!!!

                            Comment

                            • jerger
                              New Member
                              • May 2007
                              • 77

                              #44
                              cool i will work on cleaning it up eventually thanks for the tip.

                              what i need to do now, is get the internal loop to reset itself so that it starts at the beginning of the textfile again... that way... if

                              abba
                              apple

                              and someone types, apple abba

                              what is happening is it would stop and only print apple since abba came first, and its already past this point when it restarts the loop. if that makes sense.

                              if the user types, abba apple, then it prints both translations amazingly. so im trying to find some reset variables through trial and error

                              Comment

                              • DeMan
                                Top Contributor
                                • Nov 2006
                                • 1799

                                #45
                                We're a little outside of my knowledge here, but......
                                when you are at the point where you have found a word and are moving onto the next one, try

                                [code=cpp]
                                myFile.close();
                                myFile.open("c: \\eAh.txt");
                                [/code]

                                This should close your file and reopen it from the beginning

                                Comment

                                Working...