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. 13 2588
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
Tell us what is this supposed to do: - void get_busno()
-
{
-
bus_no;
-
-
}
,regards. ;)
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 -
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;
-
}
-
}
Check the line: - if(strcmp(obj2[n].get_busno(),obj.get_busno())==0)
The method
is void. Anyway, your attribute
is an integer so using strcmp is a bit strange...
HTH
Check the line: - if(strcmp(obj2[n].get_busno(),obj.get_busno())==0)
The method
is void. Anyway, your attribute
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*)â
Tell us what is this supposed to do: - void get_busno()
-
{
-
bus_no;
-
-
}
,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.
Banfa 9,065
Expert Mod 8TB
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).
Banfa 9,065
Expert Mod 8TB
Oh and while you are reading the posting guidelines read the bit about NOT double posting your questions
Banfa
Administrator
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
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)
Banfa 9,065
Expert Mod 8TB
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?
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.
yeah
if(strcmp(string(obj2[n].get_bus_no()),obj.get_bus_no())==0)
When you declare your get_bus_no() method so: - 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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: richardkreidl |
last post by:
I have the following hash script that I use to compare two text files.
'Class
Public Class FileComparison
Public Class FileComparisonException
Public Enum ExceptionType
U 'Unknown
A 'Add...
|
by: martin1 |
last post by:
I just use DataSet to bind DataSetGrid and display from SQL DB.
when starting run in Visual Studio 2005, get "Line 1: Incorrect syntax near
'1'" error message from below fill line,
...
|
by: BenCoo |
last post by:
Hello,
In a Binary Search Tree I get the error : Object must be of type String
if I run the form only with the "Dim bstLidnummer As New BinarySearchTree"
it works fine.
Thanks for any...
|
by: W Karas |
last post by:
Would the fear factor for concepts be slightly reduced if,
instead of:
concept C<typename T>
{
typename T::S;
int T::mem();
int nonmem();
};
|
by: Superman859 |
last post by:
Hello everyone. Heads up - c++ syntax is killing me. I do quite well
in creating a Java program with very few syntax errors, but I get them
all over the place in c++. The smallest little things...
|
by: Travis |
last post by:
I'm creating a real simple tree. No sorting and every node can have
infinite children.
// TreeNode.h
#ifndef TREENODE_H
#define TREENODE_H
#include <iostream>
#include <iomanip>
|
by: Frogpolish |
last post by:
well after searching ans coming up with nothing i figured id post something in here to see if i could get some help. i have an access db that keeps track of flights that ive flown. its a logbook...
|
by: Banfa |
last post by:
So I have a little problem, I have a template class and that class contains a template function; now what I want to do is declare that function in the class (or indeed the entire class) as a friend...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |