C++ :: Passing Value From Function To Fields Of A Structure?
Jun 22, 2013
Is there a way of passing values gotten from a function into a fields of a struct?
Lets say you got a function that returns sum and square of some numbers. How can I assign those values to the new made struct?
struct Struct
{
int sum_from_func;
int square_from_func;
};
View 1 Replies
ADVERTISEMENT
Aug 24, 2014
I am trying to wright a program that takes student grade data from a command line file, calculates a final grade, and copies the final grades to an output file. So far I have two functions, one that creates a student structure by allocating memory for it and returning its address, and one that should take that address and fill it with data from a line from the input file. My ultimate goal is to use a linked list to connect all the structs, but for now I just want to get the functions working. When I run what I have so far, I get an error C2440 (using visual 2010) that says "cannot convert from 'cStudent *', to 'cStudent', and points to the line where I call my fill function. How should structure pointers be passed?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student // Declaring student structure globally.
[Code] .....
Also, here is a sample of what a line from the input file would look like:
Bill Gates, 60, 54, 38, 62, 65, 60, 50
View 2 Replies
View Related
Feb 27, 2014
I've been able to write the program completing all requirements except for one... creating a function that accepts a nested structure array. Here's the program:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct PayInfo {
double hours; // Hours worked
double payRate; // Hourly payRate
[Code]...
I don't even know where to begin. Specifically, concerning all the aspects of the function.
View 5 Replies
View Related
Oct 21, 2014
Goal: Use a struct that has 4 fields, input to those fields, and pass to function to display.
Problem: (38) : error C2365: 'displaySData' : redefinition; previous definition was 'data variable'
Code:
#include <iostream>
#include <string>
using namespace std;
//prototypes
void displaySData(MovieData, MovieData);
[Code] .....
View 3 Replies
View Related
Oct 31, 2013
Description: Use functions and structures to simulate storage in a warehouse
*/
#include <cstdlib>
#include<iostream>
#include<cmath>
#include<iomanip>
#include<string>
using namespace std;
struct Bin {std::string name; int Quantity;}; //create a structure for "Bin"
[code].....
I keep getting a linker error on every function. what am I doing wrong?
View 2 Replies
View Related
Mar 20, 2014
What this is, is a more recent assignment and my question is if my errors are directly related to passing structure addresses to functions. I've tried several syntax variations at the beginning of my loops such as this one:
while (choice != "Q" || "q")
But the loops will not run since I introduce polar to rectangular and the choice element. My last working code was rectangular to polar and all of it worked fine.
#include<iostream>
#include<cmath>
using namespace std;
//structure declarations
struct polar
{
double distance; //distance from origin
double angle; //direction from origin
[Code] ....
View 14 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
Mar 28, 2013
I am using set in my code to get fields from a reader xml which are being errored out but it is storing it in ordered form(i.e it is internally sorting it) not in accordance to how the reader xml has the fields. What should i do so that it write the errored file in accordance to the reader xml.
View 1 Replies
View Related
Mar 28, 2013
I am using set in my code to get fields from a reader xml which are being errored out but it is storing it in ordered form(i.e it is internally sorting it) not in accordance to how the reader xml has the fields.What should i do so that it write the errored file in accordance to the reader xml.
View 8 Replies
View Related
Jul 23, 2013
I am beginner in C++ programming. And i was try use STRTOK code with NULL fields, but in ARRAY fields NULL values is skiped. For example:
input text in Memo2:
AnsiString PAT02[100][20];
for (int IndexVRadku02 = 0; IndexVRadku02 < Memo2->Lines->Count ; IndexVRadku02++) {
AnsiString PNF02 = Memo2->Lines->Strings[IndexVRadku02];
char * PN02;
[Code] ....
Result:
Array 00 01 02 03 04 05
00 0000 TEXT1 TEXT2
01 0002 TEXT3 TEXT4 TEXT5
02 0003 TEXT6
But i need this result:
Array 00 01 02 03 04 05
00 0000 TEXT1 TEXT2
01 0002 TEXT3 TEXT4 TEXT5
02 0003 TEXT6
View 2 Replies
View Related
Aug 9, 2014
Why do you use Private fields in a class if an outside class can change the private fields using get and set properties?
View 2 Replies
View Related
Sep 19, 2013
I'm trying to pass my structure to a function using switch method, so that It will ask for user's name and age then then from the switch method it will call a print function, but my coding is not working.
View 7 Replies
View Related
Nov 6, 2014
How can you actually create private fields in a C struct? I got the concept of VTable and inheriting methods aswell overriding them, but private variables can be obtained with static keyword in sourcefile. If you have variables in a struct, you can access them as soon as you got a ref to the struct. The functions are easy:
Code:
struct classA {
pointerToFunction *p;
} static void* thePrivateFoo() { }
void* publicFoo() {
thePrivateFoo(); /* or something like that */
} ...
/*in init code somewhere in the c file */ classAInstance->p = publicFoo;
But I was thinking about the variables... How is this achievable? I was thinking of a struct with only get/set functions and with no datamembers at all. All vars to be static outside the struct. But this kind of destroys the encapsulation.
View 4 Replies
View Related
Sep 16, 2013
Question: How can we write functions to access various fields?
Example: A phone number -- it has an area code, exchange & subscriber -> (123)-456-7890
How can we write a function to access the various fields of a phone number both in a particular class & in the main?
View 5 Replies
View Related
Dec 2, 2014
is it possible to define as template the following get functions.
class Config {
public:
enum { NO_ID = 999 };
struct ValueType {
bool a;
size_t b;
std::time_t c;
[code]....
View 6 Replies
View Related
Sep 26, 2014
I have made the following code to illustrate my problem:
#pragma once
#include <bitset>
#include <iostream>
[Code].....
Now I want to avoid my bitset field being constructed before the ConstructTest constructor is called. So at first I tried wrapping it in a unique_ptr but found out that this would give me some potential problems with const functions.
And then I realized I could just set it to NULL, as I have in the above code. I tried that, got unexpected print outs, until I found out that NULL is just equal to 0 in C++ and that the bitset has a = operator that takes a number, int, long or maybe something else. Either way, this effectively constructs the bitset and sets it to the number 0.
So my efforts so far have been shut down. But then how can I avoid the bitset being constructed in advance, if at all possible?
View 5 Replies
View Related
Nov 17, 2014
reading and writing 2D arrays I've been trying out a few tutorials using FileStream but I couldn't get any of them to work.
Anyway what I'm trying to do is save the playerArray to a .txt file and then read from that .txt into the fields within the GUI. This is supposed to act as a database.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
[Code]....
View 2 Replies
View Related
Mar 7, 2014
Code:
const MenuData breakfast[NUMOFBREAKFASTITEMS] = {
{"Egg Breakfast", "Two eggs with a side of bacon and two slices of toast.", "", "2 Eggs, 2 Toats, 2 Bacons", "", 250.00},
{"Pancake Breakfast", "Three Buttermilk pancakes served with butter and syrup.", "", "Three Pancakes, Butter, Syrup", "", 200.00},
[Code]....
What I'm trying to do is call the printReceipt function in the main, but I'm doing it wrong. Here is my attempt.
Code:
printReceipt (const MenuData menu[], qty, info)
I've tried it many other ways but I keep getting an error. I tried researching it on the internet, but I don't get much info.
View 14 Replies
View Related
Jan 13, 2014
Make a structure for date containing two function one for increment Date() that add to date if day increase month also increase.
2nd for validation for date like day is not greater than 30 or 31.and validate the user input before user entered value.
I have done second. but no idea for first one. Any hint for increment date.
View 4 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
Jul 14, 2014
I'm beginners in C Programming, i have a question about Structure in C. i know in Function it is possible for
1. Sending the value of argument
2. Sending the address of the argument
3. Returning values from a function by return statement
Does in Structure it is possible for
1. returning structure from a function using by return statement
View 2 Replies
View Related
Apr 2, 2013
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct employee {
char firstName[20];
char lastName[20];
float rate;
[Code] .......
View 5 Replies
View Related
Apr 4, 2013
I am having problems with my function definition of a function that should return a structure value.
This is the error I get compute.cpp(9): error C2146: syntax error : missing ';' before identifier 's_advertisebus'
The error is on the line where I start my function definition typing my function type as a structure. A long time ago in c the keyword struct is used with the structure type like struct s_advertisebus s_readadbus(). I tried it both ways but I got errors.
// struct.h
#ifndef STRUCT_H
#define STRUCT_H
struct s_advertisebus {
int nnumberofads;
float fpercentused;
[Code] ....
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
Feb 29, 2012
I'm currently working on the ioquake3 engine . The ioquake3 engine is separated into 2 different main threads at runtime: the gamecode and the engine. Both are communicating but not all information and my problem resides here.
In the gamecode, there's a struct called gentity_t which contains a lot of fields:
Code:
typedef struct gentity_s gentity_t;
struct gentity_s {
entityState_ts;// communicated by server to clients
entityShared_tr;// shared by both the server system and game
// DO NOT MODIFY ANYTHING ABOVE THIS, THE SERVER
// EXPECTS THE FIELDS IN THAT ORDER!
//================================
struct gclient_s*client;// NULL if not a client
[Code] ....
This whole entity is passed to the engine at runtime, but only the first two fields are declared for the engine:
Code:
// the server looks at a sharedEntity, which is the start of the game's gentity_t structure
typedef struct {
entityState_ts;// communicated by server to clients
entityShared_tr;// shared by both the server system and game
} sharedEntity_t;
My problem is that I need to access the health field of gentity_t from the engine. Technically, this is possible, but the health field is not declared in sharedEntity (which is the same memory address than gentity_t in the gamecode), so this is not straightforward.
I am looking for an elegant way to do this, and my constraint is that I must not edit the gamecode, only the engine.
The solutions I've thought:
- Just copy the whole gentity_t fields into sharedEntity_t. This would work I think but would be redundant, and I would like to avoid copying this huge set of fields.
- Include the two headers files declaring the gentity_t and sharedEntity_t structs, and create a Getter and a Setter functions that would cast a gentity_t over a sharedEntity_t and return/set a field. The problem is that I can't simply include them because they are both including some common headers files and this produce a recursive include error (and I can't modify the files to add a check, these are normally in the gamecode).
- Directly access the health field using a clever memory pointer, but I don't even know if that's possible given the huge number of fields prior health with many different types?
View 2 Replies
View Related
Jan 3, 2015
I am trying to create a callback system for input events in my game engine.
Here is a cut down version of the EventManager.h file
#include "Controls.h"
#include "Object.h"
enum MouseEventType{PRESSED, POINTER_AT_POSITION, PRESSED_AT_POSITION };
[Code].....
This works if the function pointer being passed to the event manager is not a member function.
I have two other classes Scene and Object that could potentially use this EventManager to create callback events. Scene and Object are both pure virtual objects. How can I pass a pointer to a member function of the child classes of both Scene and Object? I am fine with just having two separate watchEvent functions for Scene and Object but how do I pass the type of the Object or the type of the Scene? The child classes are unknown as they are being created by someone using this game engine.
For example, if I want to make a player object it would be something like
class PlayerObject : public Object{...};
Now that PlayerObject type has to find its way to PlayerObject::functionToCall(). I think I need templates to do this but, since I never used them before
This is how I intend to use this
class OtherScene : public Scene{
void p_pressed(void){
//pause
}
[Code].....
View 6 Replies
View Related