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

When I run this code, I get the following error
Screenshot erore
50:7: error: no match for 'operator<<' (operand types are 'std::__ndk1::ostream' {aka 'std::__ndk1::basic_ostream'} and 'void')
cout << generateRandomNumber();
compilation terminated due to -Wfatal-errors.
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
// class game
class mathGames
{
public:
mathGames();
void generateRandomNumber();
void calculate();
void show();
void answer();
private:
int x;
int y;
int z;
char ans;
};
// constructor
mathGames::mathGames()
{
x = 0;
y = 0;
}
// generate random numbers
void mathGames::generateRandomNumber()
{
srand (time(NULL));
x = rand()%9+1;
y = rand()%9+1;
}
// calculate numbers
void mathGames::calculate()
{
z = x + y;
}
// show generate number
void mathGames::show()
{
cout << " " << x << " + "
<< y << " = " << z << endl;
}
// user answer
void mathGames::answer()
{
cout << " true or false (t/f) ? ";
cin >> ans;
if (ans == 't')
cout <<
generateRandomNumber();
}
// main
int main ()
{
mathGames number;
number.generateRandomNumber();
number.calculate();
number.show();
number.answer();
}

That error it is showed becouse the function don't return anything (it is a void) and so it is impossible to print something.
Also if you want to store somewhere this random values you can pass to the function by address two argouments, in this way you can store the results here and don't lose them after you exit that scope.

Your function is returning void. Therefore you cannot print anything.

Related

CPP program displays shadows the perimeter error [duplicate]

I am trying to make a function that returns double the integer number that I will pass to it. I am getting the following error message with my code:
declaration of 'int x' shadows a parameter int x; "
Here is my code:
#include <iostream>
int doublenumber();
using namespace std;
int doublenumber(int x)// <-- this is the function which returns double the value .
{
int x;
return 2 * x;
cout << endl;
}
int main()
{
int a;
cout << "Enter the number that you want to double it : " << endl;
cin >> a;
doublenumber(a);
return 0;
}
You have x as a parameter and then try to declare it also as a local variable, which is what the complaint about "shadowing" refers to.
I did it because your advice was so helpful, and this is the final result :
#include <iostream>
using namespace std;
int doublenumber(int x)
{
return 2*x;
}
int main()
{
int a;
cout << "Enter the number that you want to double it : " << endl;
cin>>a;
int n= doublenumber(a);
cout << "the double value is : " << n << endl;
return 0;
}
#include <iostream>
using namespace std;
int doublenumber(int x)
{
return 2*x;
}
int main()
{
int a;
cout << "Enter the number that you want to double it : " << endl;
cin>>a;
int d = doublenumber(a);
cout << "Double : " << d << endl;
return 0;
}
There are some problem with your code. Your declaration and definition of function dies not match. So remove declaration as no necessity of it.
You are declaring local x variable inside function which will shadow your function arguments.

Loop and object intialization question (C++)

