2012-03-12 24 views
0

私は現在、main()で初期化された文字列を扱っています。何らかの理由で、グローバル化しようとするとフリークアウトします(文字以外の文字列になります)。私はプログラムで宣言された関数にこの変数にアクセスすることができるかどうか疑問に思っています...関数自体はmainから呼び出される関数またはmainから呼び出される関数のみですが、main()が達成された。私もそれを保持するクラスを作り、手で関数に私はそれを使用するたびに変数を渡し、前方宣言を検討してきたC++:関数からメイン変数にアクセスする最も簡単な方法は?

string getCommand(int input_pos,string inputstring) 
{ 
int temp_paren=0; 
int begin_pos = stdinstring.rfind("(",input_pos); 
int len = 0; 
while (temp_paren>0 && len < 10) 
{ 
if (stdinstring.substr(begin_pos+len,1)=="(") {temp_paren++;} 
if (stdinstring.substr(begin_pos+len,1)==")") {temp_paren--;} 
len++; 
} 
return stdinstring.substr(begin_pos,len); 
} 

int main(void) { 

string stdinstring = ""; 
} 

:ここ

は、関連するコードのスキニーバージョンです変数...この問題を解決する最も単純な方法は何でしょうか?理想的には、私はそれをグローバル変数にして、それがどこで、なぜ間違っているのかを理解するのに十分な時間があるでしょうが、これを行うだけです。すべての助けをありがとう。


EDIT:興味がある人々のために:これは私がしようとすると、文字列がグローバルにするとどうなるかです。文字のすべてが(それはでも、目に見えるかどう知らん)


編集編集「」になる:さて、ここに私のコードのより完全なバージョンです。また、私は上記の貼り付けしようとしたシンボルは「0 0 0 1」のように見えるもののボックスであった(ASCII文字コード?)

#include<iostream> 
#include<cstdio> 
#include<cstring> 
#include<cstdlib> 
#include<cmath> 
#include<stdint.h> 
//#include<regex> 

#define PI M_PI 
#define VERBOSE 1 
using namespace std; 

string stdinstring; 

template <class dataclass> 
struct linkm { 
    dataclass value; 
    linkm *next; 
}; 

template <class dataclass> 
class linklist 
{ 
    public: 
    linklist() 
     {top = NULL;} 
    ~linklist() 
     {} 
    void push(dataclass num) 
     { 
     linkm<dataclass> *temp = new linkm<dataclass>; 
     temp->value = num; 
     temp->next = top; 
     top = temp; 
     } 
    dataclass pop() 
     { 
     if (top == NULL) 
     return 0; 
     linkm<dataclass> * temp; 
     temp = top; 
     dataclass value; 
     value = temp->value; 
     top = temp->next; 
     delete temp; 
     return value; 
     } 
    bool isEmpty() 
     { 
     if (top == NULL) 
     return 1; 
     return 0; 
     } 
    private: 
    linkm<dataclass> *top; 
}; 

double evaluateExpression(string expression) 
{ 
// sample expression : (* (/ a 1) (+ b 2)) 
    if (expression.substr(0,2)=="(+") {cout << "add found"<<endl;} 
else if (expression.substr(0,2)=="(-") {cout << "sub found"<<endl;} 
else if (expression.substr(0,2)=="(*") {cout << "mult found"<<endl;} 
else if (expression.substr(0,2)=="(/") {cout << "div found"<<endl;} 
else if (expression.substr(0,2)=="(sin") {cout << "sin found"<<endl;} 
else if (expression.substr(0,2)=="(cos") {cout << "cos found"<<endl;} 
else {cout << "Error: invalid operation";} // or is it just a number? 
} 

string getCommand(int input_pos,string inputstring) 
{ 
int temp_paren=0; 
int begin_pos = stdinstring.rfind("(",input_pos); 
int len = 0; 
while (temp_paren>0 && len < 10) 
{ 
if (stdinstring.substr(begin_pos+len,1)=="(") {temp_paren++;} 
if (stdinstring.substr(begin_pos+len,1)==")") {temp_paren--;} 
len++; 
} 
return stdinstring.substr(begin_pos,len); 
} 

class symContainer 
{ 
public: 
string index[500]; 
float value[500]; 
int currindex; 

symContainer() { currindex = 0 ; } 

void add(string id,float invalue) 
{ 
index[currindex] = id; 
value[currindex] = invalue; 
currindex++; 
} 

float get(string id) 
{ 
int i=0; 
while (i<currindex) 
{ 
if(id==index[i]) {return value[i];} 
i++; 
} 
cout << "Invalid input - an unassigned symbol was requested:" << id << endl; 
exit(2); 
} 

}; 


struct transform { int type; double arguments[4]; } ; 
struct point { double x; double y; } ; 
struct drawing { linklist<point> points; linklist<transform> transforms; void applyTransforms(linklist<transform> * trsptr) {} } ; 
struct group : public drawing { linklist<int> drawings; void transform(linklist<transform> * trsptr) {} } ; 
struct symbol {string index; double value;}; 

