Thread: Translator

  1. #1
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45

    Translator

    I seem to be having trouble with the file input output what I am I doing wrong with this prorgam?

    Code:
    #include "stdafx.h"
    #include <fstream.h>
    #include <iostream.h>
    #include <iomanip.h>
    #include <afx.h>
    #include <cstring>
    #include <stdlib.h>
    
     int x, y, z;
     char varnames [ ] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','x','y','z' };
    
    
    char varvalues[ 27 ];
    void takein(char var){
    
    	while(var != varnames[x]){
    
    x++;
    
    cin>>varvalues[x];}
    
    }
    
    void display (char var)
    
    {
    
    x = 0;
    
    
    while(var != varnames[x]){
    
    x++;
    
    cout<<varvalues[x];}
    
    }
    
    
    void addition(char v1, char v2, char v3)
    
    {
    
    x = 0;
    
    
    while (v1 != varnames[x]){
    
    x++;
    
    
    y = 0;}
    
    while (v2 != varnames[y]){
    
    y++;
    
    z = 0;}
    
    while (v3 != varnames[z]){
    
    z++;
    
    varvalues[z] = varvalues[x] + varvalues[y];
    
     
    }
     
    
    }
    
    
    void subtraction(char v1, char v2, char v3)
    
    {
    
    x = 0;
    
    
    while (v1 != varnames[x]){
    
    x++;
    
    
    y = 0;}
    
    while (v2 != varnames[y]){
    
    y++;
    
    z = 0;
    }
    while (v3 != varnames[z]){
    
    z++;
    
    varvalues[z] = varvalues[x] - varvalues[y];
    
    }
    
     
    
    }
    
    
    void multiply(char v1, char v2, char v3)
    
    {
    
    x = 0;
    
    
    while (v1 != varnames[x]){
    
    x++;
    
    
    y = 0;}
    
    while (v2 != varnames[y]){
    
    y++;
    
    z = 0;}
    
    while (v3 != varnames[z]){
    
    z++;
    
    varvalues[z] = varvalues[x] * varvalues[y];
    
     
    
    }
    
    }
    
    
    void divide(char v1, char v2, char v3)
    
    {
    
    x = 0;
    
    
    while (v1 != varnames[x]){
    
    x++;
    
    
    y = 0;}
    
    while (v2 != varnames[y]){
    
    y++;
    
    z = 0;}
    
    while (v3 != varnames[z])
    {
    z++;
    
    varvalues[z] = varvalues[x] / varvalues[y];}
    
    }
    
    
    
    void main()
    
    { 
    
    
    	ifstream file("a:\file.txt", ios::infile.txt");
    
    for (int i= 0; i < 40;++i)
    
    { 
    
    file.getline(file[i],';')
    
    }
    
    fin.close();
    
     
    
    
    
    
    while (varvalues[x] != " "){
    
    word += file[ x += 1 ]
    
    if(word == "cin")
    
    takein(file[ x += 1])
    
    
    if(word == "cout")
    
    display(file [ x += 1 ]
    
    
    if(word == "addition")
    
    addition(file [ x += 1 ], file[ x +=2 ], file [ x += 2 ])
    
    if(word == "multiply")
    
    multiply(file [ x += 1 ], file [ x += 2 ], file [ x +=2 ])
    
    
    if( word == "subtraction")
    
    subtraction( file[ x += 1 ], file [ x += 2 ], file [ x +=2 ])
    
    
    if( word == "divide")
    
    divide (file [ x += 1 ], file [ x += 2 ], file [ x +=2 ])} 
    
    }

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    What is the point in using code tags with no indentation in the code. We might as well just get rid of the tags.
    The keyboard is the standard device used to cause computer errors!

  3. #3
    Registered Luser
    Join Date
    Apr 2003
    Posts
    17
    He hasn't yet specified the exact 'problem' he faced.

  4. #4
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45
    The main problem is that when it gets to the code with trying to read in the file it gives me errors. It says things like infile is not part of ios.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Just use:
    Code:
    ifstream file("a:\file.txt");
    Edit: Where exactly are you trying to store your input from the file? You have a variable named file which is bad since it conflicts with the other file that is your ifstream object. You need a new array to hold that data.
    Last edited by hk_mp5kpdw; 05-09-2003 at 06:42 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45
    still doesn't like it. now it is yelling about the file.getline

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    getline takes three parameters, the destination buffer, a number representing how many bytes to extract from the input file, and a character representing the character to stop extracting at if we encounter that character before reading up to the number of bytes requested in the second parameter. It is used like this:
    Code:
    char buffer[10];
    file.getline(buffer,sizeof(buffer),';');
    Of course that only has a one-dimensional character array for holding a single line of input from the file, you will want a two-dimensional array to store all the lines from your input file.

    I also noticed that you have a different variable name for closing the file.
    Code:
    fin.close();
    Should be:
    Code:
    file.close();
    What's you current code look like?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45
    I changed it around a little. Now I have a rpoblem with making the while loop work. it says ++ needs an lvalue.

    Code:
     #include "stdafx.h"
    #include <fstream.h>
    #include <iostream.h>
    #include <iomanip.h>
    #include <afx.h>
    #include <cstring>
    #include <stdlib.h>
     int x, y, z;
     char nm [ ] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','x','y','z' };
    char vv[ 27 ];
    char buffer[27];
    void in(char rr){
     while(rr != nm[x]){
    	x++;
    	cin>>vv[x];}
    }
    void out (char rr)
    {
    	x = 0;
    while(rr != nm[x]){
    	x++;
    	cout<<vv[x];}
    }
    void add(char rr1, char rr2, char rr3)
    {
    	x = 0;
    while (rr1 != nm[x]){
    	x++;
    	y = 0;}
    while (rr2 != nm[y]){
    	y++;
    	z = 0;}
    while (rr3 != nm[z]){
    	z++;
    	vv[z] = vv[x] + vv[y];}
    }
    void sub(char rr1, char rr2, char rr3)
    {
      x = 0;
    while (rr1 != nm[x]){
      x++;
      y = 0;}
    while (rr2 != nm[y]){
      y++;
      z = 0;}
    while (rr3 != nm[z]){
      z++;
      vv[z] = vv[x] - vv[y];}}
    void mul(char rr1, char rr2, char rr3)
    {
      x = 0;
    while (rr1 != nm[x]){
      x++;
      y = 0;}
    while (rr2 != nm[y]){
      y++;
      z = 0;}
    while (rr3 != nm[z]){
      z++;
      vv[z] = vv[x] * vv[y];
    }
    }
    void div(char rr1, char rr2, char rr3)
    {
      x = 0;
    while (rr1 != nm[x]){
      x++;
      y = 0;}
    while (rr2 != nm[y]){
      y++;
      z = 0;}
    while (rr3 != nm[z]){
      z++;
      vv[z] = vv[x] / vv[y];}
    }
     
    void main(){ 
      ifstream tran("a:/TransProg.txt");
    for (int i=0; i < 40;++i)
    { 
      tran.getline(buffer,sizeof(buffer),';');
    }
    	tran.close();
    
    while (vv = ++buffer){
    
    	  switch ( vv ){
        case 'inn':
                in(buffer[x += 1]);
                break;
        case 'out':
                out(buffer [x += 1]);
                break;
        case 'add':
    		     
               add(buffer [x += 1], buffer[x +=2], buffer [x += 2]);
    		   break;
    	case 'sub':
    			sub(buffer[x += 1], buffer [x += 2], buffer [x +=2]);
                break;
        case 'mul':
    			mul(buffer [x += 1], buffer [x += 2], buffer [x +=2]);
    			break;
        case 'div':
    			div(buffer [x += 1], buffer [x += 2], buffer [x +=2]);
    			break;
        default:
    			cout<<"Not a command.";
    			break;}}
    
    
    
    }

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    One step forward two steps back...

    Of course that only has a one-dimensional character array for holding a single line of input from the file, you will want a two-dimensional array to store all the lines from your input file.
    Like I said, you will need a two-dimensional array to hold the data you are reading in from the input file. char buffer[27] just won't cut it here because in the loop where you are reading in from the file, you are just going to end up constantly overwriting the data in that array. Since you appear to be looping from 0 to 39 in this loop (40 elements total), I recommend you change buffer like so:
    Code:
    char buffer[40][27];
    And then in the loop change the getline part to this:
    Code:
    tran.getline(buffer[i],sizeof(buffer[i]),';');
    This will put each command that you are trying to store into a unique location from buffer[0] through buffer[39].

    Your while( vv == ++buffer ) loop is another problem, this was where you are getting your error that you mentioned. This should probably just be a for loop although a while loop could still be used in this case. Now, another problem is your whole switch/case bit of code. You can't have case statements checking for things like 'inn', 'out', 'add' etc... Single character or integral types only, I think you may have been trying to use "inn", "out", "add" since single quotes are for characters and not strings which use double quotes. Try going back to using an if or if/else if construct. In summary, that whole bit of code would probably be better like:
    Code:
    for( int j = 0; j < 40; ++j )
    {
        if( !strncmp(buffer[j],"inn",3) ) in( buffer[j][4] );
        else if( !strncmp(buffer[j],"out",3) ) out( buffer[j][4] );
        ...
        ...
    }
    You should be able complete the rest of that yourself.
    Last edited by hk_mp5kpdw; 05-09-2003 at 10:30 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. language translator (having problems in tokenizing)
    By jumbo2410 in forum C Programming
    Replies: 2
    Last Post: 03-23-2009, 05:29 AM
  2. Power Translator
    By siavoshkc in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 09-02-2006, 02:37 PM
  3. Very Inefficient translator.
    By XenoForce in forum C++ Programming
    Replies: 15
    Last Post: 10-24-2004, 02:58 PM
  4. IDEA: Programming language translator
    By ygfperson in forum Contests Board
    Replies: 1
    Last Post: 09-02-2002, 04:35 PM
  5. Morse Code Translator!
    By Paro in forum C++ Programming
    Replies: 4
    Last Post: 04-05-2002, 07:23 PM