C/C++ :: Function Isn't Returning A Vector
Nov 11, 2014
I am writing a function to take two vectors and put them end to end in a third vector. I'm new to working with vectors, and I cannot figure out why my append function is not returning vector C. I had the function print out vector C within it to make sure the logic in the function wasn't the problem, and it worked perfectly. My code is as follows:
#include <iostream>
#include <vector>
using namespace std;
//append function to put vector b after vector a in vector c
vector <int> append(vector <int> a, vector <int> B)/>/>/> {
vector <int> c;
[code]....
and my output is as follows:
Vector A contains: 10 18 123 172
Vector B contains: 283 117 17
The two vectors back to back are:
Obviously, the third vector is not returning from the function to main properly, but why.
View 5 Replies
ADVERTISEMENT
Sep 6, 2013
Example code:
Code:
#include <iostream>
#include <vector>
using namespace std;
class A{
[Code]....
I read somewhere, that we can imagine the reference as a pointer to the vector. So, my question is:
Let's assume that instance of class A, named a, was created with new. We call a.getV() to foo and then we call the destructor of a. foo is safe? Is the copy constructor of std::vector called?
View 1 Replies
View Related
Sep 22, 2014
pop_back just returns void, so I just can't use that?
erase is okay but it doesn't return anything....
Do I use a combination of both?
View 2 Replies
View Related
Feb 20, 2015
I am making a game commonly know as the Hangman using C++.
Now I am trying to add a man in it like this:
0
|/
|
/
Now the problem i am facing is that i am using a check that if a function returns the value 0 "return 0" it means the guess is wrong and it will not update the man but if it returns any value there will be a function called which will update the man.
I just wanna know that how i am going to use the check, the kind of thing that i am trying to use is, in general words "if(function returns a value) then update the man"
int main() {
return match;
}
How are we going to use it in check that if int main is returning 'match' in the check...
View 3 Replies
View Related
Jan 22, 2014
This simple little program is not returning a value. The output is
" (string) contains characters" (The number of characters is supposed to display between the 'contains' and 'characters.'
However, if I go to the function and cout the length, the cout in the main body displays just fine.
Here's the main portion :
cout << "'" << input << "' contains "; //Output of character count.
charCount(input);
cout << " characters, including any spaces.
"; //Output of character count.
and here's the function.
int charCount(char *string) {
int length = 0; //Variable to hold the number of characters.
//Gets the number of characters contained in *string and puts that number into length.
[Code] .....
View 2 Replies
View Related
Nov 21, 2014
As the title says, i'm using a function which returns a pointer to a struct:
the struct is the following:
Code:
typedef struct POINT
{
uint16_t x;
uint16_t y;
}
Coordinate; the function i'm using:
Code:
Coordinate * Read_XTP2046(void)
{static Coordinate screen;
//calculations to determine the coordinates
screen.x=(temp[1]+temp[2])/2;
screen.y=(temp[0]+temp[2])/2;
// and so on...
return &screen;}
The question is: how do i catch this pointer and make it into a Coordinate struct in which i can read the x and y.
In my main program i would do the following:
Code:
Coordinate cor;
cor = Read_XTP2046();
This does not work, as the function returns a pointer, but how to transform this pointer into a Coordinate struct.
View 8 Replies
View Related
Mar 7, 2014
Here's a small portion of my program:
Code:
int choice(void) {
char buffer[BUFSIZ];
char answer;
printf("
[Code] .....
I am wondering which is correct to use
Code: return choice();
or
Code: choice();
return num;
View 4 Replies
View Related
Mar 31, 2013
I need to create a function which will print a list from 100Hz to 1000Hz then 1000Hz to 9000Hz. I have created a function in order to calculate and set up the frequency values from 100Hz to 9000Hz using two for loops as shown below. However I am unsure how to return this to the array at the main.
int main(void) {
double Frequency[18];
system ("PAUSE");
return(0); } double Frequency (void)
{
int count;
[Code]....
View 1 Replies
View Related
Apr 15, 2013
I am using two threads and i want to take value of a function from one thread and use it in other. I am not good at the concepts of threads. Here is the following code:
Code:
void ThreadA(void const *argument){
uint32_t status = I2S002_FAIL;
status = I2S002_Config(&I2S002_Handle0, &I2SConfig_U0C1_A);
if (status != DAVEApp_SUCCESS) {
[Code] ....
So, i want to use the return value of temp_buffer from ThreadB into Thread C and want to put this value to TXBuf in ThreadA...
View 1 Replies
View Related
Nov 14, 2013
why the function is not returning the integer 1 or 0 ... We have two arrays A and B, each of 10 integers. Write a function that tests if every element of array A is equal to its corresponding element in array B. The function is to return true (1) if all elements are equal and false (0) if at least one element is not equal.*/
#include <stdio.h>
#include <iostream>
#include <time.h>
using namespace std;
int TEST (int arrayA[], int arrayB[], int j);
int main() {
srand (time(NULL));
[code].....
View 2 Replies
View Related
Dec 17, 2014
How is the definition of the term "expression" affected by value returning functions, and why?
View 5 Replies
View Related
Nov 10, 2014
Is this considered a memory leak?
Polygon* create_square(const Vector2d& position, const RGB& colour, const std::string name) {
// Polygon, Vector2D, RPG and Vertex2d are structs with public data members only
Polygon *temp = new Polygon;
temp->name = name;
temp->colour = colour;
temp->position = position;
temp->vertices = new Vertex2d;
return temp;
}
View 1 Replies
View Related
Jun 17, 2013
I am trying to neaten my code by putting them in different cpp files but in one function an array is changed:
int function(int array2[100][9])
{
for (int i =0; i < 100; i++)
[Code]....
View 9 Replies
View Related
Feb 15, 2015
#include<iostream>
using namespace std;
int &fun() {
int x = 10;
return x;
}
int main() {
fun() = 30;
cout << fun();
return 0;
}
The code outputs 10.
Shouldn't it show an error because x is created locally on stack and gets destroyed on function return?
View 1 Replies
View Related
Mar 23, 2015
#include <stdio.h>
#include <conio.h>
#include <string.h>
[Code].....
The code above is my attempt. If I can get the above code to work
View 5 Replies
View Related
Sep 27, 2013
I have a function, which has to return some int data. In that function there are multiple return statements. There is also possibility that function may return in some cases. Will this result in undefined behavior???
View 3 Replies
View Related
Apr 14, 2014
I'm trying to solve some problems in c that i read in internet as the following example.
Make a program who receives a int number n>0 and returns how many digits of n, and the first digit of n.
View 13 Replies
View Related
Feb 9, 2015
How to output vector contents using the push_back function. My program reads in values just fine, but it does not output anything and I've been stuck on why.
here is my code:
#include <iostream>
#include <array>
#include <vector>
using namespace std;
int duplicate( vector < int > &vector1, const int value, const int counter)
[Code].....
View 3 Replies
View Related
Mar 2, 2015
I'm having trouble returning a char array by a function, here's the code. The problem is the 'reverse' function, the purpose of the function is to send two char arrays, 'newline' containing the char array, reverse it and place it in the 'rev' char array then output it back in main, however the output remains blank so I assume there must be something wrong with the reverse function.
Code:
#include <stdio.h>
#define MAXLINE 10
int fgetline(char line[], int maxline);
void copy(char to[], char from[]);
void reverse(char forw[], char rev[], int arrsize);
[Code] .....
View 1 Replies
View Related
Feb 14, 2014
I'm trying to return a short int in a function..This is my function signature :
short int* StartRecord(int seconds)
this is my array :
short int waveIn[NUMPTS];
I'm trying to get the data into an array :
short int arr [] = StartRecord(3);
getting this error: Error2error C2440: 'initializing' : cannot convert from 'short *' to 'short []'
View 1 Replies
View Related
Mar 24, 2014
I have a bool type function and set it to explicitly return false, but I am still getting true as the return.
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[]){
double coeffs[3];
double roots[2];
[code].....
View 7 Replies
View Related
Dec 12, 2013
How to use a function twice to calculate two different variables. How to have a function compute the city tax, then it uses the function a second time to compute the county tax.
My program compiles, and runs fine, however it only outputs the city tax calculation. It seems it never attempts to calculate the county tax.
One thing: All of the directives I used is what we are limited to, no more, no less. He requires a set format and will dock points of you use anything but those directives and void main().
Here is my code so far:
/* function for city tax calculation */
#include <iostream>
using namespace std;
#include <string>
#include <cstdlib>
#include <cmath>
#include <iomanip>
const double city_tax_rate=0.03;
[Code] ....
I am using Visual studio 2010.
View 9 Replies
View Related
Dec 10, 2014
I know how to pass a 2-D array to a function. The prototype for that is void f(int (*p)[2]) assuming the array is of integers and there are 2 columns in it.
However, if I wanted the same function to return a pointer to a 2-D array, what would be the prototype?
View 2 Replies
View Related
Jul 23, 2013
So I have a function like the one below
bool get_array( float input_param, vector<int>& output_param ){
// some code that handles output_param output
if( error ) return false;
}
So basiclly this function must return 2 value well one of the value is just a bool returning whether the procedure doesn't fail at some point or not.
is there any better way to write this function ?
View 7 Replies
View Related
Apr 16, 2014
this is a piece of code from my program for a game of quiz...this class is supposed to manage the marks of a player. i am passing marks for a question depending on its correctness to the setscore function and i want it to keep on updating the value of m_score so that finally i can return it from the getscore function....with this piece of code i am getting some junk values being returned by the getscore function .
class Player
{
private:
char* m_name;
int m_score;
public:
friend class Scoremanager;
[code]....
View 6 Replies
View Related
Oct 24, 2013
In my Header file:
class MyData {
struct Fparams {
int i;
bool flag;
};
}
Now in CPP file:
void MyData::GetData() {
Fparams fParam;
fparam = AllData();
[Code] ....
Now i get an error saying - error: no match for 'operator=' in fparam = AllData(); ........
View 10 Replies
View Related