How to overload istream to accept input giving like this: (5, 6, 7) - overloading

I have very little code other than code I use to test. I think this is the way to begin:
istream& operator>>(istream& os, Vect3D& in)
{
is >> in.x >> in.y >> in.z;
return is;
}
Testing code:
cout << "Testing overload of >> operator" << endl;
stringstream stringin("(1.5, 2.5, 3.5)");
stringin >> v;
cout << v << endl;
cout << "Should be:" << endl;
Vect3D vCorrect(1.5, 2.5, 3.5);
cout << vCorrect << endl << endl;
cout << "Testing chaining of overload of >> operator" << endl;
stringstream sin("(2, 3, 4) 5.9");
double x;
sin >> v >> x;
cout << v << " and " << x << endl;
cout << "Should be:" << endl;
cout << "(2, 3, 4) and 5.9" << endl << endl;

There are a few things that look problematic here. The overloaded operator function takes in a istream reference named 'os' but in the block you use 'is'. Also, to pipe from input streams to ints and doubles variables they are separated by spaces so your extra '(' and ',' are likely causing a problem as well.

Related

A lot of : "error: no match for 'operator>>' (operand types are 'std::basic_ostream<char>' and '<unresolved overloaded function types>') [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
This is the code :
#include <iostream>
using namespace std;
int main()
{
cout << " SSSSSSSSSSSSSSS" >> endl;
cout << " SS:::::::::::::::S" >> endl;
cout << "S:::::SSSSSS::::::S" >> endl;
cout << "S:::::S SSSSSSS" >> endl;
cout << "S:::::S" >> endl;
cout << "S:::::S" >> endl;
cout << "S::::SSSS" >> endl;
cout << " SS::::::SSSSS" >> endl;
cout << " SSS::::::::SS" >> endl;
cout << " SSSSSS::::S" >> endl;
cout << " S:::::S" >> endl;
cout << " S:::::S" >> endl;
cout << "SSSSSSS S:::::S" >> endl;
cout << "S::::::SSSSSS:::::S" >> endl;
cout << "S:::::::::::::::SS" >> endl;
cout << " SSSSSSSSSSSSSSS" >> endl;
cout << "IIIIIIIIII" >> endl;
cout << "I::::::::I" >> endl;
cout << "I::::::::I" >> endl;
cout << "II::::::II" >> endl;
cout << " I::::I" >> endl;
cout << " I::::I" >> endl;
cout << " I::::I" >> endl;
cout << " I::::I" >> endl;
cout << " I::::I" >> endl;
cout << " I::::I" >> endl;
cout << " I::::I" >> endl;
cout << " I::::I" >> endl;
cout << "II::::::II" >> endl;
cout << "I::::::::I" >> endl;
cout << "I::::::::I" >> endl;
cout << "IIIIIIIIII" >>
}
This is for school so don't ask why I am writing this code. But i get a lot of errors like the one in the title and I can't seem to find the problem so if anyone can help me, it'll be great!
When writing using the extraction or insertion operator (e.g >> or << respectively) you need to basically point it where you want the flow to go, and not change the flow for that whole statement, to put it in simple terms, in your case you want both insertion/extraction operators that you use to be facing like so << and that one is called an insertion operator.
This is the code you want:
#include <iostream>
using namespace std;
int main()
{
cout << " SSSSSSSSSSSSSSS" << endl;
}
and replicate that for as many times as you want there to be output statements.
Keep the flow going one direction and one direction only:
std::cout << "..." << std::endl;
You've got this going in and out at the same time.
You can't read from std::cout, it's for output only. It's an ostream which means it has no idea what >> even is. That operator isn't defined which is why you get that error.

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.

Compiler error when subtracting std::strings

