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] ....
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?
There are many questions on the web on how to fix a specific signed/unsigned mismatch, and the solution is usually just making one variable unsigned.
I have a grid (x,y) which should be unsigned, since you can't have a (-5,-3) sized grid. However, I have a Direction object which should be signed, since I can have a (-1, -1) direction. The problem is when I do something like Location_x + Direction_x > grid_x which throws the signed/unsigned mismatch warning.
I tried to write a little bit of code to set all bits within a signed int with exception to the MSB, yielding the greatest max positive value. The odd part is that it works for shorts ints, and longs, which are 2, 4 and 4 bytes respectively, however long longs, with a size of 8 bytes, simply yields -1, which would indicate that it failed to clear the MSB. Heres the little segment in question:
my::numerics is just an exercise- its thoroughly tested and I'm certain thats not the issue.
For shorts, ints, longs, this yields the maximum value. However, when I use it on long longs, the output is 0xFFFFFFFFFFFFFFFF, i.e. ~0. Obviously this means the maximum value for unsigned long longs, but -1 for signed long longs.
I'm really brand new to C++ and I'm currently learning it for fun. I'm working on prototype functions, and I got an error.
I get an error saying “warning : '<' : signed/unsigned incompatibility” but I haven't defined an unsigned int anywhere. Actually, I don't even really know much about them except that they allow bytes from -127 to 127 instead of 0 to 256 for signed int's if I understood correctly...
The function itself is supposed to make a pointer go to the end of a string and add the Null Byte at the end. There's probably an already-existing way to do so, but I don't know it. Plus it is fun making this.
In the below code I'll be making comments for most lines so you may correct me if I'm not doing what I think I am. Any way, here's the code of my "my_fct.h" header file :
#include <stdio.h> //Includes basic functions necessary in C as 'scanf' or 'printf'. #include <string.h> //Includes string functions such as 'strcpy' or 'strcmp'. #include <iostream> //Not sure about what this does. int strend(char[]); /* Specifies the existence of a function 'strend' that uses a char * string as argument and that returns a value of integer type. */
Write a C++ application program to accept a signed decimal integer as input and output the equivalent 2s complement version in 16-bit binary. Include a space between every four bits in the output string. The input will only be processed by the application if it falls in the valid range that can be represented in 2s complement format with 16 bits. The range of a decimal number from - to + is -32768 to 32767.
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.
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 {
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!
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.
I am having a hard time with some of my homework, specifically regarding how to printf floats. I can't seem to print the number i want out using float, it just becomes a jumbled mess.
Thats the code I currently have, I've probably tried everything to get the number to come out, but I just cant seem to figure it out. It should look like this, but with different numbers and stock:
#include <stdio.h> struct database { int id_number; int age; float salary;
[Code] ....
When I compile, I get an error: test.c|18|error: incompatible type for argument 1 of 'printf'| note: expected 'const char *' but argument is of type 'float'|
I thought employee.salary is a float but the compiler expected 'const char'. How do I make this work?
I have the following line of the code. Now I want to save the content to a string. Is there a quick way for me to do the conversion using the same arguments/codes of printf?
I am having trouble with the printf in this function:
Code: void print_orig_array(char string_array[MAX_PEOPLE][NAME_SIZE], int ages[MAX_PEOPLE], int length) { int counter; printf("Original list"); printf("
[URL] ....
Here is the output:
Am I missing something with the format specifier? How do I fix the 84 that gets pushed out?
Code: char Answer; printf(" To search for a specifc word, type (Y), to use a dictionary file, type (N):
[Code] .....
When I run the program, it gives:
To search for a specific word, type (Y), to use a dictionary file, type (N):
Sorry, the given input is invalid, please try again:
I can then input the number. Typing n,N,y or Y goes to the next part of the program without any problems, but if I type something else, I get:
Sorry, the given input is invalid, please try again:
Sorry, the given input is invalid, please try again:
I've fiddled about with the code for a while now, but nothing I do seems to work. What is causing it to be printed twice, or why the first getchar() is ignored.
I'm making my way through most of this assignment that I have, but now it seems like I've run into a bit of a roadblock. The issue that I'm having is not being able to printf a series of ints that I thought I had previously defined in another function. I don't want to clog up this post with the entire code, so I'll just post one function that defined an int to give an example. I will upload the whole thing upon request however.
Code:
#include <stdio.h> #include <stdlib.h> //Prototypes int AGrade1(int* grade1); int AGrade2(int* grade2); int AGrade3(int* grade3);
[Code] .....
I've tried many many things, but I just cant figure it out. This is what it's supposed to look like.
I just checking but confused with float. in that code same size int, and same type double are working but float showing nothing in printf..why?? i'm using GCC compiler int 32bit win7 os
I would like the following code to print: "Why doesn't this print?" and "I would like to print the sum of nc: 5". What am I doing wrong.
#include <stdio.h> //Use to test ideas and formats// main() { int c, nc; nc = 0;
[code]....
My result as compiled by gcc -o testing testing.c
This prints.
test t1,e2,s3,t4, 5,
I have not figured out how to sum and print as the above code indicates, which complicates my ability to do many of the exercises in "The C Programming Language". I am using a MacBook gcc compiler and X code as well. I cannot get the last two printf functions to work. I did the temperature example with "while (fahr <= upper)" and the printf printed.