I'm writing a small piece of code that increments through a string of numbers. For every 5 numbers a product is produced. However i'm having an issue understanding an error that keeps occurring.
if you look at the code i've got a string with 5 numbers 1-5 for testing. As the loop increments through the string it takes the first 5 characters and converts them to integers and then a product is returned. The issue is that instead of storing the single letter at position J its storing some combination from the string and as a result gives me a huge product. I tried debugging. I checked what the string[j] value was with a simple cout << and it returned the right number. I even returned the char value before it was converted into an integer and it was the right number.
I came across some code and it's not clear why it is casting an unsigned char * to another pointer type only to free it right after. Here are the relevant structures:
As you can see, _Edge_Message has a *msg field, but in the function below, they cast it to the other two structure types inside the case blocks of the switch statement only to free it. What is the point or advantage of doing this?
Code: void _edje_message_free(Edje_Message *em) { if (em->msg) { int i; switch (em->type) {
I was going over this with a friend and it seems as though getline() is not reading anything in and thus throwing the abort error. I'm not sure why this is because I've included the textfile, with the correct name of course, in both the regular file location and the debug folder. I ask for user input and the user then inputs the name of the file they want, I do some required things behind the scenes and display the results for them in a cmd window. I've included pastebin files for both my header and cpp files because it is far to large for one post I shall, however, post the full code in the comments.
Quick Code
The problem occurs on line 159. I'm assuming once this line is fixed, line 163 will have the same problem.
// Read regular price getline(nameFile, input, '$'); vectorList[count].regPrice = stof(input.c_str());// Casts string to a float // Read sale price getline(nameFile, input, '#'); vectorList[count].salePrice = stof(input.c_str());
I am tying to convert an int to a char. Below is an example of the code that I have but I am getting an error('=':left operand must be l-value). I am not seeing a problem with the code.
int num = 5; char temp[2]; char final[2]; itoa(num, temp, 10); m_pRes->final = temp;
I'm currently finishing up an assignment that was half written by my professor. Below in the testGrades section of code there are two errors both are the same message.
Error: no matching function for call to Grades:: Grades(const char [15])
Test Grades
//Purpose: Test program for the class Grades // Create stu1 Grades object // Add 5 grades to stu1 - only 3 can be stored in stu1 - other 2 discarded // Create stu2 Grades object // Add only 2 grades
Well I tried to assign a new char value from a struct to another char variable but I got the "Cannot convert 'int' to 'char'" error when compiling. I've tried several alternations but I still can't get away with this error.
Here's a section of the code:
javascript:tx('code') for(int i = 0 ; i < 10 ; i++){ pts[i].dist = sqrt((pts[i].x*pts[i].x)+(pts[i].y*pts[i].y)); }
Let's say I have a product that needs service after 500 hours at 100 RPM. I can dsiplay the remaining hours only as an integer and we only have integer math. Management wants it to display 500 for the first hour, not 499 after the first minute is ticked off. But after a few minutes fullHours will be 499 and partialHours will have some value. I have 3 ways to determine displayHours to suit management and I want to know if the first is ligit.
short fullHours = 500; short partialHours = 0; short displayedHours = 0;
// Method 1 - Is is Kosher to cast a boolean as an int? Is TRUE always a 1 or is that a bad assumption?
I find type casting to be very hard to grasp, I am not sure why. I understand how it works I suppose, but I find it hard to grasp why it would be needed in programming. So my question is, how often is type casting used in general programs? Is there an easier way I could be trying to teach myself about it? I am just using the tutorials provided by this site.
double a = 1.0 + 1 + 1.0f; //everynumber will be added up as a double even the last one which is a float. All 3 numbers will turn into 3.0 as a double.
int x = (int)(7 + 3.0 / 4.0 * 2); //the variable will do the math bracket first. then the va type will still be an int because int was never changed.
Console.WriteLine((1 + 1) / 2 * 3); // 1 + 1 will be done first then 1 / 2 then * by 3 Console.WriteLine(x); Console.ReadKey();
I THINK THATS ALL WRONG ^ =/ like the comments
double a = 1.0 + 1 + 1.0f;
In this equation, everything is using addition, so we start working left to right. 1.0 + 1 is the first step. These two representations of 1 aren't the same type though. In fact, none of the three are.
The first is a double, the second is an int, and the last is a float.
So in order to do 1.0 + 1, we need to convert one type to another. Since the double type is "wider" than the int type, we'll move things up to the double type. We'll convert the int version to a double, and to 1 + 1 using double types, resulting in 2.0 as a double.
Next we do the other addition. This has the same problem, though, because we'll be adding our result from the first step (a 2 as a double) to a float. So again, the float gets converted up to a double and we do the addition using doubles.
We now have a value of 3 that is the double type, which we can simply store in our a variable without any conversions at all, since our value is already a double type.
So, I've used int to float cast before. And it makes sense that it preserves the int value just converts it. I don't really need the answer, I'm just interested, and a resources could suffice.
1. Why in my test program it seems to preserve the int value, I expect that, but why for 0x8000 is it registering bit negation also. I know that is the negative bit for float, but it seems wrong. Is this an error in the gcc compiler conversion code?
2. What is the documentation on these type cast on how they actual work.
3. I know like no assembly, I'm wondering if some of the built in routines to handle or is it all c side code.
4. Can I convert type and preserve the bits. Maybe use void* casting ? I've never really bothered with void* so I don't all that I can do. Except be a pointer that doesn't know the type, obviously. I tested that out in the second code, the output doesn't seem correct, except -0.000. Is it working and my test numbers just are improper float format? It can't be that I test 0x3E20000 = 0.15625 from SingleWiki , but I got 1.328e-36 so the int to void* to float doesn't seem to work in the code below:
#include <cstdio> int main(){ float flout; unsigned int num = 1; int ant; printf("Int Shift float"); for(unsigned int shift=0;shift<32;shift++){
Casting Pointers in C Programming. I don't want to move onto implicit casts until I have this down pat. I'm failing to understand how casting pointers works.
The line int *mnt = (int*)&flt; if I read this correctly passes the address of flt which has been converted to an int to the pointer mnt.
1 - When I output mnt I get a garbage value, probably because the address of flt is then converted to a pointer and passed onto mnt as a value and then reinterpreted as a memory address. (that is the first part I don't understand)
2 - - What exactly does the (int*) cast say? Does this mean that a pointer will be returned or an address will be returned. What does the fact that the * is inside the parenthesis mean?
Code: #include <iostream> using namespace std; int main() { float flt= 6.5; int *mnt = (int*)&flt; cout << mnt << endl; // outputs hex memory address cout << *mnt << endl; // outputs garbage value }
This code i made, utilizing the type casting construct, isn't outputting what i wanted. The output for 'Dollars' and 'Cents' are returning '0' for both. instead all i want it to do is seperate the two. for example changing the float value of amount to an integer, giving a dollar value.
I am doing an exercise which has to do with International country codes.The user must give a code and the programm will display the corresponding country.
I am just getting back in to C++ after 10 years not doing any, contributing to an open source project. I'm adding in some functionality and am hitting a road block.
I need to send a multicast packet out on the network that is structured in a certain way. I have the definition, and know what data is going in each byte. I can successfully send a message using multicast, I now just need to send the right message.
I have used a char array to hold the message, as each char represents 1 byte, and I can transmit the array.
I am having trouble putting all of the data in the right place though. If my source data is a string, then I seem to be able to convert it, but if it is a short or int, then I keep getting errors when compiling. Similarly, two of the lines, (version and type) i initially tried using char arrays with a length of one.
Should I be using memcpy or a different function, or even be doing this in a totally different way altogether? This is the code that I am using, along with the packet structure:
//Construct a Zone Query packet // 4 bytes - Signature "Ohz " = 0x6f, 0x68, 0x7a, 0x20 // 1 bytes - Version = 1 // 1 bytes - Type (0 = Zone Query, 1 = Zone Uri) // 2 bytes - Entire message length = 12 + zone length // 4 bytes - Length in bytes of the zone ID // n bytes - Zone ID to query
[Code] ....
The errors that I get are:
error: invalid conversion from ‘short int’ to ‘const void*’ [-fpermissive] memcpy(buffer + 6, packetLength, sizeof(packetLength)); ^ [Code] ....
Goal: To allocate some memory as a char*, read in some binary data, re-interpret it as a float* and then free the memory.
My code looks like:
void someFunction(float* &result) { char * tmp = new char[1000]; //...Fill the char buffer here... result = (float*)tmp; //Reinterpret binary data as floats
[Code] ....
Is the cast back to char* necessary on the red line (or could I have validly left it as float*)? Would it be different if I had written char * tmp = (char*)malloc(sizeof(char)*1000); on the blue line (and correspondingly used free (char*)floatData on the red line?
I am parsing a binary data file by casting a buffer to a struct. It seems to work really well apart from this one double which is always being accessed two bytes off, despite being in the correct place.
If I attempt to print GROSS using printf("%f", row->GROSS) I get 0.0000. However, if I change the type of GROSS to char[8] and then use the following code, I am presented with the correct number...
Code:
typedef struct example { double d; }
example; example *blah = (example*)row->GROSS; printf("%f", blah->d);
I've made a code to check whether or not a save file has been created correctly, but for some reason it always returns this line: readdata[qa]=='1' as true. in which qa is the counter I use in a for loop and readdata is a character array consisting of 50 characters that are either 0, 1 or 2.
this is the entire code:
#include <iostream> #include <fstream> #include <string> using namespace std;
[Code]....
at first is also went wrong at line 22 and also returned that as true, but then I added brackets and it worked.
I am trying to concatenate two words from a file together. ex: "joe" "bob" into "joe bob". I have provided my function(s) below. I am somehow obtaining the terminal readout below. I have initialized my memory (I have to use dynamic, dont suggest fixing that). I have set up my char arrays (I HAVE TO USE CHAR ARRAYS (c-style string) DONT SUGGEST STRINGS) I know this is a weird way to do this, but it is academic. I am currently stuck. My file will read in to my tempfName and templName and will concatenate correctly into my tempName, but I am unable to correctly get into my (*playerPtr).name.
/* this is my terminal readout joe bob <- nothing is put into (*playerPtr).name, why not? joe bob joe bob seg fault*/ /****************************************************************/ //This is here to show my struct/playerInit
Is there any way to cast a non-const variable to const one?
I want to read variable n from file and then use it to declare array "int arr[n]", but because n is non-const, the compiler doesn't allow me to do that.
I have a file which contains a year and the name of an associated file to be read. I need to extract the data in the txt file and perform some calculations.
( year data file) 2004 2004data.txt 2005 2005data.txt 2006 2006data.txt
Here is what I do. I first declare "char yeardata" and then pass "2004data.txt" to it. Then I call yeardata in ifstream to extract the data inside the file "2004data.txt". The problem is that char yeardata is not constant so I cannot pass the file to it. It doesn't work if I change "char yeardata" to "const char yeardata".
Code: int oldnewcomp_temp(char* lcfile) { using namespace std;
Cannot manage to find the error source when i try running the program, the first part of the program runs just fine its when i try to get the temperature one that i get the error
#include <iostream> #define pi 3.141592 using namespace std; int main() { double r, h; //declare variables for radious and height double Surfacearea;
I've just recently started to learn C++, and I'm encountering some errors I can't seem to figure out.
InventoryItem.h:
Code: #pragma once class InventoryItem { public: InventoryItem(string name, int amount); ~InventoryItem(void); string getName(void); int getAmount(void);