C++ :: Get Value From String Containing Variable Name?
Nov 2, 2014
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.
View 5 Replies
ADVERTISEMENT
Sep 8, 2013
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.)"
View 5 Replies
View Related
Aug 10, 2014
How to change an enum type variable to a string type variable?
View 2 Replies
View Related
Feb 11, 2013
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]....
View 3 Replies
View Related
Feb 1, 2013
The while loop is increased by i++ each time. In the commented line I want to express.When:
i = 1 , player1.cards[j] = random;
i = 2 , player2.cards[j] = random;
void cardScramble()
{
int random;
int i = 1;
while (i <= 4)
[code]....
I tried to define it or manipulate it as a string but didn't work.
View 2 Replies
View Related
May 12, 2013
I have defined a class in a header file; just the class, no templates involved. I have a program where I'm reading in data in string format. Each string consists of a word, a delimiter, and a variable name. Example:
cajun/mustard
I want to take that string and make it the variable name of that class type. It would be implemented along the lines of:
Code:
string str;
//read/process string here, get:
str = "mustard";
createName(str);
//pass string to creator function When the function is called, I should get the variable:
Class mustard;
Thing is, I'm not supposed to know beforehand what the variable names are, only that I create them as they are read in. It could be mustard, it could be Maynard_James_Keenan, it could even be bazinga.
My problem is, what do I do for createName()? I've looked into the concepts of pairing, Factory implementation, and maps, but I don't think they answer my question.
(P.S. if I run into the same variable name being read in twice, what steps can I take to make sure that a duplicate variable isn't created? Do I need to add in code, or does the compiler know to watch for multiple variables of the same name?)
View 6 Replies
View Related
Feb 12, 2014
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.
View 3 Replies
View Related
Apr 11, 2014
I would like to convert a string variable to float.When acquiring data from text file, I use:
while( getline( ifs, temp )) {
numberOfFeatures++;
vecteur.clear();
string::size_type stTemp = temp.find(separateur);
number_descriptorEntries=0;
[code].....
the matrix that I obtain is of type vector<Vec> where Vec is a vector of strings.I want to convert each element of matrixe to a float.
View 3 Replies
View Related
Oct 7, 2014
Like if the user enters "38 F" how do I take out the 38 only and store it in a variable? So far my program goes like this:
string text;
double temperature;
cout << "Enter temperature: ";
getline(cin, text); // user enters 38 F
temperature = text // store 38 from string into double
View 1 Replies
View Related
Feb 20, 2013
I am having trouble finding the solution to printing the contents of a variable. Here is my code:
void OnSize(HWND hwnd, UINT flag, int width, int height) {
std::wstringstream ss (std::wstringstream::in | std::wstringstream::out);
ss << flag;
std::wstring myStr = ss.str();
MessageBoxW(hwnd,myStr, L"Window Resized",NULL);
}
Note, I have #defined UNICODE at the beginning. The compiler tells me this for myStr.
std::wstring myStr
Error: No suitible conversion function from "std::wstring" to "LPCWSTR" exits
View 2 Replies
View Related
Jun 18, 2013
I have a problem that states: age is an int variable and name is a string.
What are the values of age and name after the following input statements execute.
cin << age;
getline(cin, name);
if the input is:
a. 23 Lance Grant
b. 23 Lance Grant
Ok I have been fooling around with this book all day. My instructer wants us to build the program instead of just saying
a = 23 Lance Grant
How to input B. I mean when I write the program I do not know how to ask for two lines for one input.
View 1 Replies
View Related
Apr 16, 2015
Is this possible in c++:
Code:
string s = R"(some rand string)" + argv[4] + R"(some rand string1)";
if it isn't, what is the best way to do this?
View 11 Replies
View Related
Dec 25, 2014
What I'm trying to do is to assign the length of a string to an integer variable. This is what i tried, it did not work.
Code:
printf("Enter a string
");
fgets(test_pass, 30, stdin);
strcpy(x,(strlen(test_pass)));
printf("%d", x);
This was just a test, and it did not compile. How to do this?
View 4 Replies
View Related
Feb 11, 2014
I am trying to read a tab delimited file containing 8 columns and store each element of the column as an 1-dimensional array. Though the program prints the strings correctly as in the commented printf but it doesn't give me first_col[0] value and so on. My code is as follows:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main() {
FILE *int_file1;
int BUF = 1024;
[Code]...
The inputfile test.txt has the following elements:
34932 13854 13854 2012-01-07
172098 49418 53269 2012-01-07
-
-
-
View 7 Replies
View Related
Mar 6, 2015
I need to create dynamic string by given format(%d,%s,%f,%lf,%c) using variable number of arguments in function. This code gives me an error(main.exe has stopped working):
Code:
#include<stdio.h>
#include<stdarg.h>
#include<string.h>
#include<stdlib.h>
char *form(char *format,...);
char *form(char *format,...)
[Code]...
I assume the error is in functions(itoa,fcvt,ecvt).
View 1 Replies
View Related
Jun 15, 2013
How to use string variable in windows form application?
I'm using Microsoft Visual C++ 2010 Express And where to include libraries?
View 7 Replies
View Related
Oct 13, 2014
I need to do this simple Battleship game project for my C++ class. Here is my current code so far:
#include <iostream>
#include <array>
#include <string>
using namespace std;
enum BShip{ EMPTY, BTLSHIP, HIT, MISS };
const int BSIZE = 6;
const int BTLSHIP_SIZE = 4;
class Battleship {
private:
array<array<Bship, BSIZE>, BSIZE> sea, hits;
[code]....
For some reason, I'm getting errors on the string and enum declarations.
View 10 Replies
View Related
Mar 14, 2015
I need to create dynamic string by given format(%d,%s,%f,%lf,%c) using variable number of arguments in function. This code gives me an error(main.exe has stopped working):
#include<stdio.h>
#include<stdarg.h>
#include<string.h>
#include<stdlib.h>
char *form(char *format,...);
char *form(char *format,...)
[Code] ....
I assume the error is in functions(itoa,fcvt,ecvt).
View 2 Replies
View Related
Jun 4, 2014
Ii made one string variable and called it email_confirm. I used cin to get the users email but i don't know to check if the email is real.(without being connected to the internet).
For example,in PHP you can use ereg() or eregi() to see if the email is real, I mean you can see how many characters the email_confirm is made off or how many characters is used after the @ or . is used. is there any functions for this?
View 5 Replies
View Related
Mar 30, 2014
I have program where i have to check to see if file exist, if it does not then it needs to be created. SO I have a read file that works fine, if th efile exist it reads whats in it, if it does it says the file does not exist. Now Im trying to creata function that creates the file if it doesnt exist. so in my read function when the person enters the name of the file to be checked for..I pass that name to a variable called name..Hoping that I could then pass it into my create file function if it does not exist..and use that variable to pass the name they entered into the createfile array..called filename.. but I am having trouble because i get error when i try to pass from a string name to char array.. even when I change the varialbe name to char, or char [256] it will not work.. I try to fing a way to convert th string to a char using the strncopy function but still no dice..here the code i have for the createfile funciton
void CreateNew(ofstream & FileNew)//Create file
{
char filename[256];
string name;
[Code].....
How can i do this without having to ask the person to enter the file name twice..
View 12 Replies
View Related
Nov 14, 2013
When a declare a string e.g.
Code:
char str[30]; as a global variable, the srting is initialized to NULL.
But if I declare char str1[30] INSIDE main(), then the string has garbage inside.... Why this happens??
E.g. the following code snippet...
Code:
#include <stdio.h>
char str[50];
int main(){
char str1[50];
[Code] ....
View 4 Replies
View Related
Jul 10, 2014
I'm making a .json loader for a project that I'm working on, and to simplify things, I decided to make all the root attributes named in a separate file. Here's my problem: my loading function, Add(const char* id), works just fine when I pass it a string literal.
However, when I use a function that iterates through a vector list, even with the exact same definitions as the literal, it returns the error: std::out_of_range at memory location 0x0026fb30
I've stepped through it with the VS2010 debugger about a hundred times, checked against memory locations, and just have done everything I can think of, all to no avail..
The code, with data-checking omitted:
The std::map I'm adding to:
static std::map<const char*, Item*> *s_mItems;
Initialized as std::map<const char*, Item*> *Item::s_mItems;
Add() Function (Works by itself with a literal):
static bool Add(const char* id) {
...
std::string name = node.Get("name").ToString();
std::string desc = node.Get("description").ToString();
int rarity = StrToRarity(node.Get("rarity").ToString());
[Code] ....
AddList() function, where the program always breaks:
static void AddList(std::vector<std::string> list) {
for(std::vector<std::string>::iterator it = list.begin(); it != list.end(); it++) {
Add(it->c_str());
}
}
View 3 Replies
View Related
May 12, 2014
My intent was to convert the string variable for the year to an integer data type. The code compiles but now cannot run on my system. I'm not sure what's going as to what the program is displaying.
Objective: Prompt the user for two years. Print all Comedy movies that were released between those two years.
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <cctype>
using namespace std;
struct Movie {
string name;
[Code] .....
View 2 Replies
View Related
May 10, 2013
I would like to convert a float or double variable into unsigned char variable and back.
float number_float = 23.453f;
unsigned char* number_char = (unsigned char*)malloc(sizeof(float));
number_char = reinterpret_cast<unsigned char*> (&number_float);
float* number_float_0 = reinterpret_cast<float*>(&number_char);
I am not getting the same value back.. why?
View 2 Replies
View Related
May 1, 2013
I am having trouble compiling my interface. I am trying to store a reference variable as a member variable of the interface object. Compiler says that the variable has not be initiated correctly.
LCD inherits from VisualInterface which is expecting a DisplayDriver object to be passed in (DisplayDriver is another interface, but thats not important).
I pass the displayDriver object in when LCD is instantiated in maininterfaces.zip
I was pasing it before as a pointer but was told that this could cause me problems with memory leaks and a reference was better, but now I cant seem to get it to compile.
View 11 Replies
View Related
Mar 2, 2013
This problem is best explained by looking at its output below. I stored 15 different values but when retrieved some are wrong.
#include <iostream>
using namespace std;
class A {
public:
int ** pointer;
[Code] ....
I need to store and retrieve the 15 different values correctly. How to solve this? The main thing is, I need to store the pointer in class A objects.
View 6 Replies
View Related