I'm trying to use a loop to infinitely create objects from a class unless a specific input is entered. I think I have everything done except the loop. I know how to initialize one object in my main function, but I'm stuck as to how to use a loop to do this infinitely. My code is below.
Driver File:
#include <iostream>
#include "square.h"
using namespace std;
int main()
{
square square1;
square1.setSide();
square1.calcArea();
square1.calcPerimeter();
square1.showData();
}
Header file:
#pragma once
#include <string>
class square
{
public:
square();
void setSide();
double getSide() const;
void calcPerimeter();
void calcArea();
void showData();
private:
double squareSide;
double squarePerimeter;
double squareArea;
};
Implementation file:
#include <iostream>
#include <iomanip>
#include "square.h"
square::square()
{
squareSide = 0;
squarePerimeter = 0;
squareArea = 0;
}
void square::setSide()
{
std::cout << "Enter a side length: ";
std::cin >> squareSide;
std::cout << "\n";
if (squareSide == -1)
{
std::cout << "Exiting program.\n";
exit(-1);
}
while (std::cin.fail() || squareSide < 0)
{
std::cout << "\nYou must enter a positive number. Please try again.\n\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Enter a side length: ";
std::cin >> squareSide;
}
}
double square::getSide() const
{
return squareSide;
}
void square::calcPerimeter()
{
squarePerimeter = 4 * getSide();
}
void square::calcArea()
{
squareArea = getSide() * getSide();
}
void square::showData()
{
std::cout << "The side length of the square is: " << getSide() << "\n";
std::cout << "The perimeter of the square is: " << getSide() * 4 << "\n";
std::cout << "The area of the square is: " << getSide() * getSide() << "\n";
}
You could add a do ... while loop around your code in main and store the squares in a vector<square>.
Example:
#include <limits> // you use numeric_limits from this header
#include <utility> // move
#include <vector> // vector
int main() {
std::vector<square> squares;
std::string answer;
do {
square square1;
square1.setSide();
square1.calcArea();
square1.calcPerimeter();
// move the square into the vector
squares.push_back(std::move(square1));
// ask the user if he/she wants to enter another
std::cout << "Go again? ";
} while(std::cin >> answer && answer == "yes");
// display what you stored
std::cout << "You stored " << squares.size() << " square(s)\n";
for(square& sq : squares) {
sq.showData();
std::cout << '\n';
}
}

Is it possible to change a generic variable into a specific one, or initialize a variable of a specific type with the value of a generic variable?

