C/C++ :: How To Set Each Bit If The Variable Is Short

Jan 3, 2013

i am using the below logic. but it gives me error when i am setting the bit_position 15 or 16. i suspect, the problem with the integer range.

short bit_map, bit_position  
bit_map = bit_map | ( 1 << bit_position )

View 2 Replies


ADVERTISEMENT

C++ :: Making Function To Read Unsigned Integer Into Variable Of Type Unsigned Short Int

Apr 3, 2014

How can i write a function that will read an "unsigned integer" into a variable of type "unsigned short int"? i can not use cin >> inside the function.. so i am looking for atleast a hint!

View 16 Replies View Related

C++ :: Difference Between Int And Short Int

Aug 19, 2014

size of int is 2 bytes and of short int is also 2 bytes.The range of values for int and short int are the same.

Then why int and short int are used? only int or short int is enough ....

View 4 Replies View Related

C++ :: How To Set Certain Bits Of Unsigned Short

Aug 8, 2013

I have a double variable and depending on certain conditions I need to set certain bits of an unsigned short Variable. For example, if double var is odd I need to set the 15th bit of the unsigned short variable.

View 4 Replies View Related

C++ :: Returning Short Int Array In A Function

Feb 14, 2014

I'm trying to return a short int in a function..This is my function signature :

short int* StartRecord(int seconds)
this is my array :
short int waveIn[NUMPTS];

I'm trying to get the data into an array :

short int arr [] = StartRecord(3);

getting this error: Error2error C2440: 'initializing' : cannot convert from 'short *' to 'short []'

View 1 Replies View Related

C/C++ :: How To Null Terminate Unsigned Short

Jul 23, 2014

I've stored a binary pattern in what is interpreted as an unsigned short.

unsigned short byte_one = 128;

I've done some bitwise manipulation and want to store it back into an array, however, it needs to be null-terminated.

buf[1] = byte_one;

How do I null-terminate this?

View 3 Replies View Related

C++ :: Printf With Signed Short / Byte

Jul 17, 2012

I am using print/sprintf with a "%i" format string. Works fine if the input is indeed a 32bit integer. But how can i put in 8/16 bit (ie short & byte) 'integers'? If i just throw them in, they are always taken as unsigned, as the topmost bit/s is/are casted to zero... [URL] ....

View 3 Replies View Related

C++ :: Convert 2-byte Array To Short Int?

Nov 18, 2012

I'm having trouble reading a block of bytes into a vector of short ints. My code is as follows:

Code:
FileStream.seekg(2821);
vector<short> g_Data;
int iter = 0;
g_Data.reserve(maxNumOfInts);

[Code] ....

The relevant block of data starts at offset 2821 in the file. Every two bytes are a signed short integer. What's odd is that it's giving me the correct results only part of the time. At offset 1052421 and 1052422 there are two bytes 40 and 1F that are correctly read in as 8000, but at offset 1052415 and 1052416 bytes 88 and 13 are read in as -120 instead of 5000.

I don't see anything wrong with my code, though, unless I'm misunderstanding completely how to convert an array of two bytes into a single float. Is my method correct? Better still, is there some way to just convert en mass an array of bytes into a vector of signed short ints?

View 5 Replies View Related

C++ :: Simply Converting Character Array To A Short Int

Feb 14, 2013

Any function of simply converting character array to a short int?For example. char array[4]={'0','2','f','f'}; to short int 767?

View 3 Replies View Related

C/C++ :: Polymorphism Is Stopping Short Of Desired Class

Mar 2, 2014

I've been working on this project which inserts data for different types of books lately, and I'm trying to utilize inheritance and polymorphism. The issue I've been having is after I create an object with my MediaFactory, I go to insert data into that type of object, but it won't reach the correct child class. The parent class in this case is MediaData. The class I'm trying to reach is Childrens, and the class that falls in between is called Book. The function I'm calling to insert the information is setData(ifstream&). This function simply places the information from a txt file into an object with the insertion operator.

Right now the program runs into the setData function of Book instead of Childrens. I need Childrens so I can enter all the required attributes (title,author,year). The call to this function is in MediaManager through the function called buildMedia, and my test is running into the case if (type == 'Y'). From there a pointer of type MediaData is created and pointed to a new object returned of type Childrens. I'm using my debugger in Xcode which does show that the correct object type (Childrens) was created.

I've tried a couple things so far. First I created another function called getData. From there I passed in *media and infile (txt input file) into that function, which then passed it right into setData with a pointer to the object and infile in the parameter. That didn't work, so I also tried going back into Childrens and removing MediaData from all my virtual functions and replacing it with Book. I got the same result when that happened; It morphed into Book class instead.

I have a portion of my UML as a reference. Childrens is a Book; Book is a MediaData. I also included all code for MediaManager, MediaHash, MediaFactory, MediaData, Book, and Childrens.

I did not include the operator overloads in the .cpp files to eliminate some redundancy.

//-----------------------------------------------------------------------------
// MediaManager.h
// Manager class for MediaData type objects
//-----------------------------------------------------------------------------

#ifndef MediaManager_H
#define MediaManager_H
#include "MediaHash.h"
#include "MediaFactory.h"
#include "MediaData.h"
using namespace std;
class MediaManager {

[Code] ....

View 3 Replies View Related

C++ :: When Are Rvalue References To Primitive Integers Short-lived Or Long-lived

Sep 4, 2012

I have done some experimentation on rvalue references with the TDM-GCC 4.6.1 compiler and made some interesting observations that I cannot explain away with theories. I have a very simple program that does not deal with objects but int primitives and that has defined 2 functions:

foo1 (returning a local variable by rvalue reference) and
foo2 (returning a local variable by value)

#include <iostream>
using namespace std;
int &&foo1();
int foo2();
int main() {
int&& variable1 = foo1();

[Code] .....

It seems the value returned by foo1 and received by variable1 dies out after some time - perhaps, a brief period of some milliseconds. Notice that I have prevented cout from printing "My name is softwarelover" by commenting it out. If I allow that statement to run, the result is different. Instead of printing 5, 0 it prints 0, 0. Seems like it is because of the time-delay introduced by "cout << "My name is softwarelover." that 5 turns into 0.

Is the above how an rvalue reference is supposed to behave when referring to a primitive integer which a function returned by reference as opposed to return-by-value? By the way, why is it 0, why not garbage?

Notice also that variable2 never seems to die out, no matter how many times I print it with cout! variable2 refers to a primitive integer which a function returned by value, not return-by-reference.

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







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