By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,529 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,529 IT Pros & Developers. It's quick & easy.

Syntax Error comparing private class data

P: 8
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,type 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.


Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<iomanip>
  3. #include<fstream>
  4. #include<string>
  5. class add
  6. {
  7. private:
  8.      int bus_no;
  9.      int route_no;
  10.      string source;
  11.      string dest;
  12.      int time;
  13.      int ticket_price;
  14.      string type;
  15.  
  16. public:
  17. void enter_bus()
  18. {
  19.         char temp[1000],busno[5];
  20.         string type("A/C");
  21.         cout<<"Enter Bus_No::";
  22.         cin>>busno;
  23.         bus_no=string(busno);
  24.         cout<<"Enter Bus_Route::";
  25.         cin>>route_no;
  26.         cout<<"Enter Source::";
  27.         cin>>source;
  28.         cout<<"Enter Destination::";
  29.         cin>>dest;
  30.         cout<<"Enter Time(Hrs:Min)::";
  31.         cin>>time;
  32.         cout<<"Enter Ticket_Price::";
  33. cin>>ticket_price;
  34.        // Type:
  35.         cout<<"Type of Bus (A/C or NonA/C)::";
  36.        // cout<<"Enter Type::";
  37.         cin>>type;
  38. }
  39.  void get_busno()
  40. {
  41.   bus_no;
  42. }
  43.  
  44. void display__bus()
  45. {
  46.  
  47.                 cout<<"Bus details"<<endl;
  48.                 cout<<"|"<<bus_no<<"|";
  49.                 <<setw(10)<<route_no<<"|";
  50.                 <<setw(15)<<source<<"|";
  51.                 <<setw(15)<<dest<<"|";
  52.                 <<setw(8)<<time<<"|";
  53.                 <<setw(6)<<type<<"|";
  54.                 <<setw(3)<<no_of_seats<<"|";
  55.                 <<setw(4)<<ticket_price<<endl;
  56. }
  57.  
  58. void intofile__bus()
  59. {
  60.  
  61.  
  62.                 cout<<"|"<<bus_no<<"|";
  63.                 <<setw(10)<<route_no<<"|";
  64.                                                 <<setw(15)<<source<<"|";
  65.                 <<setw(15)<<dest<<"|";
  66.                 <<setw(8)<<time<<"|";
  67.                 <<setw(6)<<type<<"|";
  68.                 <<setw(3)<<no_of_seats<<"|";
  69.                 <<setw(4)<<ticket_price<<endl;
  70. }
  71. };
  72. void main
  73. {
  74.  
  75.         add obj;
  76.         fstream file;
  77.         file.open("Bus.txt",std::ios_base::ate | std::ios_base::in | std::ios_base::out );
  78.         file.seekg(0, std::ios_base::end);
  79.         unsigned long length = file.tellg();
  80.         int obj_length=sizeof(obj);
  81.         int p=length/obj_length;
  82.         int n;
  83.         int option;
  84.         char choice;
  85.         int flag=0;
  86.         add obj2[p];
  87.         file.seekg(0,ios::beg);
  88.         cout<<"Want to enter details in the file (y/n)?"<<endl;
  89.         cin>>choice;
  90.         while((choice=='y') || (choice=='Y'))
  91.         {
  92.                  cout<<"Enter the option 1 for adding details:"<<endl;
  93.                 cin>>option;
  94.                 switch (option)
  95.                 {
  96.                  case 1: obj.enter_bus();
  97. for(n=0;n<=p;n++)
  98.                         {
  99.                            file.read((char *)&obj2[n],sizeof(obj2[n]));
  100.                            if(strcmp(obj2[n].get_busno(),obj.get_busno())==0)
  101.                              {
  102.                                cout<<"Sorry the bus already exist"<<endl;
  103.                                flag=2;
  104.                                break;
  105.                             }
  106.                             else
  107.                             {
  108.                               flag=1;
  109.                             }
  110.                          }
  111.  
  112.                 default: cout<<"Please enter the option 1 for adding details"<<endl;
  113.                 }
  114.  
  115.                 if(flag==1)
  116.                 {
  117.                    obj.enter_bus();
  118.                    obj.intofile_bus();
  119.                    file.write((char *)&obj,sizeof(obj));
  120.                    cout<<"Record inserted successfully...";
  121.                 }
  122.                else
  123.                 {
  124.                  cout<<"Want to enter the details again(y/n)?"<<endl;
  125.                  cin>>choice;
  126.                 }
  127.             }
Aug 8 '08 #1
Share this Question
Share on Google+
13 Replies


Expert 10K+
P: 11,448
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
Aug 8 '08 #2

Savage
Expert 100+
P: 1,764
Tell us what is this supposed to do:

Expand|Select|Wrap|Line Numbers
  1. void get_busno()
  2. {
  3.   bus_no;
  4.  
  5. }
,regards. ;)
Aug 8 '08 #3