char getNext() { //for the lookaround function. has hard-filters, like replacing newlines/tabs with spaces 
char temp = getchar(); 
if (temp=='\n') {temp=' ';} 
if (temp=='\t') {temp=' ';} 
return temp; 
} 

int main(void) { 

stdinstring="a"; 

char command[20], args[2048]; 
int commandindex=0;  //'i' for what command we're on 
int stdinsize=2; 
double argsArray[8]; 
bool filled=0; 
int parenlevel = 0; 
symContainer symbol; 

//1 is the character that will be written at the end of each loop. 0 and 2 are 1 char ahead/behind, respectively 
char c_lookaround[2]; 
c_lookaround[0]=NULL; 
c_lookaround[1]=getNext(); 
c_lookaround[2]=getNext(); 

unsigned long i=0; 

bool write=0; 
while(c_lookaround[2] != EOF) 
{ 
write=1; 
// Lookaround logic goes here. (Clearing duplicate whitespaces, newlines, and the like) 

while (      c_lookaround[1]==' ' && c_lookaround[2]==' ') {c_lookaround[2]=getNext();} 
while (c_lookaround[0]=='(' && c_lookaround[1]==' '       ) {c_lookaround[1]=c_lookaround[2]; c_lookaround[2]=getNext();} 
while (      c_lookaround[1]==' ' && c_lookaround[2]==')') {c_lookaround[1]=c_lookaround[2]; c_lookaround[2]=getNext();} 
while (c_lookaround[0]==NULL && c_lookaround[1]==' '       ) {c_lookaround[1]=c_lookaround[2]; c_lookaround[2]=getNext();} 
//while (c_lookaround[0]==')' && c_lookaround[1]==' ' && c_lookaround[2]=='(') {c_lookaround[1]=c_lookaround[2]; c_lookaround[2]=getNext();} 
//while (c_lookaround[0]==')' && c_lookaround[1]==' ' && c_lookaround[2]=='\0') {cout<<"aa";c_lookaround[1]=c_lookaround[2]; c_lookaround[2]=getNext();} 

if (c_lookaround[0]=='(' && c_lookaround[1]==':' && c_lookaround[2]=='=') 
{ 
    getCommand(i,stdinstring); 
} 

//Determine current parentheses level 
if (c_lookaround[1] == '(') { parenlevel++;} 
if (parenlevel==0) {write=0;} 
if (c_lookaround[1] == ')') { parenlevel--;} 

//Write the character 
if (write) {stdinstring.push_back(c_lookaround[1]);} 
cout << stdinstring<< endl; 

//Advance the tape! 
i++; 
c_lookaround[0]=c_lookaround[1]; 
c_lookaround[1]=c_lookaround[2]; 
c_lookaround[2]=getNext(); 
} 

stdinsize = i; 

} 
+1

どのようにgetCommandを呼び出しますか?どのように文字列を取得していますか? – Collin

+2

あなたの質問が分かっていれば、その文字列を 'inputstring'パラメータ(関数のシグネチャは必要ですが何もしません)として' getCommand() 'に渡す必要があります。 –

+0

Oh whoops。私はこのコードを調整していたので、署名にそれを残しておく必要があります。前述のように、私は関数に文字列を追加することを検討しましたが、関数が実際に読む文字列であることを見て、私はその解決策があまり好きではありません。 (私はそれが壊れていないと思う...) – xyzzy

答えて

1

あなたはmain()からgetCommand()を呼び出している場合は、変数を渡すことができるはずです。あなたはどこか別の場所からgetCommand()を呼び出す場合

int main(void) { 
    string stdinstring = ""; 
    string answer = getCommand(0, stdinstring); 
} 

あなたはその後main()とからgetCommand()にその関数に変数を渡す必要があります。また、あなたはそれをグローバルにすることができるはずですが、コードなしで私はなぜあなたができなかったのかわかりません。

//stdinstring = "a" in the case (how is this set to anything but it? 
string getCommand(int input_pos,string inputstring) 
{ 
    int temp_paren=0; 
    int begin_pos = stdinstring.rfind("(",input_pos); //This equals -1 
    int len = 0; 
    while (temp_paren>0 && len < 10) //There is no (so the loop terminates with len=10 
    { 
     if (stdinstring.substr(begin_pos+len,1)=="(") {temp_paren++;} 
     if (stdinstring.substr(begin_pos+len,1)==")") {temp_paren--;} 
    len++; 
    } 
    return stdinstring.substr(begin_pos,len); //returns the first 10 chars of stdinstring which would be "a" 
} 
+0

私はコードを貼り付けるが、それはかなり長いです、と私はどの部分が関連するかもしれないか分からない。私は試してそれをカットしよう、それは約200行です。 – xyzzy

+0

問題の変数を含むコードを必ず貼り付けてください。 – twain249

+0

私の友人(似たようなプロジェクトに取り組んでいます)は、 "endl"プレースメントに起因するバグを見つけました。それ以来、私は関連コードかもしれないことに非常に注意してきました。 – xyzzy

関連する問題