C++ :: How To Tell Function Which Part Of Struct To Use

Jul 4, 2013

Say I have a structure defined as such:

struct data{
char c;
int x;
};

And I want to create a function to print either c or x. However, I don't want to just use a switch or something to figure this out, I want to reference which part I'm outputting.

Now here's the twist

vector<data> dataList;

I need to access element x or c in the vector, but all I pass is the vector and which element, so that I might have something like:

void (vector<data> x, DataType i){
cout << x[0].i;
}

So now, if I pass either x or c (someway), I can output one of them.

View 19 Replies


ADVERTISEMENT

Visual C++ :: Function That Can Take Out Part Of The String?

Jul 10, 2013

Is there a function that can take out part of the string such as,

C:/Users

I want to take out Users.

I need the function to search for the first "/" and take out the last dir name

C:/windows would be c:
C:/users/bla would be c/users

Im programming in visual studio 6.0 MFC

View 4 Replies View Related

C++ :: Passing Some Part Of Multidimensional Array To A Function?

Jul 31, 2013

How can I Passe some part of multi-Dimensional Array to a Function; for example only two dimension of three dimension of a an array with 3 dimension. This is because my function is defined for working with 2 dimension arrays.

View 3 Replies View Related

C :: How To Print Struct From Another Function

Oct 31, 2013

how to print struct from another function? let's say I have code this way

Code:

struct student{
int id // simple example
};
main(){
modify_data(); // calls the function.
}

[code]....

View 9 Replies View Related

C++ :: Template Function In Struct

Nov 6, 2014

I understand template functions and how to call them explicitly when no arguments are passed to them but can't seem to instantiate them explicitly when it resides within a class or struct.

struct Creator {
template <class T>
T * New(T * p) {
return new T;
}
};
Creator cr;
MyClass * pMy = cr.New<MyClass>(); //gives compile error

The compiler complains about MyClass (or any other) being used illegally in expression ...

View 6 Replies View Related

Visual C++ :: Returning Struct From Function

Oct 24, 2013

In my Header file:

class MyData {
struct Fparams {
int i;
bool flag;
};
}

Now in CPP file:

void MyData::GetData() {
Fparams fParam;
fparam = AllData();

[Code] ....

Now i get an error saying - error: no match for 'operator=' in fparam = AllData(); ........

View 10 Replies View Related

C++ :: Passing Struct Array In Function

May 5, 2013

Here's my code : [URL] .....

You should be able to enter those fields, and after some time when while loop is stopped (when u press n) it should print out that PhoneBook bellow. I don't understand what is the problem but I think it is that Display function.

View 1 Replies View Related

C :: Passing Array Of Struct To Sorting Function

Mar 6, 2015

how is this done not really sure .

Code:
//prototype
void improvedbubblesort (struct team Table[]);
//this is defined out side of main

[Code]....

the whole code is very long but can post if necessary . Basically i get no errors in this but its not sorting them . Think its in the way i pass the array of struct .

View 3 Replies View Related

C :: Function Pointer Inside A Typedef Struct

Aug 25, 2014

I want to have a function pointer inside a typedef struct but I get a segmentation fault when I run my code. Here's my code:

#include <stdio.h>
typedef struct Foo {
void (*bar)(int);
} Foo;

void func(int x) {
printf("display: %d

[Code] ....

View 2 Replies View Related

C :: How To Call A Function That Takes A STRUCT Parameter

Feb 23, 2015

I just want to call the function : outputboo(), but I dont know how

Code: /*Enthusiastic
Pessimism
desensitize
uniqueness
*/
#include <stdio.h>
#include <string.h>
struct Books{
char title[50];
char author[50];

[Code]...

View 2 Replies View Related

C :: Struct Array To Function On Header File?

Mar 13, 2014

I just can't make my code work. I need to pass a struct to a .h file. Here is my main:

Code:
#include "calculos.h"
#include "auxiliar.h"
struct dados{
int idade;

[Code].....

View 6 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++ :: Calling A Function That Holds Struct Arrays

May 16, 2013

In this program I am attempting to allow a user to input three different authors and then input three books they have written as well as the price. I am struggling with calling the functions and am not sure what to do.

#include <iostream>
#include <string>
using namespace std;
struct BookInfo{
string bookTitle;
double price;

[Code] ....

View 3 Replies View Related

C++ :: Passing Struct Arrays To A Function By Value Not By Reference?

Mar 16, 2013

I am working on incorporating a function in to an already existing piece of code, I have incorporated the function fine as far as I am aware.

The problem I have is that I am trying to pass two int arrays to the function, so that i can manipulate and compare them "the values will be changed the originals cannot be changed"

I am having trouble pulling the information out of the already created array, I am able to pass the pointer reference for the single value which is not exactly what i want "best_prog".

My function is below I have commented the memcpy parts and also the majority of the code isn't there cause it is not needed to see make the copy work.

int edit_distance(int index) {
struct prog *progp = &population[best_prog];
/* The struct of best prog not sure if i need one for the other prog I am trying to compare it with the one below doesn't work as intended.*/
//struct prog *progp = &population[];
int editdistance = 0, ar1 = 0, ar2 = 0, a = 0, b = 0, j = 0, x = 0;

[code].....

View 12 Replies View Related

C :: Linear Search Function Accessing String In A Struct?

Apr 5, 2013

I currently have a file which allows inputs to record different transistor types. I then have the task of accessing this structure, find a certain manufacturer ID, and print the information about this particular transistor.

My problem is accessing the array to search through.

Here is my code:

Code:
#include "stdio.h"
const int IDLEN=30; //All constant values defined
const int POLARITYLEN=3;
const int MAXSTOCKITEMS=10;
//First structure defined
struct TransistorRec {

[Code]......

The errors I am currently getting are on line 54 'expected primary-expression before "struct"' and on line 60 ' 'maunfacturersID' undeclared'

View 11 Replies View Related

C++ :: Return Struct Pointer From A Void Function And Print

Mar 17, 2013

i need to return a struct pointer dynamically allocated inside a function call void function() which is done using 'out parameters' in following code

struct my_struct {
int x;
} void my_function( my_struct** result ) {
my_struct* x = new my_struct{ 10 };
//...
*result = x;
}

Now i have a doubt, so if i want to print the return value from struct pointer, should i need to print it in the void function() or in the caller the function...

View 3 Replies View Related

C/C++ :: Sizeof (struct) Returns 6 More Bytes Than Actual Struct Size?

Sep 14, 2014

#include <stdio.h>
#define MAX_USERS 20
struct {
char ID[10];
char Name[40];
int Pos;

[Code] .....

I was attempting something weired with address to move data around when I discovered that the size of the array is not what I expected. I am passing this structure as &Users to a function that declares it as a void *, then I can deal with chunks of data (memmove) and not have to worry about index or things like that. However...sizeof is returning something I do not understand.

View 9 Replies View Related

C++ :: Input To Fields In A Struct And Pass Function To Display - Error C2365

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

C++ :: How To Delay Part Of Code

Jul 1, 2014

for example i have

int count = t1;
while(count>counter){
Sleep(delay);
Int32::TryParse(textBox2->Text,add);
result = result + add;
counter = counter + 1;

Problem is that sleep stops all program for specified time, Is there an alternative to sleep that would only stop part of code or can i use sleep different way to specify what it pauses?

View 8 Replies View Related

C++ :: Clear Part Of The Screen?

Nov 3, 2014

Is there any way I can clear only a selected part of the screen? (I'm aware of system("cls"))

For example, when you enter a date, and is wrong, could it just errase that input and only say "Wrong try again" without errasing everything else you where doing?

In this case, a function that only errases what is in the while

while(not wrong)
{
cout<<"DIA: ";
cin>>obj.dia;
cout<<"MES: ";
cin>>obj.mes;
cout<<"ANIO: ";
cin>>obj.anio;
}

PS: Also, is there any whay in which you can ask the user to enter the date DD/MM/YYY?

View 1 Replies View Related

C++ :: Object To Return Part Of Itself

Dec 7, 2013

Is it valid for an object to return an object of they same type? GetBlock method

class memBlock {
private:
char *mem;
unsigned long length;
public:
memBlock(char *data,unsigned long startPoint, unsigned long endPoint) {

[Code] .....

View 1 Replies View Related

C/C++ :: Cannot Find Out Why A Certain Part Is Looping

Jan 20, 2015

I am having a little bit of trouble with what should be a simple part in my code. For some reason it keeps looping the name part of the program and I seem to be passing over the problem in the code.

Here is the code:

#include <iostream>
#include <string>
using namespace std;

[Code]....

View 6 Replies View Related

C/C++ :: How To Delete Part Of The String

Nov 26, 2012

I have the following string: "type computer {dell}"

And I want to remove the {dell}

So that the output on the screen type computer

Or delete any clause between the two brackets {}

How do i do that ?

View 5 Replies View Related

C++ :: Erase Part Of The String?

Sep 29, 2014

Code:
// string::erase
#include <iostream>
#include <string>
int main () {
std::string str ("This is an example sentence.");
std::cout << str << '
';
str.erase (10,8);
std::cout << str << '
';

Code:

Output:

I don't understand this program. What do these 2 numbers represent? The index of an element in a character array? The 10th character of the array is 'n' and 8th is a "space".

View 14 Replies View Related

C :: How To Convert Part Of String And Integer

Jan 2, 2014

I'm having a problem converting part of a string to an integer.I used strtok to seperate my string and I have also a function for atoi but how to apply it to my strtok function.What i need is to change the char *years to an int.Have a look of what I got:

Code:
int main() {
char sentence[]="trade_#_2009_#_invest_#_DEALING";
char *word=strtok(sentence, "_#_");
char *year=strtok(NULL, "_#_");; // assigning NULL for previousely where it left off
char *definition=strtok(NULL,"_#_");
char *synonyms=strtok(NULL,"_#_");

[code]...

View 2 Replies View Related

C :: Strcat With Nulls As Part Of Array

Mar 30, 2014

I have a character array that includes configuration bits that are 0x00, thus when I try and do a strcat it adds the appended string into one of those spots instead of the end of the array.

View 2 Replies View Related







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