C++ :: Change Data Of Constant Variable

Dec 9, 2014

I'm using const_cast to change the data of the constant variable. but on next while i'm trying to print the value its showing old data stored.

#include <iostream>
using namespace std;
int main() {
const int a = 5;
const_cast<int &>(a) = 6;
cout << "Hello World " << a << endl;
return 0;
}

Output : Hello World 5

why the value is getting changed again.?

View 4 Replies


ADVERTISEMENT

C++ :: Static Constant Member Variable

Jun 6, 2013

What is the problem with the following code is? It compiles with Visual C++ 2012 but does not with g++:

//a.h

#ifndef Loaded
#define Loaded
using namespace std;
class MyClass{
public:
static const int MyStaticValue = 200;

[Code] ....

If I try to compile this using the command

g++ a.cpp b.cpp

I get an "undefined reference to 'MyClass::MyStaticValue'" error for the line "A = MyClass::MyStaticValue;" in main(). The strange thing is that if I change the line to "A = (int) MyClass::MyStaticValue;" it works fine and the output is

200
200

as expected.

The code also compiles under g++ if I move the defintion of MyStaticValue from a.h to a.cpp by const int MyClass::MyStaticValue = 200;

View 5 Replies View Related

C++ :: Defining Constant And Creating Double Variable With Initial Value?

Apr 4, 2014

Define a constant PI that has a value of 3.14159

- Create a double variable, Radius with an initial value of 10

- Create two double variables. Circum and Area, without initialization

- Using the following formulas, compute circumference and area of the circle:

circumference = pi * r * 2 (here, r means radius)

area = pi * r * r

- Display the result using three variables (numbers must come from variables)

- Expected output: (Don’t forget to display the period at the end of the first line)

Circle with radius of 10.

Circumference = 62.8318 Area = 314.159

View 3 Replies View Related

C++ :: Data Pointed By Ptr2 Must Be Constant

Jan 8, 2014

I have defined a pointer ptr2 as follows:

const void * ptr2 ( new A(20) );

In this case I expect the data pointed by ptr2 must be constant. But when running the program, the data could still be changed successfully. Why ?

Below is the program:

#include <iostream>
using namespace std;

[URL] ....

class A {
public:
A( int ID ) {
cout << "--Constructor with ID=" << ID << endl;
this->ID = ID;

[Code] .....

The output:
--Constructor with ID=20
ID of ptr2 is 20
ID of ptr2 is 21 after changed!
-~Destructor with ID=21
End of main()

Process returned 0 (0x0) execution time : 0.002 s
Press ENTER to continue.

Question: What is the significance of "const" in const void * ?

View 19 Replies View Related

C++ :: Constant Data Member Initialization

Apr 9, 2014

Here's a part of my program. What I need to know is how I can pass an argument to the Book constructor so I can change the const data member Category (with cascading capacity if possible. I also posted some of my set functions for further comprehension.

class Book {

friend void CompPrice(Book &,Book&);
//friend function that has access to the member functions of this class
//The arguments sent to it are by address, and of type the class Book, so that it can have access to its member functions
private:
//private data members

[Code]...

View 1 Replies View Related

C++ :: Defining A Constant Data Member?

Dec 21, 2012

Programe #1
// file.h
class File {
public:
static const int var = 9;
};

[Code]....

Program#1 is running fine, but program#2 gives linker error:

error LNK2005: "int GlobalVar" (?x@@3HA) already defined in file.obj

I know the header files are never compiled. Then in the above case, how the compiler knows the definition of variable var, but not able to find the definition of GlobalVar? What is the difference between this two programs?

View 3 Replies View Related

C++ :: Reading Data (Constant Numbers) From A File

Feb 24, 2015

How do I get c++ to read a file containing six numbers - a1, b1, a2, b2, a3, and b3 - that are constants for the following equations:

clubangle(degrees) = a1 + b1*0.85*clubnumber

clublength(inches) = a2 + b2*1.05*clubnumber

clubspeed(yards/s) = 1.1 * (a3 + b3 * swingnumber) * (clublength(inches)/40)^2;

View 5 Replies View Related

C++ :: Storing Constant - Frequently Accessed Data

Sep 9, 2013

I'm redesigning some code and I'm wondering what the best ways to store and access certain data is. The characteristics are as followed:

1) Based on data from a file, a distance matrix (N x N) is calculated.
2) Once the matrix is complete, the data will never change (unless a new file is read, but I can work around that by iteratively calling the problem with a new datafile on as command line parameter).
3) The data from the matrix is accessed billions of times from pretty much every other line of code.

In my old version, I had a class "Data" which a sub-class "Data::Distance" and I would put a reference in every other class that needed it. Now, my class hierarchy will be much flatter (basically all logic will be in one class; other classes will be POD structs).

Given the characteristics of the Distance table, is there a way to store them in a very efficiently-accessible way? Does it matter if it's stored in the main class where all the action happens in contrast to being a different class? Does making it static improve the performance? Casting it to const? Anything?

Again, the data is accessed billions of times so even minor differences can save a lot of time.

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 :: Program To Change Name Of A Variable

Jun 15, 2013

if there is a program which i can use to change the name of a variable in a c program for example if i declare a variable like

Code:

int ch;
printf("%d", ch);

Can i later change the name of the ch variable to lets say "number" the code now becomes

Code:

int number;
printf("%d", number);

View 6 Replies View Related

C++ :: Possible To Change Value Of Variable In Main Using A Function?

Jan 30, 2014

I'm currently writing a poker game and am trying my best to avoid using global variables. I have a few variables in int main() which i was hoping to use to store the value of each players hand. I then created a function which calculates the value of the hand but cannot get this value back into the main function.

For example:

Code:
#include <iostream>
using namespace std;
void getValue(int value) {
value = 4;

[Code] ....

Is there any way i can get the value of value using this function? If not what can I do?

View 8 Replies View Related

C :: Compiler That Change All Variable In One Occurrence?

Feb 3, 2013

what compiler that change all variable in one occurrence?

View 5 Replies View Related

C++ :: Possible To Change A Single Variable From A Pointer?

Dec 2, 2013

I am stuck at a problem where I have two pointers pointing to the same object, and I need to change an int on one of the pointers but point to the same object.

To be more specific, there is an array of Item objects. A long list of items a player can buy. Then, there is the player's inventory, a vector pointer. Whenever a player buys an item, it sets the pointer to the bought object.

The problem arises when he buys two of the same object. I tried to identify the objects with an ID, but it does nothing, because they are just pointing to the same object, and so I have no way of telling them apart.

This is further complicated by the fact that it is a polymorphic object. So, I can't simply make a new every time I buy an object, without making a hassle. Well, at least I am not familiar with that kind of code just yet.

View 18 Replies View Related

C++ :: Any Variable Type That Can Change Its Value In Global Scope

Nov 17, 2013

is there any variable type(or with another keyword) that we can change it's value in global scope?

View 5 Replies View Related

C :: Variable Won't Change When Calculating Factorial In Euler Number Homework

Sep 27, 2014

I started to learn programming through this site two weeks or so ago. I've got a book with exercices and so on, and one of them involves calculating e within a tolerance given by the user.

The formula for calculating e is the summation of 1+(1/i!), where i -> n.

So, here's the code and I'll explain the problem below:

Code:

#include <stdio.h>
int main()
{
float error;
float terme;
float sumatori = 0;
int cicle_euler = 1;
int factorial;

[Code]...

For some reason, when I set factorial to cicle_factorial, factorial remains 0, which I find puzzling; the program always halts when 1 + sumatori is 2.0 no matter what error is.

This must be a common problem and I suspect it has to do with some distinction between variables inside a loop and variables outside it, but as I lack technical vocabulary I can't seem to find anything on Google.

View 10 Replies View Related

C :: Distinguish Between Character Constant And String Constant

Feb 20, 2015

Can distinguish between character constant and string constant giving examples

View 1 Replies View Related

C++ :: How To Change Just A Single Student Data

Jan 6, 2015

For Example i have made a text file included some students data. Now i want to change just a single student data. That file should remain same just that student's data replace.

View 3 Replies View Related

C/C++ :: Change Data Based On Users Input?

Feb 7, 2014

Player is given choice of x number of Races This choice changes the player's base stats. Then player is given a choice of x number of Classes (archer, warrior, etc.) This choice determines how the player's stats will change upon leveling up.

#include <cmath>
#include <iostream>
#include <ctime>

[Code]....

View 7 Replies View Related

C++ :: Coin / Money Change Code Doesn't Give Exact Change

Feb 20, 2013

My coin/money change code works when there can be an exact change each time, i.e. when the 1 cent option is available. However, when the change options are only $10, $5, $1, 25 cents and 10 cents, it does not give me what I want for instance, I wanted to get change for $237.80, I was expecting to get:

23 10's, one 5, two 1's and 8 dimes. However, the code below is giving me 23 10's, one 5, two 1's and 3 quarters (there is no option left for the 5 remaining cents).how to fix it?

Code:
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void change(double cents, int a[]);
int main() {
double Dollars;
double cents;

[code]...

View 14 Replies View Related

C++ :: Char Data Type Variable

Jun 12, 2013

I have two char variables, m_GPSOffset[13] and m_FileName[100]. When m_GPSOffset has a value assigned to it, say for instance +11:25:30. The first entry of the value, in this case +, is always stored in m_FileName. I am clueless on why this is occurring.

View 15 Replies View Related

C++ :: Sending Variable Amount Of Data Over Winsock?

Oct 31, 2013

Client:

Code: ....
string cmd = "dir c:filequeue > ";
string outputFilePath;
outputFilePath.append(getTempPath());
outputFilePath.append(" est.txt");
cmd.append(outputFilePath);
system(cmd.c_str()); // dir c:filequeue > %temp% est.txt
string content = get_file_contents(outputFilePath.c_str());

send(s, content.c_str(), content.length(), 0); I'm executing the "dir" command to get a listing of Folders/files of one Folder. Then I read the Output of the file and send it over winsock to the Server.

Now, the Problem is, I don't know how I can handle the recv properly, cause I have to set the buffer size without knowing, how much data is actually transfered. Sometimes maybe no files are in c:filequeue, sometimes a 100k.

So I tried to make recv as a Loop:

Server:

Code: ...

while (rc != SOCKET_ERROR) {
printf("
#");
gets(buf); //please no discussion about gets, I will Change this later ;)
if (strcmp(buf, "ls") == 0){
send(connectedSocket, "LIST", 4, 0);

[Code] .....

now it works, but as the recv blocks, it will never leave the Loop, even when the Transfer is finished.What should I do?

I believe I could make unblocking sockets, but that's a bit complicated. Isn't there an easier solution, with malloc'ing the buffer or a Signal when to leave the recv Loop?

View 3 Replies View Related

C++ :: Returning A Different Data Type Depending On A Variable?

May 2, 2013

Okay, I've been working on this Texture class that's going to be compatible with both SDL and OpenGL with the capability to create an array of textures within one class as opposed to multiple classes and such.

Now I've run into a slight issue if I want to return the value of a texture. There's two different types of textures: SDL_Texture, and a standard GLuint Texture. The problem I have is that one or the other is used, not both which is depending on whether or not the person is using OpenGL.

So when the user wants to get the texture, I need the ability to return either an SDL_Texture, or GLuint depending on whether or not OpenGL is being used.

I tried this with a template class, but it didn't work, but I'll post the code so you can see what I'm trying to do.

template <class Tex> Tex Texture::GetTexture(int TextureNumber)
{
if (TextureNumber > NumTextures || TextureNumber < 0)
return;

[Code]....

It basically just comes down to the last four lines of code. If the person is Using OpenGL return a GLuint, if the person is using SDL, return an SDL_Texture.

I would prefer to have the GetTexture function to be one function instead of two separate ones so I don't have to call a different function every time to check if I'm using SDL or OpenGL.

View 4 Replies View Related

C++ :: Adding Data From A File Into Class Variable

Oct 22, 2013

I am trying to add data from a file that would go into a class that would later go into a vector of a class. I'm not really sure how to do it exactly. Here is the code:

Champion_Info.h

#ifndef CHAMPION_INFO_H_INCLUDED
#define CHAMPION_INFO_H_INCLUDED
#include <vector>
#include <string>
using namespace std;
class Champ_Info {

[Code] .....

View 2 Replies View Related

C :: Save Data From Global Variable Into Linked List

Jul 6, 2013

I'm trying to run so called student-administration program.I got functions that can read and write student data, but the one saving the data from about 30 students has some problem that I can't figure. (warning: I'm quite new to C programming)so this is the code:..I guess I can't use global variables as function arguments?

Code:

//global variables
static char ReturnName[31];
static char ReturnFirstName[31];
static float ReturnAverage;
}

[code]....

incompatible types when assigning to type 'char[31]' from type 'int'

View 2 Replies View Related

C++ :: Reading And Writing Variable Binary Width Data

Mar 8, 2013

How to read and write an arbitrary number of bits from/to a file stream.

For instance, how to repeatedly read 9 bits from a file, then change to 10 bits, then 11 bits, and so on?

Obviously one way is by doing a lot of bit shifting, and masking. But honestly, I'm too dumb to get it right. Then I thought about using std::bitset and std::vector<bool>.

View 5 Replies View Related

C++ :: Struct As Variable - Zero (init) Data In Class Constructor

Feb 8, 2013

at my work we use a static analysis tool and it is pointing out some uninitialized variables. One of which is a class member variable which is a C struct. Now, that variable is already declared in the header file for the class.

Currently in the class constructor I am doing:

Code:
memset( &thestruct, 0, sizeof( thestruct ) );

Is there a C++ way to do this? I Googled this but all I really found was:

Code:
StructDef thestruct = {};

Which doesn't really apply here.

View 7 Replies View Related







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