Custom Function not being called - function

I've been having trouble calling my function in my code.
#include <iostream>
#include <cmath>
#include <cstdlib>
void bunky(int);
int main()
{
using namespace std;
bucky(20);
cin.get();
return 0;
}
void bucky(int x)
{
using namespace std;
cout << "My favorite number is " << x << endl;
}
When I try to compile this it says, "[Error]'bucky' was not declared in this scope" Any and all help would be great.

function declaration is wrong change
void bunky(int);
to
void bucky(int);

Related

i was supposed to print 5,4,3,2,1 as per the problem statement,but my c++ code is showing some error

ERROR i'm getting
F:\coding\code blocks\hello.cpp|14|error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream}' and 'void')|
my code
#include <iostream>
using namespace std;
void fun(int n)
{
if (n==0) return;
cout<<n<<endl;
fun(n-1);
}
int main()
{
cout << fun(5)<< endl;
}
Your code has two issues:
fun´s return type is void. Thus, this:
cout << fun(5) << endl;
can´t work as expected. To use the value of a void returning function invokes undefined behavior.
The printing in main of n in general is redundant as you already print it in fun().
Rather do it like:
#include <iostream>
using namespace std;
int fun(int n)
{
if (n == 0)
return;
cout << n << endl;
fun(n - 1);
}
int main(void)
{
fun(5);
return 0;
}
int the main function .
instead of writing
cout<<fun(5)<<endl;
write like this.
fun(5);
Your function fun returns void, also there is no need to use cout twice, use it either in main or in the function body fun. Try this code to get the output "as per your problem statement":
#include <iostream>
using namespace std;
int fun(int n)
{
if (n==0)
return 0;
((n-1)==0)?(cout<<n<<endl):(cout<<n<<",");
fun(n-1);
}
int main()
{
fun(5);
return 0;
}

vector not in scope c++

I get an error of 'portfolio' is not in scope for 'function.cpp'. I'm trying to call a function with a "vector of structs" passed in the parameters. But i'm assuming i'm not using porfolio correctly. How do get the size of the vector and change the elements inside of the vector? Thank you!
header.h
#include <iostream>
#include <vector>
struct Stocks {
std::string symbol;
std::string company;
int numberOfShares;
double priceOfShares;
double totalPrice;
int time;
};
void sellStock(std::vector<Stocks>& Portfolios, double*);
main.cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<Stocks>portfolio;
int main() {
sellStock(portfolio);
}
function.cpp
for(int i = 0; i < portfolio.size(); i++) {
porfolio[i].numberOfShares = portfolio[i].numberOfShares + 1;
}

getline() no instance of overloaded function c++

trying to code a cin.getline function and it throws me an error saying theres no instance of overloaded function? I'm sure im using the parameters correctly, not sure how to fix..
#include <iostream>
#include <string>
#include <ctime>
#include <string>
#include <cstring>
#include "NonPerishable.h"
#include "Item.h"
using namespace std;
namespace sict {
class NonPerishable : public Item{
fstream& save(std::fstream& file) const {
file << "N," << sku() << "," << name() << "," << cost() << "," << taxed() << "," << quantity() << endl;
return file;
}
fstream& load(std::fstream& file) {
char sku1[15];
cin.ignore(2);
cin.getline(sku1, 15, ',');
//edit:
double integer;
cin.getline(integer, 15, ',');
}
ostream& write(std::ostream& os, bool linear) const {
}
istream& read(std::istream& is) {
}
fstream& save(std::fstream& file){
}
};
}
any help would be appreciated !
Edit: i fixed it for retrieving chars, but doubles/int wont work?
The third argument is supposed to be a char, so try single quotes:
cin.getline(sku1, 15, ',');

Unresolved externals error while calling static function

This is driving me absolutely nuts. I've tried everything I've found on other posts here, and nothing works... this should NOT be this complicated.
#include <iostream>
#include <iomanip>
using namespace std;
class SavingsAccount
{
private:
float savingsBalance;
static float annualInterestRate;
public:
static void modifyInterestRate(float interestRate)
{
annualInterestRate = interestRate;
}
};
int main()
{
SavingsAccount::modifyInterestRate(.03);
cout << endl;
system("pause");
return 0;
}
Getting an unresolved externals error.

call an object's member function in std::algorithms (note: this is a different object, not in containers)

My code:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
class myClass {
public:
myClass() {
init();
}
void init() {
_myVector.push_back("Hello");
_myVector.push_back("World");
_myVector.push_back("Bye!");
for_each (_myVector.begin(), _myVector.end(), &myClass::print);
}
void print(string &myStr) {
cout << myStr << "." << endl;
}
private:
vector<string> _myVector;
};
int main() {
myClass myObj;
return 0;
}
If _myVector contained myClass objects or pointers, I could use std::mem_fun_ref or std::mem_fun. Is there any way to do the above? And yes, I do not want myClass::print to be static.
for_each (_myVector.begin(), _myVector.end(), &myClass::print);
to be replaced with
for_each (_myVector.begin(), _myVector.end(), bind1st(mem_fun(&myClass::print), this));