Syntax Error comparing private class data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ishakarthika
    New Member
    • Aug 2008
    • 8

    Syntax Error comparing private class data

    how can i compare the a private variable of a class and a value in the column of a text file.

    there is a syntax error in my code while comparing. senario is i am getting bus details like busno, source , destination,typ e of bus, price/head . I should compare the busno which i get through object to the bus no which is present in a .txt file. and contents in the text file is as the format below.
    |1488|xxx|uuuu| A/c|150.
    |1422|mmm|oooo| nonA/C|900.


    Code:
    #include<iostream>
    #include<iomanip>
    #include<fstream>
    #include<string>
    class add
    {
    private:
         int bus_no;
         int route_no;
         string source;
         string dest;
         int time;
         int ticket_price;
         string type;
    
    public:
    void enter_bus()
    {
            char temp[1000],busno[5];
            string type("A/C");
            cout<<"Enter Bus_No::";
            cin>>busno;
            bus_no=string(busno);
            cout<<"Enter Bus_Route::";
            cin>>route_no;
            cout<<"Enter Source::";
            cin>>source;
            cout<<"Enter Destination::";
            cin>>dest;
            cout<<"Enter Time(Hrs:Min)::";
            cin>>time;
            cout<<"Enter Ticket_Price::";
    cin>>ticket_price;
           // Type:
            cout<<"Type of Bus (A/C or NonA/C)::";
           // cout<<"Enter Type::";
            cin>>type;
    }
     void get_busno()
    {
      bus_no;
    }
    
    void display__bus()
    {
    
                    cout<<"Bus details"<<endl;
                    cout<<"|"<<bus_no<<"|";
                    <<setw(10)<<route_no<<"|";
                    <<setw(15)<<source<<"|";
                    <<setw(15)<<dest<<"|";
                    <<setw(8)<<time<<"|";
                    <<setw(6)<<type<<"|";
                    <<setw(3)<<no_of_seats<<"|";
                    <<setw(4)<<ticket_price<<endl;
    }
    
    void intofile__bus()
    {
    
    
                    cout<<"|"<<bus_no<<"|";
                    <<setw(10)<<route_no<<"|";
                                                    <<setw(15)<<source<<"|";
                    <<setw(15)<<dest<<"|";
                    <<setw(8)<<time<<"|";
                    <<setw(6)<<type<<"|";
                    <<setw(3)<<no_of_seats<<"|";
                    <<setw(4)<<ticket_price<<endl;
    }
    };
    void main
    {
    
            add obj;
            fstream file;
            file.open("Bus.txt",std::ios_base::ate | std::ios_base::in | std::ios_base::out );
            file.seekg(0, std::ios_base::end);
            unsigned long length = file.tellg();
            int obj_length=sizeof(obj);
            int p=length/obj_length;
            int n;
            int option;
            char choice;
            int flag=0;
            add obj2[p];
            file.seekg(0,ios::beg);
            cout<<"Want to enter details in the file (y/n)?"<<endl;
            cin>>choice;
            while((choice=='y') || (choice=='Y'))
            {
                     cout<<"Enter the option 1 for adding details:"<<endl;
                    cin>>option;
                    switch (option)
                    {
                     case 1: obj.enter_bus();
    for(n=0;n<=p;n++)
                            {
                               file.read((char *)&obj2[n],sizeof(obj2[n]));
                               if(strcmp(obj2[n].get_busno(),obj.get_busno())==0)
                                 {
                                   cout<<"Sorry the bus already exist"<<endl;
                                   flag=2;
                                   break;
                                }
                                else
                                {
                                  flag=1;
                                }
                             }
    
                    default: cout<<"Please enter the option 1 for adding details"<<endl;
                    }
    
                    if(flag==1)
                    {
                       obj.enter_bus();
                       obj.intofile_bus();
                       file.write((char *)&obj,sizeof(obj));
                       cout<<"Record inserted successfully...";
                    }
                   else
                    {
                     cout<<"Want to enter the details again(y/n)?"<<endl;
                     cin>>choice;
                    }
                }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    So you want to start a guessing game? What can we win? Any strings attached?
    It doesn't work like that; tell us what the problem is and show the relevant code;
    dumping all your code here and make us guess is ridiculous.

    kind regards,

    Jos

    Comment

    • Savage
      Recognized Expert Top Contributor
      • Feb 2007
      • 1759

      #3
      Tell us what is this supposed to do:

      [CODE=c]void get_busno()
      {
      bus_no;

      }[/CODE]

      ,regards. ;)

      Comment

      • ishakarthika
        New Member
        • Aug 2008
        • 8

        #4
        Syntax Error comparing private class data

        how can i compare the a private variable of a class and a value in the column of a text file.

        there is a syntax error in my code while comparing. senario is i am getting bus details like busno, source , destination,typ e of bus, price/head . I should compare the busno which i get through object to the bus no which is present in a .txt file. and contents in the text file is as the format below.
        |1488|xxx|uuuu| A/c|150.
        |1422|mmm|oooo| nonA/C|900.
        please reply me


        [code=cpp]
        void main
        {

        add obj;
        fstream file;
        file.open("Bus. txt",std::ios_b ase::ate | std::ios_base:: in | std::ios_base:: out );
        file.seekg(0, std::ios_base:: end);
        unsigned long length = file.tellg();
        int obj_length=size of(obj);
        int p=length/obj_length;
        int n;
        int option;
        char choice;
        int flag=0;
        add obj2[p];
        file.seekg(0,io s::beg);
        cout<<"Want to enter details in the file (y/n)?"<<endl;
        cin>>choice;
        while((choice== 'y') || (choice=='Y'))
        {
        cout<<"Enter the option 1 for adding details:"<<endl ;
        cin>>option;
        switch (option)
        {
        case 1: obj.enter_bus() ;
        for(n=0;n<=p;n+ +)
        {
        file.read((char *)&obj2[n],sizeof(obj2[n]));
        if(strcmp(obj2[n].get_busno(),ob j.get_busno())= =0) {
        cout<<"Sorry the bus already exist"<<endl;
        flag=2;
        break;
        }
        else
        {
        flag=1;
        }
        }

        default: cout<<"Please enter the option 1 for adding details"<<endl;
        }

        if(flag==1)
        {
        obj.enter_bus() ;
        obj.intofile_bu s();
        file.write((cha r *)&obj,sizeof(o bj));
        cout<<"Record inserted successfully... ";
        }
        else
        {
        cout<<"Want to enter the details again(y/n)?"<<endl;
        cin>>choice;
        }
        }[/code]

        Comment

        • pootle
          New Member
          • Apr 2008
          • 68

          #5
          Check the line:

          Code:
          if(strcmp(obj2[n].get_busno(),obj.get_busno())==0)
          The method
          Code:
          get_busno()
          is void. Anyway, your attribute
          Code:
          bus_no
          is an integer so using strcmp is a bit strange...

          HTH

          Comment

          • ishakarthika
            New Member
            • Aug 2008
            • 8

            #6
            Originally posted by pootle
            Check the line:

            Code:
            if(strcmp(obj2[n].get_busno(),obj.get_busno())==0)
            The method
            Code:
            get_busno()
            is void. Anyway, your attribute
            Code:
            bus_no
            is an integer so using strcmp is a bit strange...

            HTH



            diclared bus_no in the class as string and in public scope.
            and still getting error on this line.

            if(strcmp(obj2[n].bus_no,obj.bus _no)==0)
            as

            newadd1.cpp:102 : error: cannot convert âstd::stringâ to âconst char*â for argument â1â to âint strcmp(const char*, const char*)â

            Comment

            • ishakarthika
              New Member
              • Aug 2008
              • 8

              #7
              Originally posted by Savage
              Tell us what is this supposed to do:

              [CODE=c]void get_busno()
              {
              bus_no;

              }[/CODE]

              ,regards. ;)

              i though calling a private variable of a class using object would throw an error so called the member variable through a public function.

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Please read our posting guidelines taking note of the sections on choosing a thread title and composing your post (using code tags round the posted code).


                You have posted code and said you have a syntax error, this is not enough you should post the exact error you are getting (copy and past it) and you need to tell us on what line of code it is appearing.

                However in general

                1. You have declared main as returning void. This invokes undefined behaviour, all bets are now off your program could do anything. Examining any further errors after this one is pointless.

                Main MUST be declared as either

                int main()

                or

                int main(int argc, char **argp)

                that is it must return int.

                2. Private data is only accessible inside the class. I don't see you calling any class methods so I assume your syntax error is attempting to access private data. You will need to provide access to the data by either creating a method to return the data, creating a method to do the comparison for you, changing the access specifier of the data being declared (but not this last one is really a hack).

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Oh and while you are reading the posting guidelines read the bit about NOT double posting your questions

                  Banfa
                  Administrator

                  Comment

                  • ishakarthika
                    New Member
                    • Aug 2008
                    • 8

                    #10
                    Originally posted by Banfa
                    Oh and while you are reading the posting guidelines read the bit about NOT double posting your questions

                    Banfa
                    Administrator
                    sorry for that without knowing did

                    Comment

                    • ishakarthika
                      New Member
                      • Aug 2008
                      • 8

                      #11
                      Originally posted by Banfa
                      Please read our posting guidelines taking note of the sections on choosing a thread title and composing your post (using code tags round the posted code).


                      You have posted code and said you have a syntax error, this is not enough you should post the exact error you are getting (copy and past it) and you need to tell us on what line of code it is appearing.

                      However in general

                      1. You have declared main as returning void. This invokes undefined behaviour, all bets are now off your program could do anything. Examining any further errors after this one is pointless.

                      Main MUST be declared as either

                      int main()

                      or

                      int main(int argc, char **argp)

                      that is it must return int.

                      2. Private data is only accessible inside the class. I don't see you calling any class methods so I assume your syntax error is attempting to access private data. You will need to provide access to the data by either creating a method to return the data, creating a method to do the comparison for you, changing the access specifier of the data being declared (but not this last one is really a hack).





                      yeah
                      i have did but again error.

                      declared a function to get bus no
                      public:
                      void get_bus_no()
                      {
                      cout<<bus_no;
                      }

                      while getting the bus_no i used a temporary variable busno converting it into string , assignin it to bus_no and declared bus_no as string in private.

                      cout<<"Enter Bus_No::";
                      cin>>busno;
                      bus_no=string(b usno);

                      i have declared bus_no as private.
                      private:
                      string bus_no;

                      but still error in this line

                      if(strcmp(strin g(obj2[n].get_bus_no()), obj.get_bus_no( ))==0)

                      Comment

                      • Banfa
                        Recognized Expert Expert
                        • Feb 2006
                        • 9067

                        #12
                        Originally posted by ishakarthika
                        declared a function to get bus no
                        public:
                        void get_bus_no()
                        {
                        cout<<bus_no;
                        }
                        This doesn't get the bus number it prints it to the console!


                        Originally posted by ishakarthika
                        if(strcmp(strin g(obj2[n].get_bus_no()), obj.get_bus_no( ))==0)
                        What type is strcmp expecting for the parameters being passed to it? What type is get_bus_no() returning? Are these types compatible?

                        Comment

                        • Savage
                          Recognized Expert Top Contributor
                          • Feb 2007
                          • 1759

                          #13
                          Again,your function is of void type and void functions can't return any value.What you did is just printed the value of bus_no to the screen,but you need to return that value to the calling function.So declare your get_busno() function to return the same type of which is bus_no made.

                          EDIT:Also bus_no is a int,but it's the string you need to return.

                          Comment

                          • pootle
                            New Member
                            • Apr 2008
                            • 68

                            #14
                            Originally posted by ishakarthika
                            yeah

                            if(strcmp(strin g(obj2[n].get_bus_no()), obj.get_bus_no( ))==0)
                            When you declare your get_bus_no() method so:

                            Code:
                            std::string& get_bus_no() const {
                               return bus_no;
                            }
                            Then you do not need strcmp anymore. The strcmp function is only used for comparing char*. You can find out all about standard library strings and string comparison / manipulation on the internet.

                            Comment

                            Working...