C :: Passing Files To Function?
Jan 28, 2014
I need to pass a file as a parameter to the get_corners function, and then fill an array with an unknown quantity of data, and return the number of data to main.
Compiling this generates some messages about unused variables, which is fine for this incomplete code.
Running it just generates a blank screen. So that obviously needs to be fixed. What am I doing wrong?
I have tried moving fpin = fopen ("c: emphw12.txt","r"); to the main function, then function call to get_corners, and then closing the file after return, but same blank screen.
hw12.txt is simply:
4,0,4,7.5,7,7.5,7,3,9,0,7,0,4,0
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
FILE *fpin; // input file
[Code].....
View 5 Replies
ADVERTISEMENT
Nov 13, 2013
The game is going to be an RPG where you take turns attacking enemies. Your character will be able to attack/defend/use item/run/use magic spell. For now, we just want the bare basics - for a character to be able to do a generic attack and to be able to fight a generic enemy (so we're not worried about items and such things yet).
Our cpp files are: hero.cpp (which is actually the main file... and should be renamed to something better, admittedly), enemyStats.cpp (contains a class for enemies that lets you declare its HP/attack/etc), playerStats.cpp (see previous; but for your character) and attack.cpp, which contains the following:
int attackFunction(player myPlayer, enemy myEnemy)
{
print("Your player attacked for %d damage, bringing the wolf down to %d HP.", myPlayer.getAttack(), myEnemy.getCurrentHealth-myPlayer.getAttack());
}
We barely started, but we're already stuck on that part. I managed to make the program run just fine in terms of giving the player and enemy stats and reading it, but whenever I try to run this code in my hero.cpp (inside of main):
attackFunction(warrior, wolf);
it tells me that "enemy" has not been declared.
If I run the same code, but without any references to the wolf/enemy (so just warrior/myPlayer), it runs perfectly fine. Likewise, the program runs perfectly fine if I don't use attack.cpp and put the function directly inside of the main hero.cpp folder.
View 11 Replies
View Related
Oct 6, 2014
I have been working on code for quite some time and am able to successfully read in a text document and take certain words and information that I need. The issue is that I need to read in close to 100 plus documents and was wondering how I could read in the multiple documents. I thought about creating a structure of arrays and have each text document be an element and walk through taking each document but I am not sure how this works.
View 8 Replies
View Related
Aug 20, 2013
I want to use one median function "selectfunction" to choose one of the 2 other functions at random to pass my 2-dim array to the selected function. There is a problem in the median function
#include <iostream>
#define random(x)(rand()%x) // for random number between numbers of 0 and 1
using namespace std;
void proc1 (int iArray[][2]);
void proc2 (int iArray[][2]);
void selectfunction(int iArray[][2]);
int A[4][2] = {{1, 2} , {3, 4} , { 5, 7} , {8, 1} };
[Code]...
View 1 Replies
View Related
Jul 3, 2013
I am trying to pass function as argument to another function. My idea is to write function that can works with any type of array, and for it to be able to compare array items I'd like to use my own compareTo function. But I need to be able to pass function to use for comparing argument.
To say it short I am trying to write my own qsort that would take compareTo as one argument just like original qsort does.
Here is my code
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
template <class T>
int compareTo( T a,T b){
[code]....
and errors
1>d:my documentsvisual studio 2012projects est est est.cpp(29): error C2896: 'void DoSomething(T,int (__cdecl *)(T,T))' : cannot use function template 'int cmp(T,T)' as a function argument
1> d:my documentsvisual studio 2012projects est est est.cpp(8) : see declaration of 'cmp'
1>d:my documentsvisual studio 2012projects est est est.cpp(29): error C2784: 'void DoSomething(T,int (__cdecl *)(T,T))' : could not deduce template argument for 'T' from 'int [3]'
1> d:my documentsvisual studio 2012projects est est est.cpp(21) : see declaration of 'DoSomething'
View 2 Replies
View Related
Sep 8, 2013
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CMD_LINE 500
}
[code]....
The value that opt_rdrct_2 & inpt_rdrct_2 point to are not being passed to opt_rdrct and inpt_rdrct.
View 3 Replies
View Related
May 21, 2013
How to pass an int that I got from user input into a function to use it. I am trying to print out the words to a string of numbers.
I got the input from user.
I got an absolute value of the input.
I then separate the string into individual digits and name them.
I can print these out.
Then I started my if statement by checking if the original input was zero, and if it is, printing zero and exiting.
Then I an trying to pass the digits into a switch function and this is where I go off the rails.
Code:
#include <iostream>
#include <string>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
[Code] .....
View 7 Replies
View Related
Dec 3, 2013
I want to scan numbers in from within a function, but have access to them in main, so I tried using pointers to do so:
Code:
// Path Of Exile socket colours simulation
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
[Code].....
View 9 Replies
View Related
Jul 6, 2013
The program asks for to ask the user for how many students there are, and how many strawberries they picked. The farm gets to keep half and the students get to keep the rest.
I am using functions and passing values to one function to another. I know I am making it harder then I need to, but I want to work with functions more and passing values. I figured it was better to practice with simple programs.
Code: #include <stdio.h>
float people(float);
float strawberries(float x);
[Code].....
View 4 Replies
View Related
Jul 31, 2014
I want to pass array to function, to fill array with new values and then to print the array in the main. But I have problem because it prints me just array of zeros. Maybe the concept is wrong, I'm new with passing arrays to function.
function:
Code:
void printSum(int *return_array) {
int return_array[3];
int i;
for(i = 0; i < 3 ; i++){
return_array[i] = 5;
Code:
void printSum(int *return_array);
int main {
int m_return_array[3];
int i,j;
for(i= 0 ; i < 3 ; i++){
m_return_array[i] = 0;
} printfSum(start,m_return_array);
[Code]...
View 1 Replies
View Related
Sep 17, 2013
I need to pass a 2D array to a function. I want to know, where I may have made a mistake. This is the piece of code that I found on the web, and I am using it to allocate my 2D array.
Code:
signed char allocate2D( int** arr2D, int rows, int cols )
{
int i;
arr2D = malloc( rows*sizeof( int* ) );
if( !arr2D )
return -1;
[Code]....
My main function passes the 2D array to the function
Code: signed char read_root_message_file( int **Root_Messages, int *num ) This is how I pass the array in the main function to the above function:
Code:
int main( int argc, char *argv[] )
{
int **Root_Messages;
...
...
Root_Messages = NULL;
read_root_message_file( Root_Messages, &num_root_msg );
[Code]....
View 7 Replies
View Related
Apr 6, 2013
Would this be an example of passing an array to a function??
Code:
void wipe (int theArray[] ) {
int index;
printf("
Inside function wipe() sets each array element back to 0.
");
[code]....
View 4 Replies
View Related
Jan 19, 2014
I am following a tutorial and the topic was passing array to function so i tried to do a BMI calculator by myself. I am using code blocks to compile the codes, it is actually working while using compiler's run button. But when I open the exe file, its closing the window after entering the persons' weights and heights. Here is the code
Code:
// Passing array to function example BMI calculator of n person
#include<stdio.h>
#include<math.h>
void assess(float bmi[],int a);
int main(void){
int n,i,j;
}
[code]....
View 4 Replies
View Related
Feb 26, 2014
I am bit confused in passing the structure to a function. I made a small code and it doesn't build !
Code:
#include<stdio.h>
struct Compute_off_Time(struct WHATTIME);
typedef union _Time {
unsigned long int Value;
[Code] ....
View 1 Replies
View Related
Jan 8, 2014
I am learning arrays i want to whats the following effect of cods while passing array to the following function
void fun(array[])
void fun(array[4])
void fun(array[],4)
void fun(array[5],4)
View 3 Replies
View Related
Feb 22, 2013
Quick question... how do you pass a list in to a function...
For example forvoid reverseListwhat would the parameters be for my function... I'm trying to put everything in my case R statement in a function so I can make a neat class...
#include <iostream>
#include <list>
#include <cstdlib>
#include <conio.h>
using namespace std;
[Code] ....
View 2 Replies
View Related
Feb 13, 2013
This is supposed to check for valid input
if(((c.integer < 0)&&(c.numerator < 0))||(c.denominator<=0)) {
c.integer = 0;
c.numerator = 0;
c.denominator = 0;
}
if((c.integer>0)&&(c.numerator<0))
[Code] ....
And it does just fine, until it's passed into a function, then it doesn't work:
void validInput(Mixed a) {
if(((a.integer < 0)&&(a.numerator < 0))||(a.denominator<=0)) {
a.integer = 0;
a.numerator = 0;
a.denominator = 0;
[Code] ....
It works, when the object is passed, except for two cases (one where the minus sign shifts) and whenever there is a zero or a negative integer in the denominator.
Also, I'm passing the function like validInput(c);
View 4 Replies
View Related
Mar 29, 2014
ok I have a class Player with lots of variables and im gonna call a function to checkXp, if I call it with the whole player object does it use a lot more memory then if I just passed the couple things I need?
ex
checkXP(Player* play) // this is a whole object of player
or
checkXP(play->getXP(), play->getLVL()) // the variables I want.
I just realized I may not be able to modify anything from player in the checkXP() function
question 1: does passing the whole object use more memory
question 2: if I passed as just the variables I need, I wont be able to modify anything of object play?
View 1 Replies
View Related
Apr 18, 2014
i have defined an object which is an array of my class called Player now i want to pass this whole array as a parameter in a function, i think it will be done by using pointers but i am not able to figure out how it is done?
Player player[4];
void showscoreboard(" ", int cround){}
" " represents the blank where the array should b passed.
View 9 Replies
View Related
Nov 3, 2013
i'm trying to send a vector to a function however i am getting these errors
error: invalid initialization of non-const reference of type 'std::vector<sf::Texture>&' from an rvalue of type 'std::vector<sf::Texture> (*)(sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::T|
error: in passing argument 1 of 'int set_explosion_textures(std::vector<sf::Texture>&)'|
my relevant code is
//vector containing fighter explosion frames
std::vector<sf::Texture> f_explosion_textures(sf::Texture f_explosion_1, sf::Texture f_explosion_2, sf::Texture f_explosion_3,
[Code].....
View 7 Replies
View Related
Jan 7, 2015
gcc v.8.3 -std=gnu++11
[URL]
I'm trying to pass a function as a parameter and failing. It seems simple, until I get the error messages.
Here is the code:
class MinimalSolver {
typedef double (*func)(double sum, double char);
void driver();
[Code]....
View 2 Replies
View Related
Mar 11, 2015
I have two questions :
1)
#include <iostream>
using namespace std;
void func1() {
cout << "Func1" << endl;
[Code] ....
Why ptr_func1() does not work here?
IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type
2) How can i pass func1 to func2 as parameter?
I tried void func1(void* function), but I think I'm wrong here.
View 2 Replies
View Related
Feb 5, 2014
I'm familiar with function pointers as the method to pass a function as an argument to another function. However, I recently encountered some other syntax. I have seen in multiple times and in books,so I know it is probably not a syntax error. But I am not totally sure.
#include <iostream>
void Function1() { std::cout << "Function1"; }
void Function2(void aFunction()) { aFunction(); }
// make a param that describes the function the will be given
// as a param (return type, identifier,
[Code].....
View 4 Replies
View Related
Feb 10, 2013
I was trying to pass the following 2-dimensional array to a function called jac_inv
jac_inv(jac,3);
where jac is a 3x3 matrix
function is as follows:
void jac_inv(int *jac, int m) {
double determinant=0.0;
for(i=0;i<m;i++) {
for(j=0;j<m;j++) {
j_inv[i][j]=jac[i][j];
[Code] ....
However,I get the following error
invalid types 'int[int]' for array subscripts
Is it with regard to passing a multi dimensional array to a function?
View 4 Replies
View Related
Jan 8, 2014
i am learning arrays i want to whats the following effect of cods while passing array to the following function
void fun(array[])
void fun(array[4])
void fun(array[],4)
void fun(array[5],4)
View 7 Replies
View Related
Feb 21, 2014
I was just wondering how is this generally resolved. Let say you have this large function that runs in two modes. In the first mode it evaluates the data passed to a function as a map the the second mode it fills the map. example:
Code:
template <typename Map, typename Int>
void func(Map & map, Int i){
int z = 0;
string zz;
[Code] ....
The point is i do not want to write a large function just to include different modes so i decided to set "i" to be a mode identifier. However when i want to compile my function given two modes i get an error since the modes are not recognized (obviously). if i pass map as
Code: map<int,int>
and mode 1 i get an error here :
Code: map[z] = z; besause map
Code: map[z] expects z to be an int not string and the other way around (though in practice this cannot happen since i set the modes). So am i restricted to writing my function for both modes separately (polimorf.) or there is a way to make my example work.
View 1 Replies
View Related