1

I have a student's project for school, I'm mostly programming in PHP, so C++ is not a my strong side :) Please let me know, is it possible to change variable which I mention in cin string? Maybe my code below will be more understandable:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

string A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, R, Q, S, T, U, V, W, X, Y, Z;
string instrukcja;
string krok[3];
int i = 0;

string MAKENULL()
{
    return " ";
}

void WRITE(string x)
{
    cout << x;
}

int main()
{   
    // get instruction Begin
    char s[10];
    scanf("%10[0-9a-zA-Z ]", s);
    istringstream iss(s);
    while(iss)
    {
        string sub;
        iss >> sub;
        krok[i] = sub;
        i++;
    }
    // get instruction End


    if (krok[0] == "MAKENULL")
    {
        "" Variable in krok[1] "" = MAKENULL();
    }

    if (krok[0] == "WRITE")
    {
        WRITE("" Variable in krok[1] "");
    }

    if (krok[0] == "OPPOSITE")
    {
        OPPOSITE("" Variable in krok[1] "", "" Variable in krok[2] "")
    }

}

In code above I will do something like ADD A B, and A should became A => A+B (one string stick to the other). Main question is, can I point a variable to change in scanf?

4
  • Its not really clear what you want to do. You want to concatenate two strings? and what is OPPOSITE() ? Commented Apr 11, 2016 at 9:35
  • Maybe I will try to explatin whole project. There will be registers (A-Z), and this program will only get one input (in loop) with instruction (like ADD A, Z; OPPOSITE A, W; WRITE A etc...) and that I need now is can I point in scanf/cin a specific variable to change? Commented Apr 11, 2016 at 9:41
  • if I understand you correctly, you have to provide some mapping from the string representations ("A","B"...) to the variables holding the values (ie. the string A,B,...). You could use a std::map<std::string,std::string> where the key is the string that you get from scanf and the value (i.e. the second string in the map) is the value that you want to modify Commented Apr 11, 2016 at 9:43
  • Example: if I write CREATE A qwerty, A should become qwerty (A = "qwerty"), if I write then OPPOSITE A, A should become ytrewq (A = "ytrewq") Commented Apr 11, 2016 at 9:57

1 Answer 1

2

If I understood correctly then instead of keeping variables as now A,B,C,... you need a map container:

#include <map>
// ...
std::map<std::string, std::string> varMap;

where key is the variable name: A,B,C and value is a its value.

Then you can use your current way of reading, when you assign value to variable A, then you use:

varMap["A"] = newValue;

and when you read it you also use std::string value = varMap["A"], but be carefull here, if "A" does not exist in map it will be created and value initialized (default constructor is called - so that it will be empty string). To check if key exsists use if (varMap.find("A") == varMap.end()) {/*no such variable*/}

Another thing is that you istringstream usage is wrong you should check if something was read before using it, the proper idiom is:

istringstream iss(s);
string sub;
while(iss >> sub)
{
    krok[i] = sub;
    i++;
}

[edit]

What you probably had in mind was a possibility to use php like variable variables concept, or reflection from java and other languages. There is no such thing in c++, and the recomended way - as also in php is to use associative container like map.

Sign up to request clarification or add additional context in comments.

3 Comments

It might work... I will try and give feedback :) Thank you!
If I understand correctly, it is some kind of array, but instead of numbers, there are chars, right?
Associative container - but you might think of this as an array with std::string as indexes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.