Comma separate in overloading ostream and istream in struct - c++

I want my struct PersonData to get the output (firstname,lastname,personage,countrycode,income) with the comma seperator.
I have written this code but it only works with whitespace.
So output will be (firstname lastname personage countrycode income)
How can i change this to ,(comma) instead?
std::ostream& operator<<(std::ostream& output, const PersonData &pd)
{
output << pd.firstName << " " << pd.lastName << " " << pd.age << " " << pd.countryCode <<
" " << pd.income;
return output;
}
std::istream& operator>>(std::istream& indata, PersonData &pd)
{
indata >> pd.firstName >> pd.lastName >> pd.age >> pd.countryCode >> pd.income;
}

You can just change symbols (string literals) you've been outputting between PersonData's fields.
std::ostream& operator<<(std::ostream& output, const PersonData &pd)
{
output << pd.firstName << ", " << pd.lastName << ", " << pd.age << ", " << pd.countryCode <<
", " << pd.income;
return output;
}

Related

Reading and writing from a file

I need to make card catalog entries for a library system and will need a menu and save the card and read the card from a file. I'm just looking for help and not saving for anyone to do it just tips if you have any because I want ro learn and not just copy and paste.
I already finished the information they need to add but having a problem making them save and read from a file and choosing from the menu to add new entry, review entry.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
struct libary
{
string title;
string author;
string ISBN_code;
int page_count;
int publish_year;
} s[10];
bool processMenu()
{
int choice, spot;
cout << "Main Menu" << endl;
cout << "Select your options" << endl;
cout << "1. Add a new entry to the system" << endl;
cout << "2. Review an entry" << endl;
cout << "3. Save entries to a file" << endl;
cout << "4. Read entries from a file" << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "which spot do you want to add a entry to the system " << endl;
cin >> spot;
break;
case 2:
cout << "which spot do you want to review to a file " << endl;
cin >> spot;
break;
case 3:
cout << "Save entries to a file" << endl;
ofstream myfile;
myfile.open("file.txt");
myfile << "this will show in the file";
break;
case 4:
cout << "read entries from a file :" << endl;
break;
case 7:
return 0;
}
}
int main()
{
while (!processMenu())
{
cout << "Sorry, that is not a valid choice." << endl;
cout << "Please try again." << endl << endl << endl;
}
cout << " Enter information of each card catalog" << endl;
for (int i = 0; i < 10; i++)
{
cout << endl;
cout << " Enter title " << endl;
cin >> s[i].title;
cout << endl;
cout << " Enter author " << endl;
cin >> s[i].author;
cout << endl;
cout << " Enter Page count " << endl;
cin >> s[i].page_count;
cout << endl;
cout << " Enter publish year " << endl;
cin >> s[i].publish_year;
cout << endl;
cout << " Enter ISBN code, it should be 13 digits" << endl;
cin >> s[i].ISBN_code;
while (s[i].ISBN_code.length() != 13)
{
cout << " Please enter a ISBN code thats 13 digits" << endl;
cin >> s[i].ISBN_code;
cout << endl;
cout << endl;
}
}
cout << " Displaying Information" << endl;
for (int i = 0; i < 10; i++)
{
cout << " title: " << s[i].title << endl;
cout << " author: " << s[i].author << endl;
cout << " page count: " << s[i].page_count << endl;
cout << " publish year: " << s[i].publish_year << endl;
cout << " ISBN code: " << s[i].ISBN_code << endl;
}
return 0;
}
It lets me pick a spot but won't add new entry nor review or save/read.
You can make your own type writable to a stream (cout or ofstream) and/or readable from a stream (cin or ifstream) just by overriding its output (operator<<) and/or input operator (operator>>).
#include <fstream> // ifstream, ofstream
#include <iostream> // cin, cout
#include <string> // getline, string
using namespace std;
struct Card
{
// Writing to the standard output ('cout').
friend ostream& operator<<(ostream& os, const Card& card);
// Reading from the standard input ('cin').
friend istream& operator>>(istream& is, Card& card);
// Writing to a file ('ofstream').
friend ofstream& operator<<(ofstream& os, const Card& card);
// Reading from a file ('ifstream).
friend ifstream& operator>>(ifstream& is, Card& card);
string title = "";
string author = "";
string isbn = "";
int page_count = -1;
int publish_year = -1;
};
ostream& operator<<(ostream& os, const Card& card)
{
os << "Title: " << card.title << '\n'
<< "Author: " << card.author << '\n'
<< "ISBN: " << card.isbn << '\n'
<< "Page count: " << card.page_count << '\n'
<< "Publish year: " << card.publish_year << '\n';
return os;
}
istream& operator>>(istream& is, Card& card)
{
// 'getline' needs for the reading of strings because the title of a book or
// its author's name may contain Space (' ') characters. 'cin' and
// 'ifstream' separate input by every whitespace. So, if we want to allow
// spaces in our fields we have to separate them by a special delimiter
// character. We use the Newline ('\n') character here to separate fields.
// `page_count` and `publish_year` are numbers, they don't contain
// whitespaces, so they simply can be read with 'operator>>'.
cout << "Title: ";
std::getline(is, card.title);
cout << "Author: ";
std::getline(is, card.author);
cout << "ISBN code: ";
std::getline(is, card.isbn);
cout << "Page count: ";
is >> card.page_count;
cout << "Publish year: ";
is >> card.publish_year;
return is;
}
// The next two functions is for files.
ofstream& operator<<(ofstream& os, const Card& card)
{
os << card.title << '\n'
<< card.author << '\n'
<< card.isbn << '\n'
<< card.page_count << '\n'
<< card.publish_year << '\n';
return os;
}
ifstream& operator>>(ifstream& is, Card& card)
{
std::getline(is, card.title);
std::getline(is, card.author);
std::getline(is, card.isbn);
is >> card.page_count;
is >> card.publish_year;
return is;
}
int main()
{
Card card;
// Read the informations of a card from the standard input.
cin >> card;
// Write the informations of a card to the standard output.
cout << card;
// Write the informations of a card to "file.txt".
ofstream ofs("file.txt");
ofs << card;
ofs.close();
// Read the informations of a card from "file.txt".
ifstream ifs("file.txt");
ifs >> card;
ifs.close();
return 0;
}
Important: For the sake of simplicity, I omitted error checking at the opening of a file and at the writing/reading of a stream. In your code you should do these.

