Converting C to C++

Discussion in 'OT Technology' started by Kieffer87, Dec 8, 2006.

  1. Kieffer87

    Kieffer87 Orly OT Supporter

    Joined:
    Sep 23, 2005
    Messages:
    1,491
    Likes Received:
    0
    Location:
    Where the Green Grass Grows
    I have 82 lines of code that I need to convert to C++ from C by the end of the day. About all I know is how to change printf and scanf to cin, cout. haha Any help would be appreciated, I'd be willing to send $20 via paypal if someone wants to do it for me :x:
     
  2. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    paste it up
     
  3. Kieffer87

    Kieffer87 Orly OT Supporter

    Joined:
    Sep 23, 2005
    Messages:
    1,491
    Likes Received:
    0
    Location:
    Where the Green Grass Grows
    This is the original code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    #define INMAX 1024
    
    char pop(void);
    void push(char);
    
    struct A {
            char c;
            struct A *next;
        };
    
    struct A* start=NULL;
    
    int stx=0;
    
    int main() {
    
    char input[INMAX],*p1,*p2,*p3,op,word[64];
    
    int i,j,k,m,n;
    
    while (fgets(input,INMAX,stdin)!=NULL) {
    
          fputs(input,stdout);
          input[strlen(input)-1]='\0';
    
          p1=input;
    
          while (1) {
                if (*p1=='\0') { printf("err 1\n"); return EXIT_FAILURE; }
                if (*p1=='(') { p1++; continue; }
                if (*p1==' ') { p1++; continue; }
                if (*p1==')') {
                      op=pop();
                      if (op=='?')
                            printf("err 2\n");
                            return EXIT_FAILURE; 
                }
                      printf(" %c ",op);
                      p1++;
                      continue;
                      }
    
     if (*p1=='+' || *p1=='-' || *p1=='*' || *p1=='/') {
                      push(*p1);
                      p1++;
                      continue;
                      }
                if (isdigit(*p1)) {
                      i=0;
                      word[i++]=*p1;
                      p1++;
                      while (isdigit(*p1)) { word[i++]=*p1; p1++; }
                      word[i]='\0';
                      printf(" %s ",word);
                      continue;
                      }
                if (*p1=='$' && pop()=='?') { printf(" $\n\n"); break; }
                printf("err 3\n"); return EXIT_FAILURE;
                }
          }
    }
    
    char pop() {
            char(x);
            struct A *p2;
            if(start==0) return '?';
            stx--;
            x=start->c;
            p2=start;
            start=start->next;
            free(p2);
            return x;
          }
    
    void push(char c) {
            struct A* newPtr ;
            newPtr = malloc( sizeof( struct A));
            if (newPtr != NULL) {
            newPtr->c = c;
            newPtr->next = start;
            start = newPtr; }
          }
     
    Last edited: Dec 8, 2006
  4. Kieffer87

    Kieffer87 Orly OT Supporter

    Joined:
    Sep 23, 2005
    Messages:
    1,491
    Likes Received:
    0
    Location:
    Where the Green Grass Grows
    Here is the code from what I have done so far, i'd like to think im close:noes I think I got new and delete right but then again who knows. What do you guys think?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    #define INMAX 1024
    
    struct A {
            char c;
            struct A *next;};
    
    Class stack{
        Public:
            
            stack();
            ~stack();
            int stx=0;
            char pop(void);
            void push(char);
    
        Private:
            
            struct A* start=NULL;
        }
    
    
    
    int main() {
    
    char input[INMAX],*p1,*p2,*p3,op,word[64];
    
    int i,j,k,m,n;
    
    while (fgets(input,INMAX,stdin)!=NULL) {
          fputs(input,stdout);
          input[strlen(input)-1]='\0';
          p1=input; 
          while (1) { 
                if (*p1=='\0') { cout<<("err 1\n"); return EXIT_FAILURE; }
                if (*p1=='(') { p1++; continue; }
                if (*p1==' ') { p1++; continue; }
                if (*p1==')') {
                      op=pop();
                      if (op=='?') { 
                            cout<<("err 2\n");
                            return EXIT_FAILURE; }
                      cout<<(" %c ",op);
                      p1++;
                      continue;
                      }
    
    if (*p1=='+' || *p1=='-' || *p1=='*' || *p1=='/') {
                      push(*p1);
                      p1++;
                      continue;
                      }
                if (isdigit(*p1)) {
                      i=0;
                      word[i++]=*p1;
                      p1++;
                      while (isdigit(*p1)) { word[i++]=*p1; p1++; }
                      word[i]='\0';
                      cout<<(" %s ",word);
                      continue;
                      }
    
                if (*p1=='$' && pop()=='?') { cout<<(" $\n\n"); break; }
                cout<<("err 3\n"); return EXIT_FAILURE;
                }
          }
    }
    
    char pop() {
            char(x);
            struct A *p2;
            if(start==0) return '?';
            stx--;
            x=start->c;
            p2=start;
            start=start->next;
    delete p2;
            return x;
            }
    
    void push(char c) {
            struct A* newPtr ;
            newPtr = new struct A;
            if (newPtr != NULL) {
                newPtr->c = c;
                newPtr->next = start;
                start = newPtr; 
            }
    
            }
     
    Last edited: Dec 8, 2006
  5. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    use the code tags instead of quote and repaste real quick
     
  6. Kieffer87

    Kieffer87 Orly OT Supporter

    Joined:
    Sep 23, 2005
    Messages:
    1,491
    Likes Received:
    0
    Location:
    Where the Green Grass Grows
    I just tryed to compile but I get a bunch of not declared in this scope errors. If I remember right I need to assign stuff to the class don't I?
     
  7. Kieffer87

    Kieffer87 Orly OT Supporter

    Joined:
    Sep 23, 2005
    Messages:
    1,491
    Likes Received:
    0
    Location:
    Where the Green Grass Grows
  8. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    are you using visual studio? right after the #define up top put in "using namespace std;"
     
  9. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    this indenting is horrible dude lol
     
  10. Kieffer87

    Kieffer87 Orly OT Supporter

    Joined:
    Sep 23, 2005
    Messages:
    1,491
    Likes Received:
    0
    Location:
    Where the Green Grass Grows
    Yea sorry about the indenting, I was remoting into a linux comp to do the editing and it was slow as shit last night so I did everything in notepad and somewhere along the line the indentation got all screwed up.
     
  11. Kieffer87

    Kieffer87 Orly OT Supporter

    Joined:
    Sep 23, 2005
    Messages:
    1,491
    Likes Received:
    0
    Location:
    Where the Green Grass Grows
    I'm using notepad/nano when on linux and g++ for the compiler.
     
  12. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    classes dont work like structs. in your main or globally you need to declare something like "stack mystack;" which makes an instance of mystack. conventionally class start with upper case.
     
  13. Kieffer87

    Kieffer87 Orly OT Supporter

    Joined:
    Sep 23, 2005
    Messages:
    1,491
    Likes Received:
    0
    Location:
    Where the Green Grass Grows
    So I put in #define INMAX 1024 using namespace std;
    correct?

    Now I got a bunch of different errors about expected before ) and ; token. haha
     
  14. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    and can you give me a quick summary of what youre trying to do? you can probably do this alot easier using string.h or vector.h
     
  15. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    Code:
    #define INMAX 1024
    using namespace std;
    
    you need the namespace for using cout and cin.
     
  16. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    the stdlib also has linked list libraries too
     
  17. Kieffer87

    Kieffer87 Orly OT Supporter

    Joined:
    Sep 23, 2005
    Messages:
    1,491
    Likes Received:
    0
    Location:
    Where the Green Grass Grows
    Convert the previous assignment to C++. Change the I/O to cin/cout format and create a class called Stack which has public functions push() and pop(). The Constructor will initialize the stack top pointer (private data) to NULL. The Destructor will free any items remaining on the stack. Use 'new' and 'delete' operators instead of malloc() and free().


    This was the assignment. Our teacher is kind of a dick and just plopped this on us yesterday and pretty much said figure it out using your book and oh its due Firday. Like I said in teh first post I really have no idea about anything in C++ so im kinda in the dark here.
     
  18. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    i guess i meant what was the original assignment
     
  19. Kieffer87

    Kieffer87 Orly OT Supporter

    Joined:
    Sep 23, 2005
    Messages:
    1,491
    Likes Received:
    0
    Location:
    Where the Green Grass Grows
    Ok I added
    std::cout
    std::cin

    And that fixed alot of the problems

    now I am just gettins some "pop, push, stx, start, are not declared in this scope." I also added <iostream>
     
  20. Kieffer87

    Kieffer87 Orly OT Supporter

    Joined:
    Sep 23, 2005
    Messages:
    1,491
    Likes Received:
    0
    Location:
    Where the Green Grass Grows
    The original assignment was to take the original code I posted and convert it to C++. He just gave us the code, so we havn't done anything with it.
     
  21. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <iostream>
    #define INMAX 1024
    
    
    using namespace std;
    
    struct A {
            char c;
            struct A *next;
    };
    
    class Stack{
        public:
            Stack();
            ~Stack();
            int stx=0;
            char pop(void);
            void push(char);
    
        private:
           struct A* start;
    }
    
    Stack::Stack()
    {
        start = 0;
    }
    
    
    char Stack::pop() {
            char x;
            A *p2 = new A;
            if(start==0) 
                return '?';
            stx--;
            x=start->c;
            p2=start;
            start=start->next;
            delete p2;
            return x;
    }
    
    void Stack::push(char c)
    {
            A* newPtr = new A;
            if (newPtr) {
                newPtr->c = c;
                newPtr->next = start;
                start = newPtr; 
            }
            stx++;
    }
    
    
    int main() {
    
    Stack myStack;
    
    char input[INMAX],*p1,*p2,*p3,op,word[64];
    
    int i,j,k,m,n;
    
    while (fgets(input,INMAX,stdin)!=NULL)
    {
          fputs(input,stdout);
          input[strlen(input)-1]='\0';
          p1=input; 
          while (1)
          { 
                if (*p1=='\0') { cout<<("err 1\n"); return EXIT_FAILURE; }
                if (*p1=='(') { p1++; continue; }
                if (*p1==' ') { p1++; continue; }
                if (*p1==')')
                {
                      op=pop();
                      if (op=='?')
                      { 
                            cout<<("err 2\n");
                            return EXIT_FAILURE;
                      }
                      cout<<(" %c ",op);
                      p1++;
                      continue;
                 }
    
                if (*p1=='+' || *p1=='-' || *p1=='*' || *p1=='/')
                {    
                    push(*p1);
                    p1++;
                    continue;
                }
                if (isdigit(*p1))
                {
                    i=0;
                    word[i++]=*p1;
                    p1++;
                    while (isdigit(*p1)) { word[i++]=*p1; p1++; }
                    word[i]='\0';
                    cout<<(" %s ",word);
                    continue;
                }
                if (*p1=='$' && pop()=='?') { cout<<(" $\n\n"); break; }
                cout<<("err 3\n");
                return EXIT_FAILURE;
          }
    }
    
    heres a quick update. you still have to make the destructor which will go through the list and delete all the nodes

    http://richardbowles.tripod.com/cpp/linklist/linklist.htm

    that link should help too, I didn't really do anything to the main yet but following the steps on that link should help alot.
     
    Last edited: Dec 8, 2006
  22. Kieffer87

    Kieffer87 Orly OT Supporter

    Joined:
    Sep 23, 2005
    Messages:
    1,491
    Likes Received:
    0
    Location:
    Where the Green Grass Grows
    I'll give that a try thanks for your help.
     

Share This Page