vector not in scope c++ - function

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;
}

Related

c++ newbie question: How do I cout a vector that is created in a header file?

The following function is given and lies in a header file:
// reads numbers from stdin, one number per line until it detects an empty line
// returns all read numbers in a vector
std::vector<double> read_vector() {
std::vector<double> result;
try {
for (std::string line; std::getline(std::cin, line);)
result.push_back(std::stod(line));
} catch (std::invalid_argument&) {
}
return result;
}
My task is to use this function from the header file to cout the vector.
I wrote a main function:
#include <iostream>
#include <random>
#include <string>
#include <vector>
#include "io.hh"
int main(int argc, char** argv)
{
for(int i=0; i < result.size(), i++)
{
std::cout(result[i], result[result.size()-1]);
}
return 0;}
My compiler says:
error: ‘result’ was not declared in this scope
21 | for(int i=0; i < result.size(), i++)
What are my mistakes? How do I tell my compiler that I want to use result from read_vector?

thrust iterator mix usage

#include <stdio.h>
#include <stdlib.h>
#include <cuda.h>
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/scan.h>
#include <thrust/execution_policy.h>
#include <iostream>
#include <thrust/transform.h>
struct text_functor {
text_functor() {}
__host__ __device__ int operator()(const char t) const {
if (t == '\n') return 0;
return 1;
}
};
void CountPosition1(const char *text, int *pos, int text_size)
{
thrust::transform(text, text + text_size, pos, text_functor());
}
int main() {
char s[4] = {'a', 'a', '\n', 'a'};
int r[4] = {0};
int *k;
cudaMalloc((void**) &k, sizeof(int) * 4);
CountPosition1(s, k, 4);
}
In thrust::transform, I mix host iterator s and device iterator k. This results in segmentation fault. If I change argument k to r in CountPosition1 the program will be correct.
Should all iterator in a thrust function be from same source(both host or both device)? Or is there something wrong in this code?
Yes, either all iterators should be from host containers, or all iterators should be from device containers.
Upon algorithm dispatch, thrust will dispatch either the host path or the device path. All iterators should be consistent with the dispatch method.

Creating Random Binary Generator

Hi I am trying to write a code to output a random binary sequence.
This is what I have come up with but it keeps throwing an error when I try to compile it.
Can anyone help?
#include <StdAfx.h>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{int x = rand();
if (x/2 == 0);
printf('1');
else
printf('0');
}
Remove the ; after if (x/2 == 0)
#include <StdAfx.h>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int x = rand();
if (x/2 == 0)
printf('1');
else
printf('0');
}

Custom Function not being called

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);

How do I set up a function that returns a pointer to a vector in C++?

I need help figuring out bits of code. I need a function that loads up a vector with given strings and returns a pointer. I plan to use it to generate multiple vectors and then use the pointer to display them.
I am not sure how to set up both the return of the pointer by the function and the later display of the vectors. Please make suggestions only on lines that contain //HELP NEEDED HERE
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int pointerReturner (string str1, string str2) //HELP NEEDED HERE
{
vector<string> vList;
vList.push_back(str1);
vList.push_back(str2);
return vList; //HELP NEEDED HERE
}
int main(int argc, char* argv[]) {
vector<string> vMakeList1;
vMakeList1 =pointerReturner("Honda","Toyota");//HELP NEEDED HERE
for (vector<string>::iterator n=vMakeList1.begin(); n!=vMakeList1.end();++n)
{
cout<<*n<<endl;
}
vector<string> vMakeList2;
vMakeList2=pointerReturner("Chrysler","Ford");//HELP NEEDED HERE
for (vector<string>::iterator n=vMakeList2.begin(); n!=vMakeList2.end();++n)
{
cout<<*n<<endl;
}
cin.get();
return 0;
}
You have to allocate the vector dynamically using the new operator. Otherwise the vector is just an automatic variable that is destroyed after the function pointerReturner ends. You also have to change the return type of this function from int to a pointer to a vector (vector*).
But if you allocate an object dynamically you have to destroy it explicitly using the delete operator, otherwise the allocated memory will never be released which would result in a memory leak.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string>* pointerReturner (string str1, string str2)
{
vector<string> *vList = new vector<string>();
vList->push_back(str1);
vList->push_back(str2);
return vList;
}
int main(int argc, char* argv[]) {
vector<string> *vMakeList1;
vMakeList1 = pointerReturner("Honda","Toyota");
for (vector<string>::iterator n=vMakeList1->begin(); n!=vMakeList1->end();++n)
{
cout<<*n<<endl;
}
delete vMakeList1;
vector<string> *vMakeList2;
vMakeList2=pointerReturner("Chrysler","Ford");
for (vector<string>::iterator n=vMakeList2->begin(); n!=vMakeList2->end();++n)
{
cout<<*n<<endl;
}
delete vMakeList2;
cin.get();
return 0;
}