stringstream operator input failing

What is wrong with this overloaded operator?
I am trying to parse a stringstream to an object which has the members a, b and c as integers.
istream& operator>> (istream& in, Feedback& object) {
cout << __PRETTY_FUNCTION__ << endl;
in >> object.a;
in >> object.b;
in >> object.c;
cout << object.a << " " << object.b << " " << object.c << endl;
return in;
}
The last cout is printing 0 for all members.
I can see that the stringstream is properly filled before the input operator in this code...
cout << __PRETTY_FUNCTION__ << ": " << ss.str().c_str() << endl;
ss >> feedback;
this cout prints:
Feedback parseFeedbackData(unsigned char*, int): 10 2 4
The output overloaded operator is working fine. You can find the code below:
ostream& operator<< (ostream& out, Feedback& object) {
cout << __PRETTY_FUNCTION__ << endl;
out << object.a << " " << object.b << " " << object.c;
return out;
}
The output of ss.str().c_str() does not necessarily give a clue as to what the other states of the istringstream object are.
You should add tests to make sure that the reads are successful.
istream& operator>> (istream& in, Feedback& object) {
cout << __PRETTY_FUNCTION__ << endl;
if ( !(in >> object.a) )
{
cout << "Problem reading a\n";
return in;
}
if ( !(in >> object.b) )
{
cout << "Problem reading b\n";
return in;
}
if ( !(in >> object.c) )
{
cout << "Problem reading c\n";
return in;
}
cout << object.a << " " << object.b << " " << object.c << endl;
return in;
}
This will help you figure out where the problem lies.

Why does using my print method with std::cout result in an error?

