C++ :: Variable That Can Hold String And Int In Same Time?
Feb 12, 2014Is there a variable that can hold string and int in the same time?
If not, what can I do if I want to input data with string and int like a password for an example.
Is there a variable that can hold string and int in the same time?
If not, what can I do if I want to input data with string and int like a password for an example.
So far i know that the pointer is address value to the real variable. Pointer size depends on operation system right?
for example
32bit systems: 4byte pointers
64bit systems: 8byte pointers
128bit systems: 16byte pointers?
Anyways, there have to be an variable type in c++ what can hold a pointer.
Let's imagine that the int is the thing im looking for
struct stc1 {
char *chars;
int ints;
}
struct stc2 {
char *chars;
float floats;
[Code]....
My wish is to hold different types of variable or groups of different type of variables in one variable.
I am developing program in windows now yet it will be used in linux and who knows with what x bit systems.
im doing a program to store name, age, time and fitness. and i need to hold a table of 5 such records.can i do this?
#include <iostream>
using namespace std;
int name1, age1, time1, fitness1;
int name2, age2, time2, fitness2;
int name3, age3, time3, fitness3;
int name4, age4, time4, fitness4;
int name5, age5, time5, fitness5;
[code].....
So i have this -
time_t t4 = time(NULL);
//and then some stuff
time_t t5 = time(NULL);
iElapsedYah = t5-t4;
fElapsedYah2 = iElapsedYah/iNumTimes;
printf("It took %f seconds", fElapsedYah2);
but it always prints 0.0000000, and i dont know why even though individually
iElapsedYah = not zero and iNumTimes = not zero
I am using windows 7 and m complier is Dev C++
In a function, I have a static variable that I want to assign the time in seconds when a certain condition is met and keep that value until a different condition is met. The time value is a struct. Since now->sec is always incrementing, will timeWhenEventMadeActive below hold onto the initial value or will it increment every time the function is called? I cant seem to test this.
static time_t timeWhenEventMadeActive = 0;
static bool initTime = 0;
if (!initTime) {
timeWhenEventMadeActive = now->sec; //holds uptime value in seconds
[Code] .....
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.)"
i need to display the current time and date in my program and use variable to store each of the time values like
a = 10
b = 29
c = 31
printf(``%d : %d : %d``, a,b,c);
which then give the output as - 10:29:31
How to save the time in variables in C....
i am using C in windows 7 and my complier is Bloodshed Dev C++
How do I use string and atoi at the same time?
string scroes;
char input[SIZE];
cout<<" Enter your score" << endl;
cin.getline (input, SIZE);
scores = atoi (input);
This is what I have:
#include <ctime>
#include <string>
using namespace std;
void date_and_time(string &date, string &time){
time_t current_time;
[Code] ......
Is there a way to make sure that the strings would not contain trailing spaces?
I have a vector<string> of times that I want to convert to vector<double>. The time string is in the form 00:00.000. Is there a STL or something like it algorithm or function to do this, otherwise what would be the best way to do this with a function.
View 3 Replies View Relatedmay i know how do i read a string 2 characters at a time?
lets say i have a for loop like this
for(int i=0;i<stringLen;i++)
{
for(int j=0;j<???;j++)
{
//insert code
}
}
what i want to do is i want to read a string 2 characters at a time and store them into a vector.
I would like to have a program with a 5 mb string embedded in it kind of like this:
Code:
const char *c;
c = (const char*)"BEGIN_STRING_HERE, (5 MB of arbitrary values here)";
Is it possible to generate this string at compile time? Maybe with a macro? I'm trying to avoid a source file with a 5 mb string in it.
How to change an enum type variable to a string type variable?
View 2 Replies View RelatedI'm using SDL to try to create a Run and Shoot game. But I do not know how to check if a key is down while the user is HOLDING it.
I do know how to check if a key was pressed.
I have tried with the "event.key.keysym.sym" and "Uint8 *keystate = GetKeyState(NULL)" both worked to check if a key was down but I thought that the GetKeyState(); Function would even check when a key where HELD down
I want my player to move while holding down left or right arrow. So I did something like:
Code:
Uint8 *keystate = GetKeyState(NULL);
if (keystate[SDLK_RIGHT]) {
apply_surface(x++, y, player, screen);
}
How to check if a key is held down?
How can I read a file that contains numbers only, but read it by three digits at a time? I have a long string of numbers and every three digits corresponds to a particular number in itself. i.e. a string of 064045154 would need to be read as '064' '045' and '154'. I need to then subtract one from each of these numbers and the new values I need to convert into their ASCII characters and place these in a new file. This is what I have (focusing on the 'Decrypt' function) but all it does is in the new file place a string of the same character repeated over and over a total number of times equal to the number of integers in the numbers file.
Code:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "limits.h"
int Encrypt(char * FILENAME)
[Code]..
I had a program (on console) that uses a third-part software to draw some graphs. In order to hold the graphs on the screen, I used cin.get(); and that worked.
Now I created a GUI with Qt. The code remains generally the same. The code continues to call the software to draw graphs (during drawing graphs, there is a console opened automatically). Butcin.get(); in the code cannot hold the graphs on screen anymore. The graphs appear and disappear immediately.
I have a task to hold a number like 4.0000000000000000199e+30 and, in a variable like long double (the largest of the data type) doesn't hold the whole number, holds only 4.099e+30, like that.
Any way to hold the whole number?
How would I use the list container to hold a class?
class A {
private : int x;
public : void setX(int val) { x = val; }
};
class B {
private : std::list<A> pdata;
public : void addToList();
};
For adding, I thought of trying something like
void B::addToList() {
A *tmp = new A;
if(A != 0) {
tmp->setX(5);
pdata.insert(tmp);
delete tmp;
}
}
How would I do what I'm trying to do? Or is this the wrong way to go about it? For the actual program, "B" would contain several lists of various classes.
I have an abstract class named Terrain, and a class named RoadMap, which supposed to hold an N*N array of Terrains. But I'm not sure what type should the RoadMap class hold:
Code:
#ifndef TERRAIN_H
#define TERRAIN_H
class Terrain {
[Code] ....
I can't use an array of refernces here, so I tried this:
Code: Terrain** terrain; and then I thought this was the way to go:
Code: Terrain (*terrain)[]; But now I'm not sure.
The N*N matrix size supposed to be determined according to a given input... What type should I use there?
// This program creates a structure to hold data for a kennel
#include<iostream.h>
struct KennelList {
int dogID;
char gender;
int age;
[Code] ....
For the past couple of weeks I have been working on a template to hold two-dimensional arrays. Right now I am puzzling over an indexing question.
There are many places in the template where I would like to use initializer_lists to refer to user-specified row and column ranges, particularly in member function arguments. A typical example would be a member function whose declaration would be along the lines of:
Code:
Array<Type>::some_function(std::initializer_list<int> columns, std::initializer_list<int> rows); which could get called via
Code:
arrayInstance.some_function({3:4}, {5:8});
It would be really nice to be able to use Matlab-style indexing to specify the last column, or the last row, in the Array object -- along the lines of
Code:
arrayInstance.some_function({3:4}, {5:END}); where END takes the value -1, and can be defined in Array, or somewhere else.
The way I have tackled this so far was to write myself an Indices PODS class with two elements to hold start and finish values, and a constructor-from-initializer_list that looks something like this:
Code:
Indices::Indices(std::initializer_list<int> range, int replace_value) {
int const *it = range.begin();
start = (*it == END) ? replace_value : *it ; ++it;
finish = (*it == END) ? replace_value : *it ;
...
}
So the elements of "range" give the values of Indices::start and Indices::finish -- but if either of them are entered as END by the user, they will be replaced by replace_value. (The default value of replace_value is END, so Indices::start and Indices::finish will never change if it is omitted.)
I also defined an Indices::endset(int) function to do the same thing for existing Indices objects:
Code:
Indices::endset(int replace_value) {
if (start == END) start = replace_value;
if (finish == END) finish = replace_value;
} Using Indices::endset, you can code up Array::some_function by modifying the above signature to something like
Code:
Array<Type>::some_function(Indices columns, Indices rows) {
columns.endset(this->M);
rows.endset(this->N);
...
}
This does work, and I've been able to use it in practice. However, it is klutzy. What I would really like to be able to do is have the Indices constructor handle value-replacements in "columns" and "rows", instead of needing to put calls to Indices::endset in every single Array<Type> member function that uses this approach.
The basic problem is that, when Array<Type>::some_function is called, there is no easy way of inserting Array<Type>::M and Array<Type>::N into the optional argument of the Indices constructor when "columns" and "rows" are being built.
The Indices class needs somehow to get access to these, and know which one is being used, M or N. So it needs to have some sort of deeper connection to Array<Type>, but I don't know what that connection should be.
I need to dynamically create a new Memo structure to hold memorized fib #'s.I have two structures:
Code:
typedef struct HugeInteger
{
//array to hold the digits of a huge integer
int *digits;
//number of digits in the huge integer
int length;
}
[code]....
am having trouble with initializing the struct inside of the new Memo, I need the digit fields to null and the length field to 0 to indicate that F[i] has not yet been memoized...I have F->digits and F->length in the for loop but this just simply doesn't work..
Consider a new data type, the mikesint, which can hold 9 bits.
(a) What is the largest integer that an unsigned mikesint can hold?
(b) What is the largest positive integer that a signed mikesint can hold?
(c) What is the largest negative integer that a signed mikesint can hold?
Not sure how to determine this. I'm stuck.
Using SFML, I had a Board class which held multiple vectors of all of my object types in the game, and then it also held a vector of pointers to the memory addresses of these object instances, like this
class Board{
//...
std::vector<AbstractObject*> GetAllLevelObjects(){ return allLevelObjects; }
//so these are used to hold my object instances for each level
[Code]....
When looping through this vector and drawing the sprites of the objects, I get the runtime error 0xC0000005: Access violation reading location 0x00277000. I solved this error by storing the vector of pointers in the class that holds my Board instance, but I'm wondering why only this solution worked? Why couldn't I just have my vector of pointers in the same class that the instances of those objects were in?
As written in the title, I want to be able to extract a variable value from a string containing the variable's name. I know one can use associative containers such as maps but is there another more direct way?
E.g.:
int variable = 5;
string str = "variable";
// how do I get the value of 5 out of the string containing the variable name?
If I am correct, this is called 'Reflection', correct me if I'm wrong.
I know C++ has no inbuilt 'Reflection' class or anything like that so I was wondering if there is a workaround for this kind of thing or is there a library out there which can do this? (that's if I have the name right).
I have found a library called Boost Reflection which sounds like it could do this but I just wanted to make sure that reflection is actually what I am talking about and whether C++ can do what I'm trying to do? I'm not sure how.
Im trying to write a program that reads in strings and decides if the 1st one is repeated. I cant figure out how to store the first string into a variable, and compare that variable to the rest of the inputted strings.
Code:
#include <strings.h>
#include <stdio.h>
int main () {
//Declared variables
int i;
}
[code]....