P: 8
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,type 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


Expand|Select|Wrap|Line Numbers
  1. void main
  2. {
  3.  
  4.         add obj;
  5.         fstream file;
  6.         file.open("Bus.txt",std::ios_base::ate | std::ios_base::in | std::ios_base::out );
  7.         file.seekg(0, std::ios_base::end);
  8.         unsigned long length = file.tellg();
  9.         int obj_length=sizeof(obj);
  10.         int p=length/obj_length;
  11.         int n;
  12.         int option;
  13.         char choice;
  14.         int flag=0;
  15.         add obj2[p];
  16.         file.seekg(0,ios::beg);
  17.         cout<<"Want to enter details in the file (y/n)?"<<endl;
  18.         cin>>choice;
  19.         while((choice=='y') || (choice=='Y'))
  20.         {
  21.                  cout<<"Enter the option 1 for adding details:"<<endl;
  22.                 cin>>option;
  23.                 switch (option)
  24.                 {
  25.                  case 1: obj.enter_bus();
  26. for(n=0;n<=p;n++)
  27.                         {
  28.                            file.read((char *)&obj2[n],sizeof(obj2[n]));
  29.                           if(strcmp(obj2[n].get_busno(),obj.get_busno())==0)                           {
  30.                                cout<<"Sorry the bus already exist"<<endl;
  31.                                flag=2;
  32.                                break;
  33.                             }
  34.                             else
  35.                             {
  36.                               flag=1;
  37.                             }
  38.                          }
  39.  
  40.                 default: cout<<"Please enter the option 1 for adding details"<<endl;
  41.                 }
  42.  
  43.                 if(flag==1)
  44.                 {
  45.                    obj.enter_bus();
  46.                    obj.intofile_bus();
  47.                    file.write((char *)&obj,sizeof(obj));
  48.                    cout<<"Record inserted successfully...";
  49.                 }
  50.                else
  51.                 {
  52.                  cout<<"Want to enter the details again(y/n)?"<<endl;
  53.                  cin>>choice;
  54.                 }
  55.             }
Aug 8 '08 #4

P: 68
Check the line:

Expand|Select|Wrap|Line Numbers
  1. if(strcmp(obj2[n].get_busno(),obj.get_busno())==0)
The method
Expand|Select|Wrap|Line Numbers
  1. get_busno()
is void. Anyway, your attribute
Expand|Select|Wrap|Line Numbers
  1. bus_no
is an integer so using strcmp is a bit strange...

HTH
Aug 8 '08 #5

P: 8
Check the line:

Expand|Select|Wrap|Line Numbers
  1. if(strcmp(obj2[n].get_busno(),obj.get_busno())==0)
The method
Expand|Select|Wrap|Line Numbers
  1. get_busno()
is void. Anyway, your attribute
Expand|Select|Wrap|Line Numbers
  1. 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*)‚
Aug 8 '08 #6

P: 8
Tell us what is this supposed to do:

Expand|Select|Wrap|Line Numbers
  1. void get_busno()
  2. {
  3.   bus_no;
  4.  
  5. }
,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.
Aug 8 '08 #7

Banfa
Expert Mod 5K+
P: 8,916
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).
Aug 8 '08 #8

Banfa
Expert Mod 5K+
P: 8,916
Oh and while you are reading the posting guidelines read the bit about NOT double posting your questions

Banfa
Administrator
Aug 8 '08 #9

P: 8
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
Aug 8 '08 #10

P: 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).





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(busno);

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

but still error in this line

if(strcmp(string(obj2[n].get_bus_no()),obj.get_bus_no())==0)
Aug 8 '08 #11

Banfa
Expert Mod 5K+
P: 8,916
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!


if(strcmp(string(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?
Aug 8 '08 #12

Savage
Expert 100+
P: 1,764
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.
Aug 8 '08 #13

P: 68
yeah

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

Expand|Select|Wrap|Line Numbers
  1. std::string& get_bus_no() const {
  2.    return bus_no;
  3. }
  4.  
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.
Aug 8 '08 #14

Post your reply

Sign in to post your reply or Sign up for a free account.