C++ :: How To Return Multiple Values To The Main Function
Oct 13, 2013in a function how do you return multiple values to the main function.
View 4 Repliesin a function how do you return multiple values to the main function.
View 4 RepliesGet user input should be done in a function. The function should read the product number, price per unit, and quantity sold and return them to the main().
Display the value of product number, price per unit and quantyty sold in main().
The function is supposed to return value from the file in my main, but I am getting empty value. I am trying to get better with pointer. Right now just teaching myself.
right now the only way for this code to show value is when in put the putchar(*ps) inside my readfile function. I would like to readfile to return value and print in the main function.
Code:
#include <stdio.h>
char *readfile(char filename[]);
int main(int argc, char *argv[] ) {
[Code].....
If so what is the reason, and the returning int value indicates wat?
View 4 Replies View RelatedI'm trying to return a string to then call it on main function.
Code:
const char* coiso(int chave){
char str [50];
sprintf(str,"%d",chave);
char txt[50] = ".txt";
strcat(str,txt);
return str;
}
int main () {
const char* info = coiso(8);
printf("%s",info);
}
If I do a printf("%s",str) in coiso function it works but the following code doesn't work.
I want to return value from user defined function to main.
Code:
#include <stdio.h>
int mult ( int x, int y );
int add(int x, int y);
int loop (int y);
int main() {
[Code] ......
I am trying to return 2 numbers from my function to main(). They are both read in from an input file but it is not working out.
#include <fstream>
#include <iostream>
using namespace std;
ofstream outfile;
void heading(int);
int stuid(int,int);
[Code] ....
I have the main() - which has entries like
Code:
main() {
int ss, ret, z;
float yy;
ret = function1(&yy, &ss, &z);
//int function1(flat *cc, int *dd, int *k) - is it correct?
[Code]...
I'm trying to pass 2 arrays into a void funtion, and return values to one function.
this is the the program I'm working with, after I'm done I have to split it into 3 files, a header, a main, and a separate cpp file for the functions to live in.
#include <iostream>
using namespace std;
void processArrary(int numberCount[], int Numbers[], int intnumberSize, int numberCountSize);
int main() {
int Scores[26] = {76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189};
int numberCount[8] = { 0 };
[code]...
The goal of this program is to separate and count the groups of numbers then output the amount of numbers in each group. Near as I can tell, everthing should work, but I'm getting all zeros to be displayed in each group.
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?
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;
}
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.
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.
// 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] .....
I need to pass multiple values from one function to another and how to do this. Here is my code so far.
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;
//Function Prototypes//
int charString(string, string, string);
string reverseString(string, string, string);
[Code]...
I basically need to take line1, line2, and line3 and return them to the reverseString function. Also, I am not allowed to do anything like make my own classes. I have to stick to the basics and no higher level programming techniques since we have not learned them yet.
how to make subroutines and return multiple arrays.Before that, i wrote my program on matlab. could my program to be structured as following?
<header>
initial value of program;
subroutine1() {
calculating the first work;
return 2 or 3 arrays;
[code].....
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] ....
I'm writing some functions pertaining to binary trees. I've used recursion once before while learning quicksort but am still quite new and unfamiliar with it. And this is my first time touching a binary tree. So my question: In my addnode function, will the return root statement at the end ever return a value other than the value passed to the function?
Code:
#include <stdlib.h>
struct tnode
{
int data;
struct tnode * left;
struct tnode * right;
}
[code]....
I need a few pointers on how to replace the multiple "return input" in my "choice" function with a statement that perform the same action in C Programming..
Code:
/*
*FILENAME:
*PROJECT:
*PROGRAMMER: Brian Hinds
*DATE: September 05, 2014
*DESCRIPTION: This program uses a function to calculate the result of raising one number to the power of another.
[Code] ....
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] ....
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.
Ok so I am working on a game and I'm in the process of developing my Player class. Anyways, what I have is a keyboard function that is called in my main function to make a shape move.
void myKeyboardFunction(unsigned char key, int x, int y) {
switch ( key ) {
[Code].....
But when I try to call it, trying to copy my previous method,
glutKeyboardFunc(Player1.playerControls);
I get an error
error C3867: 'Player::playerControls': function call missing argument list; use '&Player::playerControls' to create a pointer to member
I get an error saying it can't convert parameters. I would just like to understand why the arguments become a problem when I make the function a member of my class, when the first method I used is so easy.
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]......
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?
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] ....
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?
}