C++ :: Functions Which Return Values

Nov 27, 2014

Write a function named cointoss that simulates the tossing of a coin.

When you call the function, it should generate a random number in the range of 1 through 2.

If the random number is 1, the function should display "heads".

If the random number is 2, the function should display "tails".

Demonstrate the function in a program that asks the user how many times the coin should be tossed, and then simulates tossing the coin that number of times.

Report the total number of heads and tails.

View 2 Replies


ADVERTISEMENT

C++ :: RValue - References As Return Values Of Functions

Aug 13, 2014

I am trying to understand RValue-references as return values of functions. First let's consider a simple function, that transforms a string into upper case letters.

const std::string
toUpper(std::string orig) {
std::transform(orig.begin(), orig.end(), orig.begin(), ::toupper);
return orig;

[Code] .....

It compiles, but I get the output 0 . Here I am wondering why the code above does not move the substr correctly while the code below does (prints out 1):

const std::string&&
no_sense(std::string abc) {
abc = abc.substr(1, 1);
return std::move(abc);

[Code] .....

In both cases abc is a temporary object inside of the function and gets deleted after the function is left. But why does the second version work and the first one does not?

cat.substr(1, 1)

And as my last question. Why doesn't

return std::move(abc.substr(1, 1));

work?

View 3 Replies View Related

C++ :: Functions Does Not Return New Value

Jul 24, 2014

In my program below, in the getage and get level functions, if an incorrect input is entered, then the correct one is entered after, it still returns the bad input back to main.

#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>

[Code] ....

View 7 Replies View Related

C :: Functions With Different Return Type?

Feb 26, 2015

understand the below program.

Why I'm getting output y is 6.000000

Code:
#include <stdio.h>
int square(double a);
int main()

[Code].....

View 6 Replies View Related

C :: Code With Two Functions Just Won't Return Correct Result

Sep 23, 2013

Each time I run it I get in correct result. I even tried running with code from from my book and it failed aswell. The code from the tutorial worked some how. BTW I use DevC++ as my compiler.

Code:

/*
Fail results
Fail results
Fail results
*/
#include <stdio.h>

[code]....

View 2 Replies View Related

C/C++ :: Updating Value Passed Through Functions With No Return Statement?

Sep 13, 2014

I am supposed to update the value temp without using a return statement, or a global variable. I have never ran across a problem like this before and I am totally stuck. I think I'm missing something really simple and need a hint. Here is the code that was provided for "fixing".

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;

[Code]....

View 2 Replies View Related

C :: Testing Array Functions - Return Sum And Difference Of Two Matrices

Nov 10, 2013

I have these arrays in a driver to use for testing:

insert Code:

#include <stdlib.h>
#include <stdio.h>
#include "TwoD.h"
int main() {
/* Initialization of LCV's */

[Code] ....

If I want to test these functions in this header:

insert Code:

#ifndef TWOD_H_INCLUDED
#define TWOD_H_INCLUDED
#define MAX_SIZE 50
/* Structure defenition for TwoD */
typedef struct

[Code] ....

I am trying to perform columnSum and rowSum, as well as twoDadd and twoDSubtract using the arrays defined in my driver. How would I do that using A and B in my driver?

View 8 Replies View Related

C++ :: Remove / Rename Functions Works But Return Non-zero Number

Jan 13, 2013

My source works perfect.The problem is that the (remove(),rename()) functions works but return nonzero number, so they fails all the time.

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <fstream>

using namespace std;
int main() {
char array[100]={"1)open-write,2)open-read,3)rename,4)delete"};

[Code] ....

View 1 Replies View Related

C++ :: Best Way To Return Values From A Function?

Jan 24, 2014

I wanted to return a string I can do

Code:

string parseFilename(string fileName){
return fileName.subtr(/*blah*/);
}

Can I also use pointers/references here though? When would you use a pointer and when just a return statement?

View 2 Replies View Related

C :: How To Return Two Values With A Pointer

Mar 6, 2015

Here is the part of my code that I need to return two values. I am working on a roulette program and I need to return the choice and the number they are betting on. How can I use a pointer to achieve this?

Code:
int makeBet(char choice, int num){

printf("
What type of bet would you like to place? ");
printf("
Type n for number.
Type e for even/odd.
Type d for dozen.

[Code] ....

View 2 Replies View Related

C++ :: Why Return Values From A Function By Ref Address

Feb 5, 2014

You can return values from functions by ref, address or value you can also do this with parameters, so what is the difference, if you have full return of a passed parameter by ref or address why would you need to ever return the function as a whole?

For ex
Code: int nValue(int& y){
y++;
}
or int& nVlaue(int y){
return y;
}

View 1 Replies View Related

C++ :: Return 3 Values From A Single Function

Jun 7, 2013

I've got the program for the most part except one part because it's basically wanting me to return 3 values from a single function and I'm unsure how to do this the way it wants me to. The rules:

Call the user-defined function to read in x in the series to be used for calculating the results. Pass a prompt for x as an input parameter, and return the validated x value to main.

After a valid x has been entered, call the same user defined function a second time, to read in y. Pass the prompt for y as an input parameter, and return the validated number of terms value to main.

After a valid y has been entered, call the same user-defined function a third time, to read in z. Pass the prompt for z as an input parameter, and return the validated z value to main.

View 2 Replies View Related

C# :: Array Doesn't Return Values

Jun 26, 2014

While working on another issue I started memory cleaning and refactoring. While refactoring I decided to create an array Resize Array Reize and Null Count:

Spoiler

public int NullCount(string[,] Original) {
try {
int returnInt =0;
for(int x =0; x<= Original.GetUpperBound(0);x++) {
if (Original[x,0]==null )
{returnInt++;}

[code]......

View 6 Replies View Related

C++ :: How To Return Multiple Values To The Main Function

Oct 13, 2013

in a function how do you return multiple values to the main function.

View 4 Replies View Related

C++ :: Vowel Test Boolean Return Values

Nov 8, 2013

I'm trying to test if a character is a vowel. I made a separate function for the test, I'm not really sure how to get my return value to output whenever I call the function from main?

Also, I'm not good with while loops and can't figure out how to get it to continue asking whether or not the user wants to keep entering values.

#include <cstdlib>
#include <iostream>
using namespace std;
bool isVowel(bool);
int main(int argc, char *argv[]) {
char var1, cont;

[Code] ....

View 2 Replies View Related

C :: Return 2 Values From A Called Function - Arguments And Parameters

Feb 2, 2013

Is there anyway we can return 2 values from a called function. Example

Code:
float xxxx(float a, float b, float c, float d)
{///
///
///
}

void xxx() {
int e,f,g,h;
////
////
xxx(e,f,g,h);
}

So if I want for example a+b and c+d, can i return those 2 answer? I don't think its possible since I am new into C programming.

View 5 Replies View Related

C/C++ :: Function To Return Lowest And Highest Values In Array

Mar 8, 2014

// I believe my loop for the functions work but the values are not returning !

// Acoustic data

#include <iostream>
#include <fstream>
using namespace std;
double low( double []);
double high( double []);
void readArrayr( double [], double , double);

[Code] .....

View 6 Replies View Related

C++ :: Passing Values To Functions?

Jul 29, 2013

I am trying to get some confirmation about how to pass to functions. If you want to assign default values to certain parameters, and have others defined inside the body of int main(), then the parameters which will have default values go at the end of the list. Is that correct?

i.e. The following code is wrong, because we cannot leave a black in the function call on the third line of the main function. However, if we switch the prototype to void Passing (int a, int c, int b = 1); and the function definition to void Passing (int a , int c, int b) everything will be okay and we can call the function as Passing (a, c).

In brief, we cannot do this EVER:
Passing( a, , c)right?
#include <iostream>
using namespace std;

[Code]......

View 4 Replies View Related

C++ :: Functions Returning Values?

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

C/C++ :: Why Won't Functions Process New Values

Nov 5, 2014

I was given an assignment where I have to input two points (four integers) on a Cartesian plane from a file and then process it using functions. My professor is very particular so the comments are a bit excessive, but here's what I have.

double radiusFn(double, double, double, double);//4 double values, one for each point. All points are needed for the calculation of distance in this function.
double diameterFn(double);//Only uses one double value - the radius. Both functions below use the same value.
double circumferenceFn(double);
double areaFn(double);
int main() {

[code].....

The file reads:

Quote

1 2 3 4 5 6 7 8 9 10 11 12

View 2 Replies View Related

C++ :: Share Values Between Functions In Same Class?

Mar 22, 2013

Code:

#ifndef CANDIES_H
#define CANDIES_H
#include <iostream>
#include <fstream>
#include <Global.h>
class Candies

[Code]...

Code:

#include "Candies.h"
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <sstream>

[Code]...

When I tested it, the file name in getName() outputted the right file name. However the file name in loadName() does not print out anything in the console window.

I want it to be so that the strings in [I]name_addTXT.c_str() in both functions are equal.

View 4 Replies View Related

C++ ::  how To Input Values Into Array Via Functions

Sep 5, 2014

What "int values" is supposed to mean as parameters to these functions? I'm not sure what do with them. Also how to input values into the array via functions. I was trying to but I just don't understand how to connect a user's input to a function to then enter into an array.

// input reads “values” integers from the user to place in the array data. It prompts the user for each value individually with the ordinal position of the value.
void input (int data [size], int values);
// Places the sum of corresponding values from arrays a and b and places the results in array s. The first “values” integers in the array are processed.
void do_sums (int a [size], int b [size], int s [size], int values);

[Code]...

View 2 Replies View Related

C++ :: How To Pass Structure Values Between Functions

Dec 29, 2012

Code:
struct NewPlayer {
int level;
int intelligence;
int damage;
};
int CharacterInfo(NewPlayer MageWizard, int Clevel,int Cint, int Cdam)

[Code] .....

View 1 Replies View Related

C++ :: Reads Process And Return Values From It - Application Crashes Because Of Buffer Size

Nov 11, 2014

I have an application that reads a process and return values from it. The problem it works fine with small processes but i have some processes that are about 1GB or even 2GB and when i try to read such big processes the application crashes. I'm trying to find a way to read the process memory in chunks of maximum 10 MB. The read code looks like:

Code:
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, entry.th32ProcessID);
unsigned char *p = NULL;
MEMORY_BASIC_INFORMATION info;
for (p = NULL; VirtualQueryEx(hProcess, p, &info, sizeof(info)) == sizeof(info); p += info.RegionSize)

[Code] ....

This reads the info.regionsize which can be as large as 100 MB. Is there any way to read it in chunks ?

View 12 Replies View Related

C :: Pass Values In Functions And Writing Them To A Txt File

Aug 23, 2013

This code is for fun, and have it doing a lot of what I want it to, just not all. I want random generated to write to a txt file. I tried to use an array but that failed. I wanted to use an array because i am only passing one value. Which makes sense since the random generated function is an int.

I made the fprintf as a comment but hopefully soon it will be able to send the values to the txt file. After that I will tackle the function.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define HIGH 49
#define LOW 1
int random_generated()

[Code]...

View 11 Replies View Related

Visual C++ :: GetMost And GetLeast Functions Returning Erroneous Values

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







Copyrights 2005-15 www.BigResource.com, All rights reserved