help!!

This is a discussion on help!! within the C++ Programming forums, part of the General Programming Boards category; What's wrong with this thing? Code: #include <iostream.h> #include <afx.h> void In(char var) { int x = 0; while(var != ...

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

    Exclamation help!!

    What's wrong with this thing?
    Code:
    #include <iostream.h>
    #include <afx.h>
    
    void In(char var)
     {
     int x = 0;
     while(var != letters[x] && x < 26)
      x++;
     if(x < 26)
      cin>>lines[x];
     }
     void Out(char var)
     {
       int x = 0;
      
      while(var != letters[x] && x < 26)
       x++;
      if(x < 26)
       cout<<lines[x] << endl;
     }
     
     void Add(char v1, char v2, char v3)
     {
      int x = 0;
      int y = 0;
      int z = 0;
       
       while (v1 != letters[x]&& x < 26)
        x++;
       while (v2 != letters[y]&& x < 26)
        y++;
       while (v3 != letters[z]&& x < 26)
        z++;
      if( x< 26 && y < 26 && z < 26)
       lines[z] = lines[x] + lines[y];
     }
     
     void Subtract(char v1, char v2, char v3)
     {
      int x = 0;
      int y = 0;
      int z = 0;
       while (v1 != letters[x]&& x < 26)
        x++;
       while (v2 != letters[y]&& x < 26)
        y++;
       while (v3 != letters[z]&& x < 26)
        z++;
      if( x< 26 && y < 26 && z < 26)
       lines[z] = lines[x] - lines[y];
     }
     
     void Mult(char v1, char v2, char v3)
     {
      int x = 0;
      int y = 0;
      int z = 0;
       while (v1 != letters[x]&& x < 26)
        x++;
       while (v2 != letters[y]&& x < 26)
        y++;
       while (v3 != letters[z]&& x < 26)
        z++;
      if( x< 26 && y < 26 && z < 26)
       lines[z] = lines[x] * lines[y];
     
     }
     
     void Div(char v1, char v2, char v3)
     {
      int x = 0;
      int y = 0;
      int z = 0;
       while (v1 != letters[x]&& x < 26)
        x++;
    
       while (v2 != letters[y]&& x < 26)
        y++;
       while (v3 != letters[z]&& x < 26)
        z++;
      if( x< 26 && y < 26 && z < 26)
       lines[z] = lines[x] / lines[y];
     }
     
    
    
    char letters [ ] = { 'a','b','c','d','e','f','g','h','i',
           'j','k','l','m','n','o','p','q','r',
           's','t','u','v','w','x','y','z'};
    int lines[ 52 ]={0};
    int main()
    {
     char info[20][10]; 
     int i =0, dx = 1;
     
     cout <<"Enter x exit"<<endl<<endl;
     while(i < 20 && info[i-1][0] != 'x' || info[i-1][0]!='X')
     { cin.getline(info[i++],10);
     }
     int x;
     int y = 0;
     int loop = 'y';
     CString space = "";
     while(loop == 'y'||loop=='Y')
     { x = 0;
     
      while (x < i){
     
      while(info[x][y] != ' ')
      {  space += info[x][y++];
        y++;
       if(space == "get")
       { In(info[x][y]);
       }  
       if(space == "put")
       { Out(info[x][y]);
       }
       if(space == "add")
       { Add(info[x][y], info[x][y+2], info[x][y+4]);
       }
       if(space == "mul")
       { Mult(info[x][y], info[x][y+2], info[x][y+4]);
       }
       if(space == "sub")
       { Subtract(info[x][y], info[x][y+2], info[x][y+4]);
       }
       if(space == "div")
       { Div(info[x][y], info[x][y+2], info[x][y+4]);
       }
       
       x++;
       y=0;
       space = "";
      }
      cout << "Do you wish to loop? (y/n)";
      cin.get();
      cin >> loop;
      cout << endl;
     }
    }
    }
    Code Tags added by Kermi3

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109

  3. #3
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45
    sorry. I forgot the tags and the message it gives meis that its ends unexpectedly, but everything seems perfect, but yet it is still yelling at me.

  4. #4
    cgoat
    Guest
    1) What are you trying to do?

    2) What does the program actually do, as you have it written?

    3) If it gets to the input part, what did you input?

    Posting a mess of code and saying "What's wrong" isn't good enough. Especially when that code has no comments and no clear intent.

  5. #5
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45
    Well basically it is a simply translator that adds, subtracts, divides, & Multiplies. You write it in the code like
    add a,b
    and its read it in and coverts it into C++ does the work and spits out the answer.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Why do you use the MFC CString class instead of the STL string?

    And if this is (as I guess) MSVC++6, use the new-style headers:
    #include <iostream>
    #include <string>

    The letters array must be declared at the top of the file, so does the lines array.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    I moved the arrays to the top, (this has been mentioned already), and then it compiled fine. I didn't try to run it. Tell me how to produce your error.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Your code hurts my eyes. I think I understand what you are trying to do, but have you ever considered using map and vector containers before?
    Code:
    #include <string>
    #include <iostream>
    #include <map>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    map<char,int> myMap;
    
    void In( char cVal )
    {
        if( !isalpha(cVal) ) return;
        cout << "Getting " << cVal << " >> ";
        cin >> myMap[cVal];
    }
    
    void Out( char cVal )
    {
        if( !isalpha(cVal) ) return;
        cout << "Outputting " << cVal << " >> " << myMap[cVal] << endl;
    }
    
    int main()
    {
        string input;
        vector<string> strVect;
        vector<string>::iterator itVect;
    
        // Initialize map container to store values at locations a->z
    
        for( int i = 0; i < 26; ++i )
            myMap['a'+i] = 0;
    
        // Get up to 10 lines of data from user stop at 10 or first 'x'
        // Automatically transform any input into lower case
    
        int j = 0;
        do
        {
            getline(cin,input);
            transform(input.begin(),input.end(),input.begin(),tolower);
            strVect.push_back(input);
        } while( (j++ < 10) && input.at(0) != 'x' );
    
        // Process users 'get', 'put', 'add', etc... requests.
    
        for( itVect = strVect.begin(); itVect != strVect.end(); )
        {
            if( itVect->find("get") == 0 ) In( itVect->at(4) );
            if( itVect->find("put") == 0 ) Out( itVect->at(4) );
            if( ++itVect != strVect.end() && itVect->at(0) != 'x' )
            {
                char choice;
                cout << "Do you wish to continue (y or n): ";
                cin >> choice;
                if( tolower(choice) != 'y' ) break;
            }
        }
    
        return 0;
    }
    That's all the code needed for just the "put" and "get" functions to be functional, you can figure out the others for yourself. Barring any typos, and providing 15 and 20 for the user inputs to the two "gets", the above code should output something like:

    put a
    get r
    get c
    put r
    x
    Outputting a >> 0
    Do you wish to continue (y or n): y
    Getting r >> 15
    Do you wish to continue (y or n): y
    Getting c >> 20
    Do you wish to continue (y or n): y
    Outputting r >> 15
    Last edited by hk_mp5kpdw; 05-08-2003 at 01:43 PM.
    "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

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