#include <iostream>
#include <typeinfo>
#include <string>
#include <cstdlib>
using namespace std;
template <typename First>
class VerifyIfTrue{
protected:
First AG;
int tries = 0;
int vaalue;
int RT;
string whatis;
string lie1;
string lie2;
VerifyIfTrue(string twhatis, string tlie1, string tlie2) : whatis(twhatis), lie1(tlie1), lie2(tlie2)
{input();}
void input(){
if(tries == 0){
cout << "Tell me your " << whatis << "\n";}else{
cout << "Come on, what's your " << whatis << "\n";}
cin >> AG;
if(typeid(AG).name() != typeid(int).name())
{
int x = 0;
AG = x;
cout << "Wrong type" << endl;
++tries;
input();
}else{
int a = AG;
positivetest(a);
}
void positivetest(int RT){
if(RT <=0)
{
cout << lie1 << "\n";
if(tries == 0)
{
++tries;
cout << lie2 << "\n";
}
int y = 0;
AG = y;
++tries;
input();
}else{
vaalue = AG;
}
}
}
...
I get the following error:
cannot convert 'std::__cxx11::basic_string' to 'int' in initialization|
Is there a way around this? I want to either be able to change AG into an int, so it can be passed into int parameters or tested if it's an int; or be able to assign its' value to an int
You might use if constexpr (c++17) with std::is_same:
if constexpr (!std::is_same_v<int, First>) {
int x = 0;
AG = x;
cout << "Wrong type" << endl;
++tries;
input();
} else {
int a = AG;
positivetest(a);
}

simplify my code in C++ below

I want to create a program which is able to calculate the surface area, volume, and circumference. for your additional info, I am studying about function, I has just learned about C++ about a week.
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int getPostP(string msgP)
{
int Ppost= 0.000;
do
{
cout << msgP << endl;
cin >> Ppost;
return Ppost;
} while(Ppost<= 0);
}
int getPostL(string msgL)
{
int Lpost= 0.000;
do
{
cout << msgL << endl;
cin >> Lpost;
return Lpost;
} while(Lpost<= 0);
}
int getPostT(string msgT)
{
int Tpost = 0.000;
do
{
cout << msgT << endl;
cin >> Tpost;
return Tpost;
} while(Tpost <= 0);
}
int surfaceArea(int Psur, int Lsur, int Tsur)
{
return (2*Psur*Lsur)+(2*Psur*Tsur)+(2*Lsur*Tsur);
}
int volume(int Pvol, int Lvol, int Tvol)
{
return (Pvol*Lvol*Tvol);
}
float circumference(int Pcir, int Lcir, int Tcir)
{
return 4*(Pcir+Lcir+Tcir);
}
int main()
{
int P = getPostP("enter the P of your block");
int L = getPostL("enter the L of your block");
int T = getPostT("enter the T of your block");
float surfAreaBlock = surfaceArea(P, L, T);
float volBlock = volume(P, L, T);
float cirBlock = circumference(P, L, T);
cout << "block which have P = " << P << " and L = " << L << " and T = "<< T << " have surface area = " <<
surfAreaBlock << " and volume = " << volBlock << " and cirBlock = " << cirBlock;
return 0;
}
Maybe one of you want to rewrite and add some comment, which parts are able to simplify, so I can understand easier.
First of all, it looks like you should make all of your integer inputs into double instead of int, since it's expected that your inputs won't necessarily be an exact integer amount (probably). Also you can get rid of all of your duplicate functions for entering the parameters. Change it to a single function and call that one for each variable.
double getInput(const std::string& prompt)
{
double input(0.0);
do
{
std::cout << prompt << "\n-> " << std::flush;
// forces input to be a double type
while (!(std::cin >> input))
{
std::cout << "\n-> " << std::flush;
std::cin.clear();
std::cin.ignore(256, '\n'); ///< could use streamsize::max here
}
} while (input <= 0.0); ///< make sure it's positive
return input;
}

why is getline() not working in my little experiment?

//////////////////////////////////////////////////////////////////
// Capture a lambda with a lambda and use it in the lambda it's //
// captured in along with some code in the lambda that captures //
// it. //
// //
// Add to that to capture a variable in the client and use that //
// too. //
// //
// Then make a lambda that captures a class object and calls //
// some method or methods with it, optionally modifies the re- //
// sult... //
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
// Note: std::function<type(type)> f; //
// f = <define lambda here> //
// f() //calls lambda //
//////////////////////////////////////////////////////////////////
#include <functional>
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::function;
using std::getline;
using std::string;
class Experimental {
private:
int x;
string s;
public:
Experimental() {}
~Experimental() {}
void set_x(int new_x);
int get_x();
void set_s(string s_in);
string get_s();
};
void Experimental::set_x(int new_x) {
x = new_x;
}
int Experimental::get_x() {
return (x);
}
void Experimental::set_s(string s_in) {
s = s_in;
}
string Experimental::get_s() {
return s;
}
int main() {
double n;
string input;
Experimental* experiment = new Experimental();
cout << "Enter a number: ";
cin >> n;
function<double(double)> f;
f = [&f](double k) {
return (k ? k * f(k-1) : 1);
};
function<double(double)> g;
g = [&f,n](double m) {
return (f(n)/n);
};
function<int()> T1; //capture a class and do stuff...
T1 = [&experiment,n]() {
experiment->set_x(13 + n);
int m = experiment->get_x();
return (m);
};
function<string(string)> T2; //capture a class and do stuff...
T2 = [&experiment](const string in) {
experiment->set_s(in);
string s = experiment->get_s();
return (s);
};
cout << "The factorial of " << n << " is: ";
cout << f(n) << endl;
cout << "The factorial of " << n << " divided by " << n << " is: ";
cout << g(n) << endl;
cout << "The new value of x in experiment is: ";
cout << T1() << endl;
cout << "Enter a string: ";
getline(cin, input); //FIXME
cout << "input is: " << input << "<-" << endl;
cout << "The new string in experiment is: ";
cout << T2(input) << endl;
delete experiment;
return (0);
}
I know it's ugly. Not reeally using lambdas as they're meant to be used while I experiment here at first. For some reason I'm not getting input for my string variable and I don't know why. Can someone help/ point out what the problem is?
The problem is your getline is reading the end-of-line left in the buffer after:
cin >> n;
Fix that by using ignore:
#include <limits>
...
cin >> n;
...
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
getline(cin, input);
This is a very common error, basically you have
cin >> number;
getline(cin, str);
Now think about this (you seem a good programmer so I'm only giving you a clue). How many newlines are contained in a number? How many newlines will cin >> number; actually read?