C++ :: What Happens When Reassign Variable

Apr 16, 2013

If I have an instance MyClass Object, already declared and initialized, and I write over it, Object = // different MyClass object , does the deconstructor get called on the first instance before assigning the second instance to Object?

Somewhat related (but more general), if I have a statically allocated object, what is the syntax for manually calling a deconstructor? I've never seen it before; is this even possible?

View 2 Replies


ADVERTISEMENT

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++ :: 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++ :: 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++ :: 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 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

C/C++ :: Change Enum Type Variable To String Type Variable?

Aug 10, 2014

How to change an enum type variable to a string type variable?

View 2 Replies View Related

C++ :: Difference Between Static Local Variable And Static Global Variable?

Aug 5, 2013

Here is the code,

Code:
class A {
};
A& CreateObject() {
static A a;
return a;
} static A aa;
int main() {
return 0;
}

So is there any difference between a defined in CreateObject and aa?

View 6 Replies View Related

C/C++ :: Passing Member Functions Member Variable To Another Variable

Aug 31, 2014

So I have a class object that contains the private member variable spot and the public member function MoveLock. Within MoveLock, is a member variable called numbers that holds the place where a user is on a "lock knob". Now, what I'm trying to accomplish is that whenever the user turns the "knob" in the wrong direction, the position is updated with that current numbers so that the clicks needed to unlock the first state is also updated. But I get these errors:

Error E2096 C:Users...switchtest.cpp 34: Illegal structure operation in function main()
Error E2294 C:Users...switchtest.cpp 39: Structure required on left side of . or .* in function main()

Ultimately, what I have in main() is a piece of what I'm going to implement in a class member function. I'm also thinking about moving the if else statements out of the for and creating a second one for the else portion.

#include <iostream>
#include <windows.h>
#include <iomanip>
using namespace std;
HANDLE inKeys = GetStdHandle(STD_INPUT_HANDLE);
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);

[code]....

View 10 Replies View Related

C :: Set Max For A Variable?

Nov 28, 2013

Is it possible to increment a number but have it print out at a maximum value I assign if it reaches that number? For example, if I have 99 tickets total and only 5 of those tickets are still available, and a person requests 10, how can I give them the 5 available and make sure it doesn't go over the max (99)?

Code:

