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
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.
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 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] == '-')
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
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
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 <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
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.
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;
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(){
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?
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;
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;
#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;
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 { //... }
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.
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 ?
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
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;
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.