C++ :: Typecasting Char Array To Int (Not Converting)
Jun 1, 2014
I am looking for the correct syntax to typecast a character array as an int. So I would be able to do something like this:
char ch[4];
//...Read characters from file into ch or something, etc...
int i = ( int ) &ch;
or
char ch[4];
//...Read characters from file into ch or something, etc...
int i;
strncpy ( ( char* ) &i, ch, 4 )
I want all the 4 characters ( or bytes ) of ch to be classed as an int. So we are taking the 4 bytes of the character array and placing them in 4 bytes of the integer? How would i go about this?
In this program, I have to ask the user for an employee, then the program will check to see if the file for that employee exist, if it doesnt then it will automatically create the file.
ReadNew function reads the file....check to see if it exist
CreateNew function creates a new file.
In my code I have no problem with the first part of reading file.. and my createnew function works in other programs where I am asking for input of file name to create the file name. However in this code I cannot figure how to automatically pass the input filename from the ReadNew function to the CreateNew function. I can't ask the user to enter the name a second time, so I have to pass the input filename into both functions. Here is my code.
Code:
//Create a file, append to it, and read it. #include <iostream> #include <fstream> #include <string.h> #include <stdio.h> using namespace std; char filename[256]; string n; string filelist; void CreateNew(ofstream & FileNew);
according to here [URL] .... ô in decimal is 147 and print ô using alt 147
and here [URL] .... ô in dec is 244 but prints ⌠ using alt 244
put on console
char c = ô; cout << (int)c << endl;
prints -109 and print m using alt -109
I am using alt to test char output. Why I'm getting a negative value? Which of the tables are correct? I have string of char that I want to print in hex. I get a hex string but the hex value don't correspond to any of the two tables on the websites because the console converts special char to negative values.
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 am writing a program that asks the user for their gender. I know that atoi converts arrays into integers, but I need the input from the user in the form of F or M into a char, and return it to the main function to be displayed at the end of the program.
string Employee::getgender(char gen) { cout << "Please enter your Gender: " << endl; //atoi function would go here, what for char? getline(cin,gen) return gen; }
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
Creating array in main and then calling a sorting function from sorting class.
In main:
const size_t SIZE = 100; int *array = new int [SIZE]; //fill array with ints, not shown here quickSort(array, SIZE); //calling the sorting function in the sorting class
In the sorting class, quickSort is declared as such:
void quickSort(int arr[], int num);
Everything works great.
Version 2 (issues)
Instead of creating the array in main, I have set up a class for the array, MyArrayClass, where I create an object containing the array of ints. So far so good. The issues come when I write a member function to call quickSort. Even though my MyArrayClass object contains an array of ints, the code calling for quickSort() won't compile as the data type isn't ints but MyArrayClass (which in turn holds ints though). The compiler (using VS 2013 btw) complains that quickSort can’t convert the first argument from 'const MyArrayClass' to 'int[]'.
How do I cast my class object array of ints as an int[] in order to be able to call the quickSort function? Or should I solve this issue in some other way? I tried altering the sorting function to accept the object as it is, but that only created an avalanche of new errors, so thinking that converting/casting the object array --> int[] might be easier...
Q.Write a program using user-defined function which accepts an integer array and its size as arguments and assign the elements into a two dimensional array of integers in the following format: If the array is 1,2,3,4,5,6, the resultant 2D array is
For Example, it the entered string is: 0324152397 I want it to get stored in an array like-[0] [3] ...[7]. Secondly the string entered may be of any length that is defined only at run time. So, I also need to calculate string length. How could I do that.
I made a program that adds two matrices and displays their sum with a max dimension of 100.
/* This program asks the user for 2 matrices called A and B, as integers, and displays their sum, C. The max dimension of each matrix is 100. */
#include <stdio.h> // Construct function void construct() { int m, n, i, j; // Variables int first[100][100], second[100][100], sum[100][100]; // Matrices variables
[Code] ....
Now I need to change it so there is no max size for each matrix. The arrays will be larger than 100x100 so I need to use malloc to create my arrays. So I cant use int A[rows][cols]. This is what I did to covert arrays to malloc. It compiles but it crashes after I entered all the integers.
/* This program asks the user for 2 matrices called A and B, as integers, and displays their sum, C. The max dimension of each matrix is 100. */
#include <stdio.h> #include <stdlib.h> // Construct function void construct() { int m, n, i, j; // Variables int *first = NULL;
I am new to coding Here is the problem. Have a program prompt the user for a filename to open. Change every alphabetic character in the file to a capital letter. Numbers and special characters should not be changed. Print the output to the screen.
Here is my code so far but i am only returning the last line of text capitalized from the file. I am trying to get the program to display all of the three lines of text from the file capitalized. The program displays the file correctly before i try and convert everything toupper();
Code:
Code: #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <ctype.h> int main() { char line[81], filename[21]; int i; FILE *inFile;
I am trying to understand why i keep getting errors in my code. The errors are after the string is converted in my console window. I have to allocate and delete memory via dynamic array to do the problem.
#include<iostream> #include<string> #include<cctype> using namespace std; int main() { string sentence; int size;
[code]....
I just want to know why the extra characters are at the end of my conversion and how to make them stop.
I followed a tutorial to load a bitmap from file and convert it to a array of bytes to access it directly. Now to display the bitmap on screen do I have to convert it to a HBITMAP or can I display it from the array of bytes?
Also the program I am building is a calendar program and I need to know if I can manipulate the bitmaps pixels in the byte array (e.g change color and resize image)