#include <iostream>
using namespace std;
class Fam
{
public:
char you, urmom, urdad;
void addPerson(char y, char m, char f)
{
you = y;
urmom = m;
urdad = f;
}
};
class Tree: public Fam
{
public:
void showFamtree()
{
cout<< "Name: " << you << endl;
cout<< "Mother's name: " << urmom <<endl;
cout<< "Father's name: " << urdad <<endl;
}
};
int main(void)
{
Tree tree;
char a,b,c;
cin >> a;
cin >> b;
cin >> c;
tree.addPerson(a,b,c);
cout<< "Family tree: " << tree.showFamtree() <<endl;
return 0;
}
I wanted to print the family tree with
the person's name, mother's name, father's name
but when I compile it, I get the following error:
invalid operands to binary expression (basic_ostream<char, std::char_traits<char> > and void)
tree.showFamtree() returns nothing (i.e. void), it doesn't make any sense to try to pass it to std::cout. You might change
cout<< "Family tree: " << tree.showFamtree() <<endl;
to
cout << "Family tree: " << endl;
tree.showFamtree();
If you define operator << like this
ostream& operator << ( ostream & ostr , const Tree & t ){
ostr << "Name:" << t.you << endl
<< "Mother's name:" << t.urmom << endl
<< "Father's name:" << t.urdad << endl;
return ostr;
}
then you can use
cout<< "Family tree: " << tree <<endl;
This is known as operator overloading in C++.
To use something similar to this:
void showFamtree()
{
cout<< "Name: " << you << endl;
cout<< "Mother's name: " << urmom <<endl;
cout<< "Father's name: " << urdad <<endl;
}
with:
cout << "Family tree: " << tree.showFamtree() << endl;
One C++ approach would be to use std::stringstream, as in:
std::string showFamtree()
{
std::stringstream ss;
ss << "Name: " << you << endl;
ss << "Mother's name: " << urmom <<endl;
ss << "Father's name: " << urdad <<endl;
return (ss.str());
}
I also often add a label. So consider using
std::string showFamtree(std::string label)
{
std::stringstream ss;
ss << label;
ss << "Name: " << you << endl;
ss << "Mother's name: " << urmom <<endl;
ss << "Father's name: " << urdad <<endl;
return (ss.str());
}
and change invocation to
cout << tree.showFamtree("Family tree: ") << endl;
Note - Perhaps the label should be on its own line, for consistent white space on the left of the 'tree'.

OOP: Method for cout output

I have to create a method, which prints all collected data's on screen, here is my try:
bool UnPackedFood::printer() {
cout << " -- Unpacked Products --" << endl;
cout << "barcode: " << getBarcode() << endl;
cout << "product name: " << getBezeichnung() << endl << endl;
cout << "weight: " << getGewicht() << endl;
cout << "price" << getKilopreis() << endl;
return true;
}
In my main:
UnPackedFood upf;
cout << upf.printer();
This shows me the correct output, but it still delivers me a bool value back, which I actually dont need. I tried to declare the method as void, but thats not working.
You should overload << operator for output stream. Then when you type cout << upf it will print your product.
Take a look at this example and try to do something similar to following snippet:
class UnPackedFood {
...
public:
...
friend ostream & operator<< (ostream &out, const UnPackedFood &p);
};
ostream & operator<< (ostream &out, const UnPackedFood &p) {
out << " -- Unpacked Products --" << endl;
out << "barcode: " << p.getBarcode() << endl;
out << "product name: " << p.getBezeichnung() << endl << endl;
out << "weight: " << p.getGewicht() << endl;
out << "price" << p.getKilopreis() << endl;
return out;
}
Three possible solutions:
Don't do cout << upf.printer();, the output is not needed since the function itself does the output.
Instead of writing to the output in the printer function, append to a string and return the string.
Make an overloaded operator<< for UnPackedFood, so you can just do std::cout << upf;

Print function to an output file

I am about to be finished with a program I am writing and have reached a roadblock.
I am trying to print the contents of a function called print which is called by a pointer.
My problem is I need to print the contents of the function to an output file and am not sure how.
This is my print function:
void English::Print(){
int formatlength = 38 - (static_cast<int>(firstName.size() + lastName.size()));
cout << firstName << " " << lastName;
cout << setw(formatlength) << finalExam;
cout << setprecision(2) << fixed << setw(11) << FinalGrade();
cout << setw(4) << Lettergrade() << endl;
}
This is the implementation of the print function:
for (int i = 0; i <= numStudents - 1; i++) {
if (list[i]->GetSubject() == "English") {
list[i]->Print();
}
}
Where the for loop is cycling through my list of Students.
My goal is that the list[i]->Print() will print to my output file.
Simply replace cout with an ostream object, something like :
void English::Print(ostream& fout){
//ofstream of("myfile.txt", std::ios_base::app);
int formatlength = 38 - (static_cast<int>(firstName.size() + lastName.size()));
fout << firstName << " " << lastName;
fout << setw(formatlength) << finalExam;
fout << setprecision(2) << fixed << setw(11) << FinalGrade();
fout << setw(4) << Lettergrade() << endl;
}
Also, you can overload << operator too in your class English
friend ostream& operator <<( ostream& os, const English& E )
{
//
return os;
}
And then can simply use:
fout << list[i] ;
Besides the answers above, I think you should try this way, using the C's original file redirection function:
Put this instruction in the first line of your main function:
int main(){
freopen("out.txt", "w", stdout);
//your codes
The "out.txt" is the file you wanna to put the data in, "w" means you want to write in the file, and stdout is the standard output stream that has been redirected.