if (strcmp(word2, "RAFFLE") == 0) {
fscanf(ifp, "%d", &purchased_raffle_tix[p]); //scans purchase
fscanf(ifp, "%d", &person[q]); //scans purchaser
num_raffle_sold += purchased_raffle_tix[p];
available_tix = 99 - last_raffle;
if (available_tix >= 1)

[Code] ....

It would out put with the number of the tickets that the purchaser received:

RAFFLE TICKETS 0 - 29 given to PERSON 22
RAFFLE TICKETS 30 - 69 given to PERSON 209
RAFFLE TICKETS 70 - 95 given to PERSON 123
RAFFLE TICKETS 96 - 99 given to PERSON 211
NO RAFFLE TICKETS given to PERSON 27

View 5 Replies View Related

C++ :: Why Can't Access The Variable

Jul 17, 2014

Code:
#include <iostream>
using namespace std;
void f();
extern int x;
int main() {

[Code] .....

x is declared outside the functions and defined inside main(). Then why this code produces a compile error?

x is already declared so it can be used in f(); and when I call f(), x is already defined. Then why can't f() sets the value of x (in main) to 10?

View 3 Replies View Related

C++ :: How To Check Which Variable Has No Value

Nov 12, 2013

I am writing a console program for a class. I have satisfied the assignment, but I want to clear up what is mostly a cosmetic problem. The program prints a form to the console and places the cursor at a location on the form where the user inputs data. The problem occurs when the user presses the enter key without entering data. The cursor goes to the beginning of the next line. If the user enters data after this, the program functions correctly. I want to know how I can reposition the cursor if the user enters no data.

This is the code that reads one of the values:

Code:

void getHousing(HANDLE screen, MonthlyBudget &inputBudget) {
placeCursor(screen, HOUSING_ROW, ACTUAL_COL);
cin >> inputBudget.housing;
while (!validateEntry(screen, inputBudget.housing)) {
placeCursor(screen, HOUSING_ROW, ACTUAL_COL);
cout << SEVEN_SPACES << endl;
placeCursor(screen, HOUSING_ROW, ACTUAL_COL);
cin >> inputBudget.housing;
}
}

validateEntry checks that the entered value is >= 0 SEVEN_SPACES is a string of seven spaces to cover up the previous entry.

View 3 Replies View Related

C :: Declare That A Variable Will Be Used Later?

Mar 6, 2015

I remember that the syntax exists I just don't remember what it is.I want to write a conditional function () prior to main (). in that function I'm using a variable that isn't introduced until main (). How do I let C know that I will be int'ng this function prior to the point where it is going to be accessed so that it doesn't freak out? alternatively is it possible to int the variable in both main() and my conditional function so that it exists? I imagine this would cause an issue because I would essentially be int'ng it twice.

I've done the cursory google searches however I don't think I'm using the correct keywords to get the results I'm looking for. If necessary I would be more than happy to post the code it's just a simple 40 line homework assignment.

View 6 Replies View Related

C :: Variable In While Loop

Feb 6, 2013

I'm writing a program to sum up the even numbers in between 1 and 100, using a while loop. At the beginning of my program, I initialize a variable "sum" to 0, and a variable "temp" to 1. Afterwards I enter a loop where I determine if "temp" is even or not, and if so add it to sum. However, at the end of my program, when I print "sum", I get a result of 0. Below is my code.

Code:

#include <stdio.h>
int main(void)
{
int sum = 0, temp = 1;
}

[code]...

View 4 Replies View Related

C :: Variable F Is Being Used Without Being Initialized

May 6, 2013

I have declared and initialized where needed but keep getting a Debug Error: Variable F is being used without being initialized.

Code:

#include "stdafx.h"
#include<stdio.h>
void Signboard (void)
{
/* This function prints the Signboard onto the output page */
int n;
}

[code]....

View 4 Replies View Related

C++ :: Variable Mean Is Being Used Without Being Initialized

Jan 27, 2013

#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include<cmath>
using namespace std;
int studentdetails_mean();
double standard_deviation();

[Code] .....

its showing Run-Time Check Failure #3 - The variable 'mean' is being used without being initialized.

View 11 Replies View Related

C++ :: Variable Not Storing The Value?

Feb 5, 2014

So i'm creating bank system. Where I have function that reads the text file (balance), and I make a deposit, which adds what was in the balance = deposit + balance.

But this is what is happening. In my text file i have number 10. Function balance works fine and reads 10 from the text file.

When I make a deposit add a value to the balance, if i add 7, the new balance is 17. And i check the text file shows me is 17. Which is correct.

The problem is here. If i make another deposit, without closing the program, for example i add 5 to the balance, the new balance should be 22 = 17(balance) + 5(deposit), because 17 was store on the text file and it was the balance that was store on the text file on the last time. But it shows me 15, it adds the balance that the program first started which was 10(balance) + 5(deposit), but should had had be 17 + 5.

When I close the program the value on the text file, is 15, that was the last sum that i did.

int main()
{
double balance = 0;
balance = currentBalance(balance);
menu(balance);

[Code].....

View 18 Replies View Related

C++ :: Variable Xxx Is Not A Type Name

Feb 8, 2015

I'm attempting to pass a couple of variables over to my Item.cpp class, that is description and item_price. However under item.set(description,item_price), i get three errors. One under the . (period) saying : expected an identifier and two more under description and item_price stating that variable " xxx " is not a type name.

Main.cpp

#include <iostream>
#include "item.h"
using namespace std;
using namespace items;
int main(){
int n;

[Code] .....

View 11 Replies View Related

C++ :: Using Variable In Pow Function?

Oct 15, 2014

I'm trying to figure out what is wrong with this:

static_cast<int>((pow(x, counter+1)) % 10)

it says "more than one instance of overloaded function "pow" matches the argument list"

View 5 Replies View Related

C++ :: How To Get The Value Of Variable From Equation

Mar 25, 2013

how to take two variables with values such as a 1; and b 2;. Now you take cout << a + b; and you will get 3, but I want to somehow have a 1; b 2; and c num; then have a + b = c. So what ever I have the user input is for a and be added together will become the value of the c variable.

View 1 Replies View Related

C++ :: Get Value From String Containing Variable Name?

Nov 2, 2014

As written in the title, I want to be able to extract a variable value from a string containing the variable's name. I know one can use associative containers such as maps but is there another more direct way?

E.g.:

int variable = 5;
string str = "variable";

// how do I get the value of 5 out of the string containing the variable name?

If I am correct, this is called 'Reflection', correct me if I'm wrong.

I know C++ has no inbuilt 'Reflection' class or anything like that so I was wondering if there is a workaround for this kind of thing or is there a library out there which can do this? (that's if I have the name right).

I have found a library called Boost Reflection which sounds like it could do this but I just wanted to make sure that reflection is actually what I am talking about and whether C++ can do what I'm trying to do? I'm not sure how.

View 5 Replies View Related

C++ ::  How To Know If Variable Has Been Accessed

Jul 21, 2014

How to tell whether a variable has been accessed in memory, no matter what it's been used for... Whether it's actually been set to something else, or whether it's been get for an operation or function call that doesn't actually do anything to affect the variable itself. Either way the variable has been "accessed" for something.

I'm not entirely certain that it's possible to detect a program getting the variable but I know that programs exist where they can trace what has accessed a certain part of memory... CheatEngine is one example, although I'm not entirely sure whether this can only detect changes in the variable and then trace what did it

View 4 Replies View Related

C++ :: Cannot Dereference Variable In A Set

Jun 29, 2014

I get errors C2100 (illegal indirection) and C2088 ("<<": illegal for class) while trying to use the myPrint function (irrespectively of whether the set for printing contains pointers or not):

template <class T = std::string>
void myPrint(const std::set<T>& mySet) {
std::cout << "Size of vector is " << mySet.size() << ", Pointers: ";
bool pp = false; std::string str = "no";

[Code] ....

The errors point to the line "if (pp) { std::cout << *(*i) << ","; }", but I cannot figure out what's wrong with it...

View 4 Replies View Related







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