Translator continued

This is a discussion on Translator continued within the C++ Programming forums, part of the General Programming Boards category; I did the 2d array and i can get the stuff in,but it wont add, sub, div, or mlt right ...

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

    Translator continued

    I did the 2d array and i can get the stuff in,but it wont add, sub, div, or mlt right and the output comes out all wrong.

    Code:
    #include "stdafx.h"
    #include <fstream.h>
    #include <iostream.h>
    #include <iomanip.h>
    #include <afx.h>
    #include <cstring>
    #include <stdlib.h>
    void in(char rr);
    void out(char rr);
    void add(char rr1, char rr2, char rr3);
    void sub(char rr1, char rr2, char rr3);
    void mlt(char rr1, char rr2, char rr3);
    void div(char rr1, char rr2, char rr3);
     char vv [ ] = { '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 buffer[36]={0};
    
    
     
    void main(){ 
        char tt[20][10];
    	int i =0, x=0, y=0, cont=1;
    	ifstream fin("a:/TransProg.txt");
        char c[2];
    	while(fin.read(c,1)){
    		if(c[0] == 10){
    			i++;
    			y=0;}
    		else{
    			tt[i][y] = c[0];
    			y++;}}
    	y=0; 
    
    CString blah ="";
    while (cont==1){
    	x=0;
    	while(x<i){
    		while(tt[x][y]!=' ')
    			blah+=tt[x][y++];
    		y++;
    
    		if(blah== "inn"){
                in(tt[x][y]);}
    		else if(blah=="out"){
                out(tt[x][y]);
    		}
    		else if(blah="add"){   
               add(tt[x][y], tt[x][y+2], tt[x][y+4]);
    		}
    		else if(blah=="sub"){
    			sub(tt[x][y], tt[x][y+2], tt[x][y+4]);
    		}
    		else if(blah=="mlt"){
    			mlt(tt[x][y], tt[x][y+2], tt[x][y+4]);
    		}
    		else if(blah=="div"){
    			div(tt[x][y], tt[x][y+2], tt[x][y+4]);
    		}
        
    	 x++;
    	y=0;
    	blah="";}
    
    cout<<"Do you wish to do it again?(Press 1 do another)";
    cin>>cont;
    cin.get();
    cout<<endl;}
    
    
    }
    
    void in(char rr){
    	int x=0;
     while(rr != vv[x]&&x<36){
    	x++;
     cin>>buffer[x];}
    }
    void out(char rr)
    {
    	int x = 0;
    while(rr != vv[x]&&x<36){
    	x++; 
    	
    	cout<<vv[x]<< "'s value is "<<buffer[x]<<endl;}
    }
    void add(char rr1, char rr2, char rr3)
    {
    	int x = 0;
    while (rr1 != vv[x]&&x<36){
    	 
    	x++;}int y = 0;
    while (rr2 != vv[y]&&x<36){
    	
    	y++;}int z = 0;
    while (rr3 != vv[z]&&x<36){
    	z++;
    	buffer[z] = buffer[x] + buffer[y];
    	}
    
    
    }
    void sub(char rr1, char rr2, char rr3)
    {
     int x = 0;
    while (rr1 != vv[x]&&x<36){
      
      x++;}int y = 0;
    while (rr2 != vv[y]&&x<36){
      
      y++;}int z = 0;
    while (rr3 != vv[z]&&x<36){
      z++;buffer[z] = buffer[x] - buffer[y];
      }}
    void mlt(char rr1, char rr2, char rr3)
    {
      int x = 0;
    while (rr1 != vv[x]&&x<36){
      
      x++;}int y = 0;
    while (rr2 != vv[y]&&x<36){
      
      y++;}int z = 0;
    while (rr3 != vv[z]&&x<36){
      z++;
      buffer[z] = buffer[x] * buffer[y];
      
    }
    }
    void div(char rr1, char rr2, char rr3){
      int x = 0;
    while (rr1 != vv[x]&&x<36){
      
      x++;}int y = 0;
    while (rr2 != vv[y]&&x<36){
      
      y++;}int z = 0;
    while (rr3 != vv[z]&&x<36){
    z++;
      buffer[z] = buffer[x] / buffer[y];
      }
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I'm not exactly sure how the inn and out functions should work. Are they supposed to only load one register or several at a time?


    Anyway, try this (old code has been commented).
    Code:
     //char buffer[36]={0};
     int buffer[36]={0};
    
    //void main(){
    //void main() is wrong.  See FAQ for reasons.
    int main(){
    .
    .
    .
    void in(char rr){
    	int x=0;
     while(rr != vv[x]&&x<36){
    	x++;
     //cin>>buffer[x];}
     }
     cin>>buffer[x];
    }
    
    void out(char rr)
    {
    	int x = 0;
    while(rr != vv[x]&&x<36){
    	x++; 
    	
    	//cout<<vv[x]<< "'s value is "<< buffer[x]<<endl;}
            }
    	cout<<vv[x]<< "'s value is "<< buffer[x]<<endl;
    }

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21