I'm a beginner in c++ so I'm just messing around with some stuff while reading articles and books. But I spent 20 minutes re-reading this over and over again and I can't tell what's wrong with it.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
cout << "Hello there, this is your personal simple calculator.";
cin.get();
cout << "Type in what you want to do. (Addition, Subtraction, Multiplication, Division)"<< endl;
string c;
getline (cin, c);
if (c == "Addition")
{
string a_1;
string a_2;
cout << "You chose addition. Press enter" << endl ;
cin.get();
cout << "Type in the first value: ";
getline( cin, a_1);
cout << "Type in the second value: ";
getline (cin, a_2);
cout << a_1 << " + " << a_2 << " = " << a_1 + a_2 << endl;
}
else
{
cout << "You spelled it wrong.";
return 0;
}
if ( c == "Subtraction")
{
string s_1;
string s_2;
cout << "You chose subtraction. Press enter" << endl ;
cin.get();
cout << "Type in the first value: ";
getline (cin, s_1);
cout << "Type in the second value: ";
getline (cin, s_2);
cout << s_1 << " - " << s_2 << " = " << s_1 - s_2 << endl;
}
}
I get this as the only error
42 83 C:\Users\Jason\Desktop\Lesson - Header Files\LH1.cpp [Error] no match for 'operator-' in 'first_argument - second_argument'
I don't get it. The addition sign works and everything but the subtraction works.
So I messed around with something else
cout << first_argument << " - " << second_argument << " = " << first_argument - second_argument << endl;
But that subtraction part works fine. I don't get it. Help please
string can deal with text. When you add two strings, they are concatenated ("2"+"2"=="22", not "4"). String doesn't have operator-.
To deal with floating-point numbers, use double. To deal with integers, use int:
double d1, d2;
//some output
cin >> d1;
//some output
cin >> d2;
cout << d1 << " - " << d2 << " = " << (d1-d2) << '\n';
The addition part works because string + string results in stringstring. It appends the two strings and returns a new string.
But subtracting two strings doesn't mean anything.
What I believe you actually want to do is convert the strings into numbers and then subtract the numbers.
To do that, you need to use something like the following:
double val_1, val_2;
cin >> val_1;
cin >> val_2;
cout << "result is " << (val_1 - val_2) << endl;
I put the subtraction inside parenthesis because I believe the << aka "shift" operator is on the same level as multiplication, which means that without them it would try to evaluate ("result is " << val_1) - (val_2 << endl).
Since I am not sure on operator precedence I checked http://en.cppreference.com/w/cpp/language/operator_precedence and found that << is lower than subtraction, so my parenthesis weren't necessary.
#include <iostream>
#include <string>
#include <limits>
using namespace std;
int main()
{
cout << "Hello there, this is your personal simple calculator.";
cin.get();
cout << "Type in what you want to do. (Addition, Subtraction, Multiplication, Division)" << endl;
string c;
getline(cin, c);
if (c == "Addition")
{
int a_1;
int a_2;
cout << "You chose addition. Press enter" << endl;
cin.get();
cout << "Type in the first value: ";
cin >> a_1;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Type in the second value: ";
cin >> a_2;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << a_1 << " + " << a_2 << " = " << a_1 + a_2 << endl;
}
else if (c == "Subtraction")
{
int s_1;
int s_2;
cout << "You chose subtraction. Press enter" << endl;
cin.get();
cout << "Type in the first value: ";
cin >> s_1;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Type in the second value: ";
cin >> s_2;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << s_1 << " - " << s_2 << " = " << s_1 - s_2 << endl;
}
else
{
cout << "You spelled it wrong.";
return 0;
}
}
Use if else statements because otherwise your code won't have a chance to check to see if the user is trying to Subtract. It is best to leave the else at the very end.
Also changed the strings to ints because strings cannot subtract as they are not numbers.
On a final note, I used the cin.clear() & cin.ignore() to flush the cin buffer.

How can I get a hex value in c++ to a proper string?

I am still working on a database for movies and I would like to show the user what he has input to the file.
However when i use cout << lisafilm << it provides me with hex value. Therefore, I need to conver hex to string.
Snippet of trouble.
void sisend()
{
string nimi;
int aasta;
long int hinne;
string vaadatud;
ofstream lisafilm("andmebaas.txt", ios::app);
cout <<"Sisestage filmi nimi." << endl;
cin >> nimi;
cout << "Sisestage filmi aasta." << endl;
cin >> aasta;
cout << "Sisestage filmi hinne." << endl;
cin >> hinne;
cout << "Kas olete filmi juba vaadanud?" << endl;
cout << "Vastake 'Jah' voi 'Ei'" << endl;
cin >> vaadatud;
lisafilm<< nimi << " " << aasta << " " << hinne<< " " << vaadatud << endl;
lisafilm.close();
{
system("CLS");
int hex_str = lisafilm ;
cout << "Aitah kasutamast andmebaasi." << endl;
system("pause");
cin.get ();
}
main();
}
when i use cout << lisafilm it provides me with hex value
This is because you are trying to output an ofstream. When this happens, operator void* gets called, producing an arbitrary hex sequence which is tied to your stream, but ultimately is very much useless.
Try this:
std::stringstream ss;
ss << std::hex << lisafilm;
const std::string s = ss.str();
lisafilm is a stream, not a string
If you want to copy lisafilm to cout something like cout << lisafilm.rdbuf(); would do the trick (assuming that lisafilm is an ostream or istream and that lisafilm's position is the start of the file.
Your code is very poorly formatted, I don't think what you posted would compile. If you clean it up stackoverflow may be able to help you more.