Why do I get these errors in the If statements?

This is a discussion on Why do I get these errors in the If statements? within the C++ Programming forums, part of the General Programming Boards category; Code: #include<iostream> #include<fstream> #include<iomanip> #include<cstdlib> #include<string> #include"StackLLImple.cpp" using namespace std; int main() { char answer; //char InputChar1, InputChar2; int num; ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    Why do I get these errors in the If statements?

    Code:
    #include<iostream>
    #include<fstream>
    #include<iomanip> 
    #include<cstdlib> 
    #include<string>
    #include"StackLLImple.cpp"
    
    using namespace std;
    
    int main()
    {
        char answer;
    	//char InputChar1, InputChar2;
        int num;
        int success=0;
    	string InputChar1;
    	string InputChar2;
    
    	character link;
    
    	ifstream InFile;
    	ofstream OutFile;
    
    	InFile.open("input.txt");
        OutFile.open("output2.txt");
    
        OutFile << "This is the the linked list function" << endl;
        OutFile<< "\n \n";
    
    	InFile>>InputChar1;
    	InFile>>InputChar2;
    	while(InFile != NULL)
    	{
    
    
    	if((InputChar1 == '{') || (InputChar2 == '}'))  //errors here
    	{
    		success = link.push(num, success);
    	}
    	else if((InputChar1 == '(') || (InputChar2 == ')')) //error here
    	{
    		success = link.push(num, success);
    	}
    	else //if(InputChar1 != InputChar2)
    		success=link.pop(num, success);
    	OutFile<<InputChar1;
    	OutFile<<InputChar1;
    
    	InFile>>InputChar1;
    	InFile>>InputChar2;
    	}
    retturn 0
    }

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    361
    It would help if you posted the errors.

    Also, you might want to post StackLLImple.cpp.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    Ok here is the LL and the errors

    :\Documents and Settings\Andrew\Desktop\Important stuff\ACS 279\Program1\stackLLClient.cpp(36) : error C2676: binary '==' : 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' does not define this operator or a
    conversion to a type acceptable to the predefined operator
    C:\Documents and Settings\Andrew\Desktop\Important stuff\ACS 279\Program1\stackLLClient.cpp(36) : error C2676: binary '==' : 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' does not define this operator or a
    conversion to a type acceptable to the predefined operator
    C:\Documents and Settings\Andrew\Desktop\Important stuff\ACS 279\Program1\stackLLClient.cpp(40) : error C2676: binary '==' : 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' does not define this operator or a
    conversion to a type acceptable to the predefined operator
    C:\Documents and Settings\Andrew\Desktop\Important stuff\ACS 279\Program1\stackLLClient.cpp(40) : error C2676: binary '==' : 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' does not define this operator or a
    conversion to a type acceptable to the predefined operator
    Error executing cl.exe.

    stackLLClient.obj - 4 error(s), 0 warning(s)

    Code:
    #include<iostream.h>
    #include"stackLL.h"
    
    //constructor
    character::character()
    {
        head=NULL;
        cur=NULL;
        pre=NULL;
    }
    
    //destructor
    character::~character()
    {
        node *ptr;
    
        while(head!=NULL)     //delete all the numbers
        {
            ptr=head->next;
            delete head;
            head=ptr;
        }
    }
    
    int character::push(int num, int success)
    {
    
        if(head==NULL)
        {
            head=new node;
            head->num=num;
            head->next=NULL;
            success=1;
        }
        else
        {
            cur=head;
            head=NULL;
            head=new node;
            head->num=num;
            head->next=cur;
            success=1;
        }
    
        return success;
    
    }
    
    
    //****remove at the head, this is stack, push at the head remove at the head.
    int character::pop(int num, int success)
    {
        node *temp;                          //make a temp pointer
    
        if(head!=NULL)               //if the list is not empty
        {
            temp=head->next;       //temp equal to head ->next
            delete head;           //delete head, remove the first data of the list
            head=temp; //now the second data of the list become the first data of the list
            return success=1;           //return success
        }
    
        else
            return success=0;           //if the list empty return fail
    
        return success;
    }
    
    
    //this function will print from the beginning of the list to the end of the list
    void character::print()
    {
        //if the list was empty print out an error message
        if(head==NULL)
        {
            cout << "There isn't any number" << endl;
            return;
        }
    
        //else while not the end of the list, print out the number and go to the next one
        else
        {
            cur=head;
            while(cur!=NULL)
            {
                cout << "number: " << cur->num << endl;
                cur=cur->next;
            }
        }
    }
    
    
    //the search function was search from the beginning of the head to the end of the
    //head.
    int character::search(int num, int success)
    {
    
        if(head==NULL)     //if there isn't any data return 0
        {
            success=0;
            return 0;
        }
    
        if(head->num == num)        //if delete the first node
        {
            cout << num << " was found" << endl;
            success=1;
            return 1;
        }
        else        //else delete any node other than the first node
        {
            cur=head;
            cur=cur->next;
    
    
            while(cur!=NULL)    //search all the to the end of the list
            {
    
                if(cur->num ==num)
                {
                    cout << num << " was found" << endl;
                    success=1;
    
                    return 1;
                }
                pre=cur;
                cur=cur->next;
                success=0;
            }// while
        } //else
    return success;
    }

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    You can't compare strings to individual characters. Try doing:
    Code:
    if(InputChar1 == "{" .....
    instead of using the single quotes. That should probably get rid of the errors, or if it doesn't then use:
    Code:
    (InputChar1.Compare("{") == 0)
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    New Question

    A more specific question: Why isn't my program pushing and poping correctly?
    Stack program: Develop a stack class using a linked list representation and string data and use it in a program that check C++ programs for the balancing symbols (/* */, ( ), [ ], and { }). The program should read in a C++ program and output one of three messages.

    Case 1: Symbol1 does not match symbol2.
    Example: ( does not match */.
    Case 2: End of program reached with unmatched symbol.
    Example: End of program reached with unmatched {.
    Case 3: All symbols correctly balanced.

    Read the name of the program file to be check from the command line.


    Input file:[] / * */ ( ) [] / * */ ( ) ( ]
    [CODE]
    #include<iostream>
    #include<fstream>
    #include<iomanip>
    #include<cstdlib>
    #include<string>
    #include"StackLLImple.cpp"

    using namespace std;

    int main()
    {
    char answer;
    //char InputChar1, InputChar2;
    int num;
    int success=0;
    string InputChar1;
    string InputChar2;

    character link;

    ifstream InFile;
    ofstream OutFile;

    InFile.open("input.txt");
    OutFile.open("output2.txt");

    OutFile << "This is the the linked list function" << endl;
    OutFile<< "\n \n";

    InFile>>InputChar1;
    InFile>>InputChar2;
    while(InFile != NULL)
    {


    if((InputChar1 == "[") || (InputChar2 == "]"))
    {
    success = link.push(num, success);
    success = link.push(num, success);
    }
    else if((InputChar1 == "(") || (InputChar2 == ")"))
    {
    success = link.push(num, success);
    }
    else if((InputChar1 == "/*") || (InputChar2 == "*/"))
    {
    success = link.push(num, success);
    }
    else //if(InputChar1 != InputChar2)

    success=link.pop(num, success);

    OutFile<<InputChar1;
    OutFile<<InputChar2;

    InFile>>InputChar1;
    InFile>>InputChar2;

    }
    return 0;
    }

    #include<iostream.h>
    #include"stackLL.h"

    //constructor
    character::character()
    {
    head=NULL;
    cur=NULL;
    pre=NULL;
    }

    //destructor
    character::~character()
    {
    node *ptr;

    while(head!=NULL) //delete all the numbers
    {
    ptr=head->next;
    delete head;
    head=ptr;
    }
    }

    int character:ush(int num, int success)
    {

    if(head==NULL)
    {
    head=new node;
    head->num=num;
    head->next=NULL;
    success=1;
    }
    else
    {
    cur=head;
    head=NULL;
    head=new node;
    head->num=num;
    head->next=cur;
    success=1;
    }

    return success;

    }


    //****remove at the head, this is stack, push at the head remove at the head.
    int character:op(int num, int success)
    {
    node *temp; //make a temp pointer

    if(head!=NULL) //if the list is not empty
    {
    temp=head->next; //temp equal to head ->next
    delete head; //delete head, remove the first data of the list
    head=temp; //now the second data of the list become the first data of the list
    return success=1; //return success
    }

    else
    return success=0; //if the list empty return fail

    return success;
    }


    //this function will print from the beginning of the list to the end of the list
    void character:rint()
    {
    //if the list was empty print out an error message
    if(head==NULL)
    {
    cout << "There isn't any number" << endl;
    return;
    }

    //else while not the end of the list, print out the number and go to the next one
    else
    {
    cur=head;
    while(cur!=NULL)
    {
    cout << "number: " << cur->num << endl;
    cur=cur->next;
    }
    }
    }


    //the search function was search from the beginning of the head to the end of the
    //head.
    int character::search(int num, int success)
    {

    if(head==NULL) //if there isn't any data return 0
    {
    success=0;
    return 0;
    }

    if(head->num == num) //if delete the first node
    {
    cout << num << " was found" << endl;
    success=1;
    return 1;
    }
    else //else delete any node other than the first node
    {
    cur=head;
    cur=cur->next;


    while(cur!=NULL) //search all the to the end of the list
    {

    if(cur->num ==num)
    {
    cout << num << " was found" << endl;
    success=1;

    return 1;
    }
    pre=cur;
    cur=cur->next;
    success=0;
    }// while
    } //else
    return success;
    }
    struct node
    {
    int num;
    node *next;
    };

    class character
    {
    public:
    character();
    ~character();

    int push(int num, int success);
    int pop(int num, int success);
    void print();
    int search(int num, int success);

    private:
    node *head;
    node *cur;
    node *pre;
    };
    [/NODE]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 04:11 PM
  3. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 12:36 PM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM

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