C++ :: Write A Function Returning 2 Values?
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
ADVERTISEMENT
Apr 1, 2013
I have been trying to make a function that compares all the values of the array and if they are all equal will return a value to print true. The problem I am having is that regardless of what values I enter the function is always returning true. Any way to tell the program o check all the values in one command instead I put them each,
Code:
#include<stdio.h>
#include<stdlib.h>
int compare(int arrayA[], int arrayB[]);
[Code]....
View 2 Replies
View Related
Feb 25, 2015
I wrote this code for a homework assignment, everything runs fine but the function void percent_votes (line 66) isn't calculating properly, it outputs 0.00 for each value. I have tried everything I can think of to try and make it work.
Here is the assignment: Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate's name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election.
Here is the code I have written:
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
[Code].....
View 4 Replies
View Related
Dec 4, 2013
This is my code: [tag]
Code:
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
[Code] .....
View 4 Replies
View Related
Jul 13, 2013
I am currently doing a bodyfat calculator for a course in school.I'm having a problem getting an output returned from my while loop, which determines the user's gender by user input.My function (outside of main) is currently:
Code:
char gendercheck(gender){
while (true) {
gender = _getch();
if (gender == 'M' || gender == 'm') {
system("cls");
printf("Your selected gender is male.");
break ;
[code]....
My failed tries include "return gender" and "return genderpicked" statements in both the if-functions and the loop itself, which I have deleted since that didn't work. My goal is to get a character stored in gender, so that I can use the information of the user's gender in my main function from this point and onwards.
MY MAIN IS:
Code:
int main() {
char *genderpicked ;
char gender ;
genderpicked = &gender ;
[code]....
View 9 Replies
View Related
Nov 5, 2014
I am working on this program below. The problem I am having is trying to return totalDays to the main. In the numOfDays() function there is a for loop that adds the totalDays (totalDays = totalDays + days). I have just been getting errors in returning the correct amount of totalDays to the main from the numOfDays function.
#include <iostream>
using namespace std;
int numOfEmployees();
int numOfDays(int);
int main() {
int totalDays = 0;
[Code] ....
View 2 Replies
View Related
Oct 9, 2013
I would like to return multiple values from one function to access in different functions. For example:
int function1(//what goes here?)
{
int a ;
a = 1 + 2 ;
int b ;
b = 3 + 4 ;
return (what goes here if i want to return the value of a and/or b to use in another function?) ;
void function2()
{
//now i want to use the value of a here to use in an equation, how do i do that?
//now i want to use the value of b here to use in an equation, how do i do that?
}
View 4 Replies
View Related
Apr 9, 2014
I have an assignment in my C++ class that is to create a menu based coffee shop program. Here is what I have so far:
#include<iostream>
#include<cmath> //doubt this is needed but i added it just in case
using namespace std;
[Code]....
View 19 Replies
View Related
Mar 21, 2015
My code is supposed to read a five to four digit code of a resistor from a file and determine the resistor's nominal, lower and upper tolerance values. I have inputted my file's first resistor code as a string so I could run tests on it but the problem that occurs is that a get the wrong values from my switch-case statements. I have put printfs after the function call to see what the values were and they turned out wrong. If I could get my return values to be right then I could be on my way coding.
#include<stdlib.h>
#include<stdio.h>
double bandNum(char x); // function prototype that will read the resistor value for the first, second and possibly third band
double bandMult(char x); // function prototype that multiplies the resistor by some value of 10
double bandTol(char x); // function prototype that the tolerance of the resistor is multiplied by
[Code] ....
View 5 Replies
View Related
Dec 2, 2013
When i return 0; at the function int userselect (int user, int comp)my output displays twice and when i ask the user to run it again the computers selection of rock, paper or scissors remains the same it doesn't find a new random number. So i made the function userselect return to main(). This works and my output is displayed once, but when i run it it skips my while part of my do while loop completely so it loops the program without asking the user if they wish to run it again.
but when i return main() the computers choice re-randomizes every time so i need to find a way to get the while part of my do while to initialize and the program to not loop without the option of ending it.
#include <iostream>
#include <string>
#include <cmath>
[Code]....
View 1 Replies
View Related
Feb 28, 2014
Which is more efficient in functions? Returning values or using pointers to redefine variables passed as arguments?
I mean either using:
void ptr_Func(int *x)
{
*x = *x+1
}
or
int ptr_Func(int x)
{
return x + 1;
}
In terms of speed, memory use etc.I want to know general efficiency, I know it will obviously vary with different uses and circumstances.
View 7 Replies
View Related
Apr 5, 2014
After compiling, I cannot understand why my getMost and getLeast functions are returning erroneous values. I've done everything I could think of to try and fix this, Problem and code below.
A local zoo wants to keep track of how many pounds of food each of its three monkeys eats each day during a typical week. Write a program that stores this information in a twodimensional 3 * 7 array, where each row represents a different monkey and each column represents a different day of the week. The program should rst have the user input the data for each monkey. Then it should create a report that includes the following information:
* Average amount of food eaten per day by the whole family of monkeys.
* The least amount of food eaten during the week by any one monkey.
* The greatest amount of food eaten during the week by any one monkey.
Input Validation: Do not accept negative numbers for pounds of food eaten.
Code:
#include <iostream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
// Constants.
const int NUM_MONKEYS = 3;
const int NUM_DAYS = 7;
[Code] ....
View 3 Replies
View Related
Oct 11, 2014
I would like to write a program that gives you a few default values. Now you can keep these values by pressing the enter key or you can change them by just typing in new ones and confirm the new values by pressing the enter key again.
Thats as far as i got on my own but it doesn't really work. what i can do to fix this.
Code:
#include<stdio.h>
#include <conio.h>
int getInt(int min, int max);
int main () {
int a[3] = {3,4};
int b;
int code;
[Code]...
View 2 Replies
View Related
Sep 22, 2012
the output of the password text file contains like this:
username
776655443322 (password encoded)
How to write the value in last line (3rd row) of the file. after that read and write or alter 3rd row value. I need logic using "C" language.
View 3 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
Sep 22, 2012
correct the below code:
file already contains entries : 1st row username; 2nd row password.
check status required to write at third line and need to read or alter the check status value.
Currently this code is working if already there is a value for check status, then it is overwriting else UI hanging.
Code:
WriteCheckStatusToFile(BOOL& locVar) {
FILE *l_pFile = NULL;
CString l_strRememberCheck;
l_strRememberCheck = GetExePath() + _T("password");
CString sVar;
sVar.Format(_T("%d"),locVar);
[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