Thread: Help altering code to work with char array

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    54

    Help altering code to work with char array

    I'm writing code for an assignment for infix to postfix conversion using stack class and queue class. My conversion function I think needs to accept a character array, but I have set up in main to get a line from the file that is the infix to be converted. It is currently getting this line and treating it like a string, and I think if I switch the code to retrieving the infix as a character array instead of a string, things will move along. I'm not quite sure what to do though :-(

    If I declare Next as string in main, and set the function to accept infix as string, the program compiles and crashes with a Debug Assertion Error, string subscript out of range.

    If I set it up how I have in this post, I have errors anywhere I use infix[i] saying subscript requires array or pointer type.

    I tried declaring Next as char Next[size]; and then infix in the function as char infix[size] which gave me the error regarding using getline.

    Halp! ::banghead::

    infix to postfix :
    Code:
    void infix_to_postfix(char infix)
    {
           Stack Stack;
    	   Queue Queue;
    
    	   for (int i=0;i<size;i++) {
    		   if (infix[i] != '+' || infix[i] != '-' || infix[i] != '*' || infix[i] != '/' || infix[i] != '^') {
    			   Queue.Enqueue(infix[i]); //if infix[i] is number, place in queue
    		   }//if
    		   else if (infix[i] == '(') {
    			   Stack.Push(infix[i]);
    		   }//else if
    		   else if (infix[i] == ')') {
    			   while (Stack.Top() != '(') {
    				   Queue.Enqueue(Stack.Top());
    				   Stack.Pop();
    			   }//while
    			   Stack.Pop(); //remove ')' from stack
    		   }//else if
    		   else if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '^') {
    			   while (!Stack.Empty() && order_ops(Stack.Top()) <= order_ops(infix[i])){
    				   Queue.Enqueue(Stack.Top());
    				   Stack.Pop();
    			   }//while
    			   Stack.Push(infix[i]);
    		   }//else if
    	   }//for
    }//end infix_to_postfix
    main:
    Code:
    int main(int argc, char *argv[]) {
    	char Next;
    	ifstream infile("A2.txt");
    
    	while (infile.good()) {
    		getline(infile,Next);
    		if (infile.good())
    			cout << "Infix : \n" << Next << endl;
    		infix_to_postfix(Next);
    	}//while
    
    	infile.close();
    
    	system("PAUSE");
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    char Next;
    That there's a datatype that will hold one, single character.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    Quote Originally Posted by rags_to_riches View Post
    Code:
    char Next;
    That there's a datatype that will hold one, single character.
    Right. So I thought about using char Next[size] and in the infix to postfix function setting the input as char infix[size] so it would hold the length that I need. But I run into an error with using getline: error C2784 ... could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ifstream'

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    Would I be able to use strcpy to convert Next to char array like this?
    Code:
    string Next;
    char Next2[size];
    ifstream infile("A2.txt");
    
    while (infile.good()) {
    	getline(infile,Next);
    	strcpy(Next2, Next.c_str());
    	if (infile.good())
    		cout << "Infix : \n" << Next2 << endl;
    	infix_to_postfix(Next2);
    }//while
    Would I then be able to pass Next2 as char to the infix_to_postfix function as the infix?

    Code:
    void infix_to_postfix(char infix[size])

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. C++ text file
    By statquos in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2008, 01:42 PM
  3. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  4. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  5. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM