no match for 'operator <<' in 'std::operator<< - c++

void printAst(int x)
{
for( int i = 0; i < x; i++)
{
cout << "*";
}
cout << " (" << x << ")" << endl;
}
void printHisto(int histo[])
{
//cout.precision(3);
int count = 0;
for(double i = -3.00; i < 3.00; i += 0.25)
{
cout << setprecision(3) << i << " to " << i + 0.25 << ": " << printAst(histo[count]) << endl;
// cout << setw(3) << setfill('0') << i << " to " << i + 0.25 << ": " << histo[count] << endl;
count ++;
}
}
I want my output to be formatted like this, so I used setprecision(3), which also does not work.
-3.00 to -2.75: (0)
-2.75 to -2.50: * (1)
-2.50 to -2.25: * (1)
-2.25 to -2.00: * (6)
-2.00 to -1.75: ***** (12)
So instead it is formatted like this
-3 to -2.75: 3
-2.75 to -2.5: 4
-2.5 to -2.25: 5
-2.25 to -2: 0
-2 to -1.75: 0
The main problem however, is that when I try to call printAst on to histo[count]. This is what is causing this error. PrintAst is used to print the asteriks, histo[count] provides the amount of asteriks to print.
cout << setprecision(3) << i << " to " << i + 0.25 << ": " << printAst(histo[count]) << endl;

You seem to have a misunderstanding about how chaining << works in streams.
cout << 42 looks like an operator expression with two operands, but it's really a call to a function with two parameters (the name of the function is operator<<). This function returns a reference to the stream, which enables the chaining.
An expression like this:
cout << 1 << 2;
is equivalent to this:
operator<<( operator<<(cout, 1), 2);
Now, the problem is that a parameter to a function can't be void but that's what printAst returns. Instead, you need to return something that can be streamed - in other words, something that operator<< is already overloaded for. I'd suggest std::string:
std::string printAst(int x);
{
std::string s = " (" + std::string(x,'*') + ")";
return s;
}
You can read more about operator overloading.

Related

Cout printing with array pointers - weird behavior

I was playing around with pointers and got results I did not expect:
#include <iostream>
#include <vector>
int main() {
int arr[4] = { 1, 2, 3, 4 };
int* pArr = arr;
std::cout << "First number: " << *pArr << " at address: " << pArr;
pArr++;
std::cout << "\nSecond number: " << *pArr << " at address: " << pArr;
pArr++;
std::cout << "\nThird number: " << *pArr << " at address: " << pArr;
pArr++;
std::cout << "\nFourth number: " << *pArr << " at address: " << pArr;
int* pArr2 = arr;
std::cout << "\n"
<< *pArr2++ << "\n"
<< *pArr2++ << "\n"
<< *pArr2++ << "\n"
<< *pArr2++ << "\n";
/*
int* pArr2 = arr;
std::cout << "\n"
<< ++ * pArr2 << "\n"
<< * ++pArr2 << "\n";
*/
}
The two different results:
1 2 3 4 - as expected using the first method
4 3 2 1 - using cout with multiple arguments I do not know the proper name.
So my question is - why does this happen? Using multiple cout statements results in expected for me code, while using just 1 cout results in backwards solution.
As a side note, another confusing thing to me is that pre-increment results in all values being equal. In the commented bit of code, the result is 3 3, no matter the ++ placement with respect to the *.
This code:
std::cout << "\n" << *pArr2++ << "\n";
std::cout << "\n" << *pArr2++ << "\n";
has a well defined order of modifications to pArr and will print
1
2
But this code:
std::cout << "\n" << *pArr2++ << "\n" << *pArr2++ << "\n";
invokes undefined behavior before c++17, because there are multiple modifications to pArr2 that are unsequenced. The program has UB, so it could print anything.
From c++17, there is a sequence point between the modifications, and the above code is guaranteed to print:
1
2

How can I remove values from output statement given a condition? - c++

I'm a little confused as to how to go around this. I have some array variables with some information, and I want to print them out after some calculations. If the value is 0, then I want to print a " " instead. There are 3 arrays that need to get checked however, how would I change the output statement to cater for all 3 checks and print an empty string instead of the value?
for(int start = 1; start < 13; start++)
{
if(check[start] == 1)
{
cout << checkMonth(start) << ": " << setprecision(1) << fixed << averagespeed[start] << "(" << setprecision(1) << fixed << sdSpeed[start] << ")," << setprecision(1) << fixed << averagetemp[start] << "(" << setprecision(1) << fixed << sdTemp[start] << ")," << setprecision(1) << fixed << Solar[start] << '\n';
}
/*if(sumTemp[start] == 0 || sumTemp[start] == 0 || sumSpeed[start] == 0){
}*/
}
Example Output looks like this:
January,5.5(1.2),25.5(12.2),196.4
For example if Sum of Speed is 0, that means all values of speed were 0 or null. So it should change to this:
January,,25.5(12.2),196.4
A single line to std::cout doesn't need to be done in one statement. For example:
std::cout << "First";
std::cout << ", second"
std::cout << ", third\n"
Prints the following line:
First, second, third
Now we can use an if to conditionally print the middle part of the string:
std::cout << a;
if (b != 0) {
std::cout << ", " << b;
}
std::cout << ", " << c << '\n';

Need Help Operator Overloading So I Can Display Functions

I'm stuck trying to figure out how to overload the << operator so I can display my rational class functions.
These are the lines I'm trying to overload:
cout << "Testing the compare() member function, found:" << endl;
if (r1.compare(r2) == 1)
cout << r1.display() << " is greater than " << r2.display() << endl;
else if (r1.compare(r2) == 0)
cout << r1.display() << " is equal to " << r2.display() << endl;
else if (r1.compare(r2) == -1)
cout << r1.display() << " is less than " << r2.display() << endl;
cout << "Testing the four arithmetic member functions:" << endl;
cout << "r1 + r2 = " << r1.display() << " + " << r2.display() << " = " << r1.add(r2) << endl;
cout << "r1 - r2 = " << r1.display() << " - " << r2.display() << " = " << r1.subtract(r2) << endl;
cout << "r1 * r2 = " << r1.display() << " * " << r2.display() << " = " << r1.multiply(r2) << endl;
cout << "r1 / r2 = " << r1.display() << " / " << r2.display() << " = " << r1.divide(r2) << endl;
The errors occur everytime I call a function. Here is the code for the functions:
void rational::display()
{
int gcd = GCD();
if (denom < 0)
{
num = -num;
cout << num / gcd << " / " << denom / gcd << endl;
}
else if (num == 0)
cout << num << endl;
}
rational rational::add(const rational &r2) const
{
rational sum;
sum.num = (num * r2.denom) + (r2.num * denom);
sum.denom = denom * r2.denom;
return sum;
}
The multiply, divide, and subtract functions are as same as the add they just have the symbols and variable name changed to match the operation. My overload operator is set up like this:
ostream& operator<< (ostream& out, rational &robj)
{
out << example code << example code;
return out;
}
Any help would be appreciated. This is my first time posting so if I need to post more of my code I will. Thanks!
First of all, change
ostream& operator<< (ostream& out, rational &robj)
to
ostream& operator<< (ostream& out, rational const& robj)
Then, instead of
cout << r1.display() << " is greater than " << r2.display() << endl;
use
cout << r1 << " is greater than " << r2 << endl;
Since you already have the above operator<< function, I would get rid of the member function display(). It doesn't (should not) give you any more functionality than using the operator<< function.
Given
rational 1
Use of
r.display();
should give you the same output as
cout << r << endl;
See What are the basic rules and idioms for operator overloading? for more details on operator overloading.

simple game, while loop and checking strings

I am a beginner at c++ and I want to create simple game. You have vector of strings, then you check if line input matched the right answer.
I want to generate random number 1 ,2 or 3. Then check if line matches correct answer and count the points.
I am probably missing something basic, yet I dont know what.
Problems:
Input line get correctly read on only first iterations
somehow points (tocke) jumps to 45763 after finishing.
At beginning time (cas) is sometimes 2.
Code:
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>
#include <string>
int main() {
int runde;
int tocke;
int cas;
std::cout << "\n" << "Pravila igre:" << "\n" << "Za pravilen odgovor dobis 1 tocko, za napacnega zgubis 2!"<<
"\n" << "Stevilo zivljenj si izberes sama!"<< "\n" << "\n" ;
std::cout << "Izberi stevilo zivljenj!:" << "\n";
std::cin >> runde ;
std::vector<std::string> latin = {"carum carvi", "artemisia absiinthium","coriandrum sativum"};
std::vector<std::string> slovene = {"navadna kumina", "pravi pelin", "koriander"};
tocke << 0;
cas << 0;
do {
int ind;
cas << cas + 1;
std::cout << "Round " << cas <<"! Ladies and gentlemans, buckle your seatbelts!"<<"\n" << "\n" ;
ind = std::rand() % 3;
std::cout << "ime rastline: " << slovene[ind] << "\n";
std::cin.ignore();
std::string line;
getline(std::cin, line);
std::cout << "\n";
if (latin[ind] == line){
std::cout << "Pravlino! Tocka zate!" << "\n";
tocke << tocke + 1;
std::cout << "Tocke == " << tocke << "\n" << "Zivjenja == " << runde << "\n" << "Prezivete runde == " << cas << "\n"<< "\n";
}
else
{
std::cout << "Napaka! :D" << "\n";
std::cout << "Pravilen odgovor == " << latin[ind] << "\n";
-- runde ;
tocke << tocke - 2;
std::cout << "Tocke == " << tocke << "\n" << "Zivjenja == " << runde << "\n" << "Prezivete runde == " << cas << "\n"<< "\n";
}
}while(runde >= 0 );
std::cout << "\n"<<"Stevilo tock == " << tocke <<"\n" << "St. prezivetih rund == " << cas - 1
<< "\n" ;
}
You seem to have a misconception regarding operators. << is NOT assignment, use = instead. So tocke << 0; doesn't assign 0 to tocke, it does bitshifting (on an uninitialized variable), then discards the result. tocke stays uninitialized and this causes problems later.
Instead of this:
tocke << 0;
cas << 0;
Do this:
tocke = 0;
cas = 0;
Also instead of cas << cas + 1; do cas++ and instead of tocke << tocke - 2; do tocke -= 2;. To learn how the assignment operators work, you can read about them here. Last but not least, try to see if your compiler gives you any warnings, it should complain about using uninitialized values.

String formatting (c++)

I tried to format output strings in my console application (like a table)
cout << "\n\n-----------------\n";
cout << setw(8) << left << "F\t|";
cout << setw(8) << left << "x\t|";
cout << "\n-----------------\n";
//...
cout.width(8);
cout.setf(ios::left);
cout << fixed << setprecision(3) << F << "\t|";
cout.width(8);
cout.setf(ios::left);
cout << x << "\t|";
cout << "\n-----------------\n\n";
But as result my output looks like this
What's wrong with my upper string formatting?
I used the same code as you did and got the same output until I removed the \t at the end of the line. See the new code:
cout << "\n\n-----------------\n";
cout << setw(8) << left << "F\t|";
cout << setw(8) << left << "x\t|";
cout << "\n-----------------\n";
//...
cout.width(8);
cout.setf(ios::left);
cout << fixed << setprecision(3) << F << "|";
cout.width(8);
cout.setf(ios::left);
cout << x << "|";
cout << "\n-----------------\n\n";
As already noted, it's the tabs that are causing the problem.
I would not stop at just removing the tabs though. As it stands right now, your code is highly repetitive and next to impossible to maintain. I'd do a (nearly) complete rewrite, with a couple of functions to cut down on the repetition. My first cut would probably look something like this:
// format a value in a field of specified width, followed by a separator
template <class T>
string field(T val, int w, char sep = '|') {
stringstream b;
b << setw(w) << left << fixed << setprecision(3) << val << sep;
return b.str();
}
// generate a separator for a specified number of fields,
// each of a specified width
string sep(int c, int w, char val = '-') {
string s(c * (w + 1), val);
return string("\n") + s + "\n";
}
int main() {
static const int w = 8;
double F = 1.234, x = 3.45;
string s = sep(2, w);
cout << "\n" << s;
cout << field("F", w) << field("x", w) << s;
cout << field(F, w) << field(x, w) << s;
}
Seems to me that this makes the code rather more readable and quite a bit more maintainable. For example, if we decided to display an a and b on the next line, it would seem fairly obvious to add something like:
cout << field(a, w) << field(b, w) << s;
...and we wouldn't have to look very hard to be pretty sure it was going to match up with the previous line. Likewise, if we wanted to change a column width, etc.
You may try:
cout << "\n\n-----------------\n";
cout << setw(8) << left << "F\t\t|"; // insert more tab here
cout << setw(8) << left << "x\t|";
cout << "\n-----------------\n";
//...
cout.width(8);
cout.setf(ios::left);
cout << fixed << setprecision(3) << F << "\t|";
cout.width(8);
cout.setf(ios::left);
cout << x << "\t|";
cout << "\n-----------------\n\n";
The console screen looks suspiciously Windows like.
If you are using Windows, you can use the Win32 API to format output more precisely.
In particular, you can use SetConsoleCursorPosition.
COORD position = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, position);
std::cout<<"This will be printed starting at position x, y"<<std::endl;
Try this:
#include <iostream>
#include <iomanip>
#include <map>
#include <string>
using namespace std;
int main()
{
map< float, float > table =
{
{ 8232.0f, 89.0f },
{ 8232.1f, 89.0f },
{ 8232.2f, 89.0f },
{ 8232.3f, 89.0f },
{ 8232.4f, 89.0f },
{ 8232.5f, 89.0f },
{ 8232.6f, 89.0f },
{ 8232.7f, 89.0f },
{ 8232.8f, 89.0f },
};
const size_t CELL_WIDTH = 25;
const string CELL_LINE( CELL_WIDTH, '=' );
// print the header of table
cout << '|' << CELL_LINE << '|' << CELL_LINE << '|' << endl
<< '|'
<< left << setw( CELL_WIDTH ) << "F" << '|'
<< setw( CELL_WIDTH ) << "R" << "|\n|"
<< CELL_LINE << '|' << CELL_LINE << '|' << endl;
// print the body
// change cout precision
cout << fixed << setprecision( 3 );
for ( auto it : table )
cout << "| " << setw( CELL_WIDTH - 1 ) << it.first
<< "| " << setw( CELL_WIDTH - 1 ) << it.second
<< "|\n";
// print the footer
cout << '|' << CELL_LINE << '|' << CELL_LINE << '|' << endl;
return 0;
}
this is the result: