C++ :: Store Function To A Variable And Call It Using That Variable?

Oct 15, 2013

I want to store few different functions to a variable for different structs/classes and then call it later using that variable, is it possible? something like

struct item {
int ID;
int special; // for function
};

item Key;
Key.special = UseKey(KEY_KING);

// now when I want to call function "UseKey(KEY_KING)" I want to use "Key.special", like this

if(iCurrRoom == ROOM_KING)
Key.special;
else if(iCurrRoom == ROOM_DRAGON)
Fireball.special;

View 5 Replies


ADVERTISEMENT

C++ :: Store A Reference Variable As Member Variable Of Interface Object

May 1, 2013

I am having trouble compiling my interface. I am trying to store a reference variable as a member variable of the interface object. Compiler says that the variable has not be initiated correctly.

LCD inherits from VisualInterface which is expecting a DisplayDriver object to be passed in (DisplayDriver is another interface, but thats not important).

I pass the displayDriver object in when LCD is instantiated in maininterfaces.zip

I was pasing it before as a pointer but was told that this could cause me problems with memory leaks and a reference was better, but now I cant seem to get it to compile.

View 11 Replies View Related

C :: Assign System Call To Variable

Feb 22, 2014

I have to ask system (linux) for a value and then assign it to the variable.

Example:

unsigned char date[] = system("date");

Of course this is wrong, but illustrated what is my goal.

How to achieve this effect ? I've heard about POSIX but how to include and use it.

View 10 Replies View Related

C :: How To Store First String Into A Variable

Feb 11, 2013

Im trying to write a program that reads in strings and decides if the 1st one is repeated. I cant figure out how to store the first string into a variable, and compare that variable to the rest of the inputted strings.

Code:

#include <strings.h>
#include <stdio.h>
int main () {
//Declared variables
int i;
}

[code]....

View 3 Replies View Related

C++ :: How To Pull Out A Value In String And Store In Variable

Oct 7, 2014

Like if the user enters "38 F" how do I take out the 38 only and store it in a variable? So far my program goes like this:

string text;
double temperature;

cout << "Enter temperature: ";
getline(cin, text); // user enters 38 F
temperature = text // store 38 from string into double

View 1 Replies View Related

C++ :: Pass Content Of A Variable (int) As Argument To Macro Call

Apr 12, 2013

I am trying to generate a couple of vectors, but the exact number of vectors that will be needed can only be determined at runtime. Therefore I had the idea to use a macro call and text substitution in order to declare the necessary number of vectors. Unfortunately, the arguments of a macro call are always (as far as I know) considered text only. So, the loop in my example below in which I am trying to generate the following code:

vector<int> vector0;
vector<int> vector1;
vector<int> vector2;
vector<int> vector3;

does not work.

#include <iostream>
#include <vector>
using namespace std;
#define declareVec(vecno) vector<int> vector##vecno;

[Code]......

I get an an error message 'vector0' was not declared in this scope.

I also tried to initialize a string or char with the content "0" and then pass this string or char to the macro call, but this did not work either.

Is there any way in which I could use the content of an integer variable or any other variable as arguments for a macro call?

View 1 Replies View Related

C++ :: How To Store Variable In Memory When Program Is Closed

Nov 3, 2013

How do you store a variable in memory so that it isn't changed when the program closes? I don't have any experience with this and am just wondering how it is possible. I am creating a program and want it to store your preferences and scores. In a simple program, everything is just reset and I don't want this for my program. How do I store a variable so that it stays the same, but can be changed even when the program is turned off?

View 2 Replies View Related

C++ :: Store 100th Fibonacci Number Into A Variable?

Feb 24, 2013

Is it even possible to store the 100th Fibonacci number (beginning with the numbers 1 and 1) into a variable? I know the number is pretty huge, and wondered if there is a data type to hold a number that big.

View 6 Replies View Related

C++ :: How To Store Entire Content Of File In Variable

Jan 28, 2013

I would like to store the entire content of a file in a single c-string variable or in a standard string class variable. Is there a function that will do this for me? I am familiar with functions that get one character or one line at a time but I don't think I've encountered a function that gets the entire file. Does this function keep or disregard the end-of-line character? If no such function exists, I would write my own function to create and return such a variable.

View 4 Replies View Related

C/C++ :: Store Initial Size Of List In Int Variable?

Nov 24, 2014

I've been trying to store the inital size of a list in an int variable, so I can access it later in case I modify the list size. For example, I did the following:

std::list<AType *> myList;
myList.push_back(anATypeobject);
int initListSize = myList.size(); //initial list size
myList.push_back(anotherATypeobject);
myList.push_back(yetanotherATypeobject);
while(myList.size > initListSize)
myList.pop_back();

What this is supposed to do is to get an initial size of a list, and then be able to return to that initial size. However, when I try to do it in my code, initListSize always change if myList.size() changes. Is there a way to change that?

View 4 Replies View Related

C++ :: Take Values From Text File And Store Them In Variable And Use For Later Calculation

Nov 8, 2013

I have a text file in the form

5502 5202.3
1523 2536.1
1254 1256.2
17846 8956.2 and so on

left one is time and right one is pressure, I need to show the time value as it is but i need to use the pressure value to calculate air speed. I don't know how to use the strings to pick up the list properly. I did the calculation formula for the air speed but i can't pick the pressure value up from the text file. here's what i did:

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

[Code].....

View 3 Replies View Related

C++ :: Read Input From User And Then Store In Structure Variable

Apr 6, 2014

My program is designed to read input from the user and then store that input in a structure variable. Whenever i use the cin.getline() function, it comes up with a semantic issue. Here is the code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int DESCRIPTION_SIZE = 100, DATE_SIZE = 20;
struct inventory {
string description[DESCRIPTION_SIZE];

[Code] .....

I left out the other functions. The error reads "no matching member function for call to 'getline'.

View 6 Replies View Related

C/C++ :: Average Integer Measuring Samples And Store This Variable

Feb 8, 2015

I need to average integer measuring samples and store this variable, so that it can`t be changed.

I need to get the initial pressure reading from my bmp085 pressure sensor, an integer value between 0 and 10000 and store the initial value after program start, so I can compare the later readings to determine whether pressure went down or up. The value to store should also be integer, rounding errors don`t matter.

View 1 Replies View Related

C++ :: Structure To Store Data From Database - Dynamic Initialization Of Variable

Jan 5, 2013

I created a class (let call it X) which contains the structure to store the data from my data base. Them I have a class (call Y) which will contain a list for each row in my data base. Third, I have a class with thousands variables (Z). What I am trying to do is to take the list of objects (Y) that contains the data to initialize Z. What I want to now if I can do something like that.

Imaging that one of my rows contain the following data:
Type Nameofvariable etc...
"static const double; MNFAIL ; 0; 0; 0,25"

In my list I have a node with contain this data

I want to use the field Nameofvariable to initialize the variable called MNFAIL contained in my class Z.

Is it possible in C++

View 3 Replies View Related

C Sharp :: Store Values In Variable Permanently Even After Restarting Machine?

Dec 26, 2013

I want to store values permanently in a variable. The same variable should be able to be edited and saved by user, and whatever it is I need the value of the variable from the last time I modified it, like a database. database because i need this to set my connection string of the database.

View 1 Replies View Related

C++ :: Store Reference To Const Object In Class As A Member Variable?

May 27, 2014

i want to store reference to a const object in my class as a member variable, as follow:

I basically want a readonly reference to |Data| in Device object.

Code:

class Device {
Device(const QList<QSharedPointer<Data>> & dataList) : _listRef(dataList) {
} protected:
const QList<QSharedPointer<Data>> & _listRef;
}

This does not allow me to initialize _listRef as something like NULL when it is not applicable.Also, i must change all my constructors and its child class to include an initialization of _listRef!!

What is the alternative? Is pointer the nearest? which of the following should be used?

Code:
const QList<QSharedPointer<Data>> * _listRef;
or
const QList<QSharedPointer<Data>> *const _listRef;
or
const QSharedPointer<QList<QSharedPointer<Data>>> _listRef; ????

View 7 Replies View Related

C :: Display Current Time And Date In Program And Use Variable To Store Each Of Values

Feb 16, 2013

i need to display the current time and date in my program and use variable to store each of the time values like

a = 10
b = 29
c = 31

printf(``%d : %d : %d``, a,b,c);

which then give the output as - 10:29:31

How to save the time in variables in C....

i am using C in windows 7 and my complier is Bloodshed Dev C++

View 7 Replies View Related

C++ :: Store Address Of Customer In Char Variable - How To Read A Line With Spaces

Oct 10, 2014

I want to store the address of a customer (with spaces) in a char variable (say cadd). First I tried to use "cin", as we know it reads until it sees any whitespace. So it reads only first word before a white space. So, I used "getline()" function, it will work. But when I used it, It did'nt wait for the I/P (it skipped it).

char cadd[20];
std::cout<<"Enter Customer Address:
";
std::cin>>cadd;
std::cout<<"Enter Customer Address:
";
std::cin.getline(cadd,20);

View 3 Replies View Related

C++ :: Creating Priority Queue Of Objects Transaction As A Variable Of Store Class

Dec 4, 2013

I've got 2 classes, Store and Transaction and I would like to create a priority queue of objects Transaction as a variable of Store class.

My store.h
#ifndef __STORE_H_INCLUDED__
#define __STORE_H_INCLUDED__
#include <queue>
using namespace std;
#include "Transaction.h"
struct TransactionCompare;

[Code] ....

The error im getting with this set up is

error C2664: 'bool TransactionCompare::operator ()(const Transaction &,const Transaction &)
const' : cannot convert parameter 1 from 'Transaction *' to 'const Transaction &'

View 6 Replies View Related

C++ :: Type Conversion - Float Or Double Variable Into Unsigned Char Variable And Back

May 10, 2013

I would like to convert a float or double variable into unsigned char variable and back.

float number_float = 23.453f;
unsigned char* number_char = (unsigned char*)malloc(sizeof(float));
number_char = reinterpret_cast<unsigned char*> (&number_float);
float* number_float_0 = reinterpret_cast<float*>(&number_char);

I am not getting the same value back.. why?

View 2 Replies View Related

C++ :: Storing Variable Size Pointers Array As Class Variable

Mar 2, 2013

This problem is best explained by looking at its output below. I stored 15 different values but when retrieved some are wrong.

#include <iostream>
using namespace std;
class A {
public:
int ** pointer;

[Code] ....

I need to store and retrieve the 15 different values correctly. How to solve this? The main thing is, I need to store the pointer in class A objects.

View 6 Replies View Related

C++ :: Write A Loop Assigning Variable X To All Positions Of String Variable

Sep 8, 2013

I have to write a loop assigning a variable x to all positions of a string variable and I'm stuck. I don't have extensive experience with arrays and I'm also a bit confused about C-String. The problem is below.

"Given the following declaration and initialization of the string variable, write a loop to assign 'X' to all positions of this string variable, keeping the length the same.

char our_string[15] = "Hi there!";

(Please note this is a 'C-string', not C++ standard string.)"

View 5 Replies View Related

C :: Transform Local Variable Into Global Variable?

Oct 25, 2014

I need to transform a local variable into a global variable so I can use it in one of my functions. I thought about passing the value as a parameter to this function but I can do this since the function is called inside the while loop and this variable counts how many times the while loop does (so the final value is outside the loop). Example to visualize better:

Code:
while(condition) {
function(parameter1, parameter2);
count = count + 1;
}
printf("%d
", count);

So, I need to transform the final value of "count" into a global variable. Can I do this?

View 5 Replies View Related

C++ :: Get Pointer That Iterator Points To And Store In Pointer Variable?

Apr 19, 2014

I'm making a system like twitter for class called ShoutOut.com I want to be able to get the PublicShoutOut pointer pointed to by the start iterator and assign it to firstShoutOutToDisplay and secondShoutOutToDisplay because I need that in order to pass the pointers to one of my functions. When I step through the debugger the values in start are all default values like "" and so are the values in this->firstShoutOutToDisplay but the message that start points to is being output just fine.

EDIT: got rid of irrelevant code. Am I using the correct syntax to do this?

if (start != finish) {
//getting these because a shoutout needs to be passed to the function that displays
//options for a shoutout
this->firstShoutoutToDisplay = (*start);

[Code] ....

View 2 Replies View Related

C/C++ :: Assign One Variable Address To Another Variable

Feb 3, 2014

I've been experimenting with pointers and am getting the below error.

'error: cannot convert 'int**' to 'int*' in assignment'

I thought it was ok to assign a variable address to another variable. Line 18 is where I get the error.

I am trying to show the progression of memory as I increment it as I have done on line 17 and again, I don't know why I don't see a progression through memory locations when output to the console on line 20.

Here's the code:
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int main() {

int *tt = new int;
int *p = new int;

[Code] .....

View 6 Replies View Related

Visual C++ :: Creating A Variable Using A Variable?

Jun 16, 2013

Any way to create a variable using a variable in the name? So E.g. if you wanted to create an int named nr(x), and x was 1, you would get an int variable named nr1? How would you do this?

View 10 Replies View Related







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