C/C++ :: Possible To Initialize Char Value Into Int Value?
Oct 13, 2012
#include<stdio.h>
int main() {
int j=printf("Hello.");
return 0;
exit(10);
}
In the above program is it possible to initialize int j with char value "Hello."?
View 6 Replies
ADVERTISEMENT
Nov 28, 2014
I am trying to store data in a struct to be able to read it latter . have problems initializing this.
struct FoodAndDrink {
struct Food {
char* CannedGoods[2] = {
"Canned Spaghetti",
"Canned Tuna",
[code] .....
View 7 Replies
View Related
Jul 8, 2014
How are map variables initialized?
PLEASE EXPLAIN THE FOLLOWING CODE TO ME?
// accessing mapped values
#include <iostream>
#include <map>
#include <string>
int main () {
std::map<std::string, int> mymap;
[Code]...
View 2 Replies
View Related
Jun 2, 2014
How to initialize a QMap<QString,int> like following?
Code:
map<string, int> m = {{"aa", 1}, {"bb",2}};
View 2 Replies
View Related
Mar 25, 2014
The deck of cards so it is populated with all 52 cards. Once you have populated the deck, you need to shuffle the deck. Here is a simple algorithm you might consider for this purpose:
For each card up to the middle of the deck generate a random number modulus the size of the deck, swap the current card with the card at that location done
To implement this requirement you need to implement and use the following functions:
void initDeck(Card * deck);
View 4 Replies
View Related
Jun 21, 2013
I have written a program, in which I create a NxN-matrix. As I need some different samples (some different such NxN-matrices), I try to create a new matrix in each recall of a while-loop. The first recall of the while-loop works without any problems, but the second recall crashes, but I get any errors from the compiler. Here is my quell code. The problem should be with line 44:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
[Code].....
View 5 Replies
View Related
Dec 6, 2013
There are two class.How can I initialize an array of different class in a constructor?
class Ticket{
private:
int ID;
[Code]....
I have to provide a no-argument constructor (Cinema();)to initialize the ticket array and give the right ticket ID.
View 1 Replies
View Related
May 8, 2014
imagine you have a world class. Than you have a tile class. Now, in the world class is a array with a lot of tiles and I want to initialize them with my imagination(For example I want a grass floor). But how I can do this, the array can only be create with the standard constructor. But it would be stupid and not very fast, that the tiles are first initialized with the standard constructor and than overridden to build the world. Whats the best way to initialize such a array ?
View 12 Replies
View Related
Aug 4, 2012
I would like to write a program that prompts the user to enter a series of client names which the program will then store in an array of structures. I'm thinking something along the lines of:
struct sInfo {
vector<string> vName;
float fClientHoldings;
};
sInfo sClientData[100]; //create an array of structures for up to 100 client's
sClientData[0].vName="Acme"; //these will be inputted by the user in my program, but I've attempted to initialize them here for simplification
sClientData[1].vName="Enron";
I can't declare a structure array after I declare it. Is this true? If so, is there an alternate approach that I should be using?
View 10 Replies
View Related
Oct 29, 2014
Code:
cout<<"Enter Filename for input e.g(inp1.txt .... inp10.txt):"<<flush;
cin>>filename;
ifstream inpfile;
inpfile.open(filename,ios::in);
if(inpfile.is_open())
[Code] .....
View 8 Replies
View Related
Dec 23, 2014
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.
View 4 Replies
View Related
Sep 29, 2014
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
[Code]....
View 2 Replies
View Related
Apr 4, 2013
I've been messing around with loops/functions and basic logic and come up with a small maths program. Here it is:
Code: #include <iostream>
#include <string>
float divide (float x, float y) //function to divide numbers {
return x / y;
[Code] ....
Would initializing the string 'anotherGo' to a value that makes the loop run at least once be a suitable way of doing this rather than using a do/while loop? I read that a do/while loop is a black sheep but I have come across a number of uses for it. Maybe it is just preference which one you should/could use?
View 9 Replies
View Related
Feb 25, 2014
In C how can I initialize a variable that is not a letter or number? For example with a number I can :
Code:
int i = 5;
for ( i = 0; i <=5; i++ );
printf( "%d", i ) This would display a row of 5's
but what if I wanted to display a row of -----? What I am trying to do is read in a simple txt file, around the file I want ----1----2-----3 ect ect on the top ----a----b-----c down the side Then I want to be able to change the file at lets say position c2 and save it. This is the early stages of my attempt to set up a editable table.
View 3 Replies
View Related
Mar 28, 2013
I was looking at some linked list material and was wondering something. Can you initialize a data member inside a struct like in C++? i.e.
Code:
typedef struct node
{
int data;
struct node * next = NULL; // this is the line in question
} LLnode;
View 3 Replies
View Related
Feb 22, 2015
I'm not finished with this code, but I am stuck on this one glitch. This is what I have so far, and it's not working.
Code:
int main() {
/*Please input an n value greater than zero. Otherwise, exit the program by entering a carriage return*/
printf("Please input an n value greater than zero. Otherwise, exit the program by entering a carriage return
");
int summation = 0, x, y;
scanf("%d", y);
for (y == x; summation <= M_E && x <= 34; x++)
[Code] ....
View 1 Replies
View Related
Feb 6, 2014
I am creating a class that has a private array on the heap with a constructor that takes the size of the array and initializes it on the heap. Later I have to make a deconstructor delete the space and print out free after.In my code, I was able to heap a private array and make a deconstructor, but I don't know how to take the size of the array and initialize it on the heap. My guess is this:
int* size = new int();
Also when you initialize size on the heap, don't you also have to delete it too? If so, where, in the code, do you do that? Here is my code so far.
Class Student {
private:
int size;
int* array = new int[size];
public:
Student(); // Constructor
~Student(); // Deconstructor
[code]....
How do you make a constructor that takes the size of the array and initializes it on the heap
Student::~Student()
{
delete[] array;
cout << "Free!" << endl;
}
View 1 Replies
View Related
Jul 22, 2013
I am currently practicing designing classes. In one exercise, I am trying to store 15 words in an array, and randomly print one (using the rand() functions and seeding it with crime. I have a header file, a respective .cpp file, and a main .cpp file. When I try to compile the code using g++ GuessWord.cpp UseGuessWord.cpp -o UseGuessWord, I get the following error in my constructor: expected primary-expression before ‘{’ token
Here is my code:
header file (GuessWord.h):
#ifndef GUESSWORD
#define GUESSWORD
#include <string>
using namespace std;
[code].....
View 2 Replies
View Related
Aug 7, 2013
I'm about to solve least-squares problems within a C++-Program. (Equations like inverse(transpose(A)*A) * b and so on)
So I use cmatrix as library and there is the following problem:
#include <cmatrix>
typedef techsoft::matrix<double> dMatrix;
dMatrix A(nDataPoints, numCol);
dMatrix z(numCol, 1);
dMatrix b(nDataPoints, 1);
dMatrix (nDataPoints, nDataPoints);
nDataPoints and numCol are globally defined integers which get the values 120 and 13 (just about the size of those matrices).
So, A is not a problem but as soon as it gets to z and b there is a dialog "Out of Memory" and it stops at the line
return HeapAlloc(_crtheap, 0, size ? size : 1);
(in malloc.c).
This program debugged without any problem with same code - now I just edited the size of the matrices. And I tried the same with another library called "Eigen" and I get the same problem - so I guess there is a problem with the heap and I have to do some kind of memory allocation...
View 4 Replies
View Related
Feb 1, 2015
I am struggling to understand what the correct way to initialize instances of a class for object orient programming is.
If I am not mistaken, there are 2 methods to initialize an instance "instance_name" of CLASS1 by calling the constructor:
(1) CLASS1 instance_name(argument);
(2) CLASS1 instance_name=CLASS1(argument);
As it turns out, I cannot use method (1) when I try to initialize a private instance inside another class. But I can use it in the main() code. I hope the code below will explain what I mean. Why only method (2) will work inside another class? Or is there another fundamental mistake I am making?
(BTW: I used CodeBlocks 13.12 with GNU GCC compiler for this example)
#include <iostream>
using namespace std;
class CLASS1{
[Code].....
View 6 Replies
View Related
Apr 23, 2014
We can initialize normal function prototype's parameters with zero like this:-
void output(float = 0.0, int = 0);
or
void output(int = 0, int = 0, double = 0.0);
But how do you do the same for a pointer array or simply an array?
Assume that second parameter has to be an array.
I have tried the following and it does not work:-
void output(float = 0.0, int = 0);
void output(float = 0.0, *int = 0);
void output(float = 0.0, int* = 0);
void output(float = 0.0, int[] = 0);
void output(float = 0.0, int []);
But if I skip the default declarations altogether, it works.
like:
void output(float, int []);
or
void output(float, int*);
how can I do it by explicitly writing zero, just like the first cases?
View 2 Replies
View Related
Jan 31, 2014
I have a class which I need to initialize some arrays upon its creation. At first, I tried doing it like I would in Java -
float xyzw[4] = {0, 0, 0, 0}
but that didn't work. Then I had someone suggest using initialization list. I have done so below-
#include <vector>
#include "Buffer.hpp"
class Vertex {
public:
Vertex() :
xyzw{ 0, 0, 0, 0 },
rgba{ 0, 0, 0, 0 },
[Code]....
However, the above code gives me the following error:
Error1error C2536: 'Vertex::Vertex::xyzw' : cannot specify explicit initializer for arrays
I am using Visual Studio 2013.
Also, is there any way to edit previous posts? I keep making mistakes with the code tags.
View 5 Replies
View Related
Mar 6, 2013
I want to make a class, named generation, having a vector<chromosome> type data member.
chromosome is also class name.
chromosome has a 'route' as data member, which type is class TArrayI defined in ROOT package.
the size of route is to be fixed according to parameters of generation class object.
class chromosome{
public: chromosome(){;}
....
protected:
TArrayI route;
};
class generation{
public: generation(int PopSize=300, int numCity = 36){
.........
}
vector<chromosome> Population;
};
I want each element, chromosome of Population, to have a numCity size of route.
View 2 Replies
View Related
Mar 7, 2014
I'm a newbie to C++ and I am writing a code that searches strings and checks their value. I'm mostly working with if statements.
My code tends crash on Dev C++, even when I haven't recently saved any changes or compiled it, citing "memory access errors".
I recompiled in visual studio, and visual studio told me I have string out of bound errors.
My code is very long... but most of it is copy and pasted with slightly different conditions.
I believe the problem lies in how I initialize arr[4] ??
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
cout << "Hall symbol -- generator matrix program" << endl;
// Setting up a data structure bc a normal array cannot handle the data for the spacegroups
[Code] ....
View 9 Replies
View Related
May 3, 2014
Need a C++ constructor to initialize each members of an array. how to give value for for each elements of an array declared as a class object according to the users input.
View 1 Replies
View Related
Aug 26, 2014
I somewhere read "You cannot initialize a structure like that at run time."
Example:
struct item_info {
char itemname[15];
int quantity;
float retail;
float wholesale;
[Code] ....
But if you want to assign values at run time then you have to do it manually like:
strcpy(item[0].itemname, "rice");
item[0].quantity = 10;
item[0].retail = 40;
item[0].wholesale = 30;
I tried in internet but am unable to know the differences. I want to know the difference between those two in terms of run time and compile time. Explanation required also for below one. Is this run time or compile time? How does we actually decide which is run time and which is compile time!
struct item_info {
char itemname[15];
int quantity;
float retail;
float wholesale;
//int quatityonorder;
[Code] ....
View 3 Replies
View Related