C++ :: Retrieving File Object From Function Then Pass To Another Function?

Jun 27, 2014

I would like to have 2 functions. (FYI, I haven't even tested these because I don't have a compiler on this PC, so don't know what they'll do. I'm also new to C++, self-teaching.)

My question is, I'm sure that oFile should be type object (of some sort), not int, but I'm not sure how to reference it correctly so that it passes from FileOpen to main to FileClose.

Code:
#include <iostream> //I/O
using namespace std;
#include <fstream> //files
using namespace ios;
int FileOpen(string fileName) {
ifstream oFile (fileName); //attempt to open file

[Code] ....

View 1 Replies


ADVERTISEMENT

C++ :: Pass Object As A Pointer In Function

Apr 12, 2013

i need to pass myboard.board (board is in the class Cboard and it is an array of int) to a function in a class called piece however this is troubling . i need to pass it as pointer os that i could change its value here under is my code.

main.cpp Code: #include<iostream>
#include"board.h"
#include "pieces.h"

[Code].....

View 7 Replies View Related

C++ :: Pass 2 Arrays Into Void Function And Return Values To One Function?

Feb 12, 2014

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.

View 6 Replies View Related

C :: How To Pass Main Function Argument To Some Other Function

Dec 26, 2014

I am writing a program in which a Fucntion has to be wriiten to parse the Command Line . When I include Code for parsing in main fuction iteslf ,its run ok . But I want to make a fucntion of that code and call it from main ,than it show Segmentation error .

By using Debugging I found Some thing is mess with " -m" Parameter of Command line , But Cant Rectify it ..

Code:
int main (int argc, char *argv[]){
//get_parameter_value(argc,argv);
// buffer[packet_size+1]= char ("'");
while (argc > 1) {
if (argv[h][0] == '-')

[Code] .....

View 3 Replies View Related

C++ :: How To Create A Function That Takes A File Object As A Reference Parameter

Feb 21, 2013

I have a school project in which need to create a function that takes a File Object as a Reference Parameter. Supposedly, it should allow me to read the first piece of data from others separated by a space from a file. The later be able to continue reading from the next piece of data.

I know how to set things up to read from the data file, such as using

Code:

#include <iostream> , and

Code:

ifstream inputFileName;
inputFile.open ("nameOfFile");

I also understand how to pass a variable by reference to a function. But I simply cannot put the two items together!

I tried using

Code:

void getFileInput (ifstream &); //parameter
getFileInput ("nameOfFile") // call
void getFileInput (ifstream &dataFileName)

In the function I used

Code:

ifstream inputFile;
inputFile.open (dataFileName);

I'm just not getting an understanding of this concept!

View 4 Replies View Related

C++ :: Pass Value From One Function To Another

Mar 30, 2014

In this program, I have to ask the user for an employee, then the program will check to see if the file for that employee exist, if it doesnt then it will automatically create the file.

ReadNew function reads the file....check to see if it exist

CreateNew function creates a new file.

In my code I have no problem with the first part of reading file.. and my createnew function works in other programs where I am asking for input of file name to create the file name. However in this code I cannot figure how to automatically pass the input filename from the ReadNew function to the CreateNew function. I can't ask the user to enter the name a second time, so I have to pass the input filename into both functions

Here is my code.

Code:
//Create a file, append to it, and read it.

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
using namespace std;
char filename[256];
string n;
string filelist;
void CreateNew(ofstream & FileNew);

[Code]...

View 3 Replies View Related

C++ :: How To Pass A String To A Function

Sep 12, 2013

How can I pass the name from the user input to the constructor ?

Code:
#include <iostream>
#include <string>
using namespace std;
class myClass {

[Code]....

View 6 Replies View Related

C++ :: Pass Array From One Function To Another

Mar 15, 2013

I'm trying to pass an array from one function to another without resorting to pointers and I have to use Pass by Reference. I can't seem to figure it out. I've been looking at many examples and just don't get it.

the function createAbc makes an array populated with the Alphabet. I need to pass that into my display function so that i can work with the array there. I don't know how to pass it in there... :(

#include "stdafx.h"
#include <iostream>
#include <cctype>

[Code]...

View 9 Replies View Related

C++ :: Pass From Function To Main

Apr 24, 2014

Why the value does not return; Here is code

#include <iostream>
#include <string>
#include <ctime> // to use the time function
#include <cstdlib>
using namespace std;
int getUserChoose (int);

[Code] ....

here is the output

Welcome to the program of Rock, Paper, Scissors
The computer is ready to play the game
Are you ready to play the game
Y for yes and N for no
Y
R = Rock; P = Paper; S = Scissors
R
You have choose Rock
1TN
1RM
0U
0C

View 5 Replies View Related

C++ :: Pass Two Strings To A Function?

Dec 3, 2014

I am writing a driver program that will eventually need to pass two strings to a function I am writing in a separate file. I am reading data from a file that is formatted like this:

ac: and
amo: love
amor: love

[Code].....

Only a few of the words are actually being processed in the way I want. It appears that only words followed by a comma are processed correctly.

View 1 Replies View Related

C++ :: How To Pass Function Return From Switch

Aug 23, 2013

How to do i fix this code im not good at using function, im trying to pass a statement from switch to my double passengersR i always come out with a error

this output:
Name passenger:
Enter number passenger:
Choose from A to D
if choose A
Total Regular Passenger: 6.19624e-312

did i do something wrong im not really good at function and im trying to learn how to pass switch to void function

Code:

#include<conio.h>
#include <iostream>
#include <Iomanip>
#include <string>
using namespace std;
double passengersR ()
{ double m;

[Code]...

View 4 Replies View Related

C :: How To Pass 2 Array Pointers To A Function

Aug 4, 2013

How would I pass let say 2 array pointers to a function X , allocate memory for them in X , fill them with values and get them back in my main function without creating a structure.

example:

Code:

void X(int *a, int*b){
a= malloc ...
b = malloc ...
// fill a and b
return them back to the main function
}
void main(){

[Code]...

View 3 Replies View Related

C++ :: Pass A Pointer Into Function - Value Of Char?

Jan 2, 2014

I'm having a problem understanding something with pointers. I was trying to pass a pointer into a function in MSVC-2013, like

char* charptr;
and then calling
myfunct(charptr);

and then inside the function i would set charptr equal to another char ptr, simply like

charptr = anothercharptr;

But this actually caused a compile failure in MSVC, saying charptr is being used without being initialized. in Code::Blocks it just gives buggy output.

I solved this issue by calling the function like

myfunct(&charptr);

and declaring the function like
myfunct(char**);

and then I had to dereference the charptr in the function when assigning it to another ptr, so
*charptr = anothercharptr;

It seems like you should be able to just pass a ptr into a function and change its address to that of another pointer? My main question is really, what is the value of a pointer? I thought the value of a pointer was just the memory address it contains. But then I had to reference it to pass it into the function.

What is the difference between the value of the char* charptr written as either charptr and &charptr?

View 1 Replies View Related

C++ :: How To Pass Char To Function Void

Jun 1, 2013

How to pass the char something[8][8] for example to function void ( char pass.....)???

View 7 Replies View Related

C++ :: Array Into Function As Parameter Pass

May 2, 2013

I have been working on this all day, and its due in like an hour and a half. I have done everything the program wants except the last part. Here is the assignment:

Write a program that inputs 10 integers from the console into an array, and removes the duplicate array elements and prints the array. You may assume that all the integers are between 0 and 100, Write at least 1 function in addition to the main function, and pass an array into that function as a parameter. e.g.

Please enter your 10 numbers: 1 2 3 4 5 6 7 8 9 10
The array contains: 1 2 3 4 5 6 7 8 9 10

Please enter your 10 numbers: 1 1 3 3 3 6 7 8 9 9
The array contains: 1 3 6 7 8 9

Please enter your 10 numbers: 1 1 1 1 1 1 1 1 1 1
The array contains: 1

The bolded part is what I cant get to work. I have tried this and it keeps telling me I have not identified the items when I have.

Here is my code:

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

[Code]....

View 1 Replies View Related

C++ :: How To Pass A Void Function As Parameter

Oct 20, 2013

How can I pass a function as a parameter? I have a class that I'm trying to reuse and one of the methods in this class need to take three parameters, two ints and a function. In other words I want to be able to call a custom function every time this method is invoked when used in other classes. The function I want to call will not return any values, its a void function.

In my Class:

void Utility::someFunction(int var1, int var2, void customFunction) {
int num1 = var1;
int num2 = var2;

[Code] .....

View 9 Replies View Related

C++ :: Pass Address Of Pointer To Function

Sep 25, 2014

#include <iostream>
using namespace std;
void myfunc(int* ); // what do i put in these parameters to accept a mem loc of a pointer
int main () {
int x = 5;

[Code] .....

SOLUTION:

#include <iostream>
using namespace std;
//Purpose to create a function that returns a pointer to a pointer
int** myfunc(int**);
int main () {
int x = 5;

[Code] ....

View 3 Replies View Related

C++ ::  How To Pass Variable To A Function In Constructor

Jun 11, 2014

In a project I am working on, I have to initialize a window and pass it as a parameter to another constructor, but before the window is initialized, it is passed as a parameter thus causing an error. Here is some code :

Game::Game()
: mWindow(sf::VideoMode(640, 480), "SFML Application", sf::Style::Close)
, mWorld(mWindow) //<---- right here is where the mWindow variable needs to be passed
{
//...
}

Is there a way to make this work???

View 2 Replies View Related

C/C++ :: How To Pass Multiple Values From One Function To Another

Mar 4, 2015

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.

View 5 Replies View Related

Visual C++ :: Pass A Handle To A Function

May 5, 2013

Visual C++ 2010

I'm using SFML with Visual C++ and need to pass a handle to a function. I've tried to find this on the web with no luck.

The handle happens to be a sprite defined as: sf::Sprite Numbers(MyNumbers);

Now I want to pass "Numbers" to a function.

-
-
getFrame(Numbers);
-
-
???? getFrame(??????) {
Numbers.SetSubRect(sf::IntRect(0,0,63,63));
return ????
}

How do I do this?

View 1 Replies View Related

C++ :: How To Pass N-dimensional Array To Function

Sep 4, 2014

If a function takes N-dimensional array as a parameter, what is generic way to do that? I couldn't figure out a nice way to do that.

View 7 Replies View Related

C :: How To Pass Symbolically Name Of Struc Member To Function

Oct 31, 2013

I have a struct with several members, for instance:

insert
Code:
typedef struct {
float u;
float v;
float w;
} mystruct;
main ()
mystruct test;
...

I have a piece of code that should operate on test.u that looks like this:

switch (j) {
case 0:
f = test.u + ...;
break;
case 1:
f = X - test.u;
break;
case 2:
f = test.u + 2;
break;
}

Now I would like to use a similar piece of code but for the other members, test.v and test.w. In principle I could copy the piece of code above and just replace test.u by test.v or test.w. Since it is the same code for all members, I would like to avoid this (copying many times the same piece of code) and replace it by a call to function. The question is, how to pass the name of the struct member I am considering to the function ? How can I tell the function, operates on member .u or .v ? Would it be possible to have a generic piece of code

insert
Code:
function (member y,...)
switch (j) {
case 0:
f = test.y + ...;
break;
case 1:
f = X - test.y;
break;
case 2:
f = test.y + 2;
break;
}

where the function could be called with function(u) or function(w) and would replace automatically .y by .u or .w ?

View 3 Replies View Related

C :: Declare A Struct Then Pass It As A Parameter Into Function

Dec 8, 2014

I have some code here where I try to declare a struct then pass it as a parameter into a function to do something to it:

Code:
struct _user {
char * initial[3];
int pos;
} user;
int initial_add (struct user * initial_list, int initials, char * buffer) {

[Code] ...

I get the error :
server2.c:15: warning: "struct user" declared inside parameter list
server2.c:15: warning: its scope is only this definition or declaration, which is probably not what you want

[Code] ....

View 6 Replies View Related

C++ :: Net Pay By Pass Referencing A Gross Pay Overloaded Function

Oct 23, 2014

I need to return taxes paid and net pay by pass referencing a gross pay overloaded function. Are the values returned from calling the overloaded function file stream objects? Can they be passed simply through a pass-by-reference function?

//Read Data from File, gather info and calculate pay, output data to file
while(counter < x) {
inFile >> last_name >> first_name >> hours_worked >> hourly_pay;
outFile << first_name << " " << last_name << " ";
outFile << calculate_gross_pay(hours_worked,hourly_pay);
counter++;
outFile<<endl;

[code].....

View 8 Replies View Related

C++ :: Pass By Reference To A Template Function Through Parameter Only

Sep 19, 2014

Due to the nature of this requirement, I've made a very minimal example, which would adequately solve my issue, without resorting to use of pointers or copy constructors.

Basically I'm trying to pass an object as a reference to the template function, rather than a copy as it's seeing. I'm needing to do this without editing Obj::Call to accommodate a reference as its first parameter, as it'd break other calls.

You'll notice in the following code the object will be destroyed upon passing, while the object defined is still in-scope due to the infinite end loop.

#include <iostream>
#include <string>
using namespace std;
class Obj {
public:
string name;

[Code] ....

In the past I tried ref(), which appeared to stop this happening, however it created a blank copy of the object instead.

View 1 Replies View Related

C++ :: Pointer - Pass Reference Variable Into Function

Oct 4, 2013

I don't understand how my code not run.

#include "stdafx.h"
#include<iostream>
using namespace std;
struct student{
char name[30];
char birthday[20];
char homeness[50];
float math;

[Code] ....

View 3 Replies View Related







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