stack and atoi
-
in the name of god hello why this causes error????? atoi(&stck.top()); error when top from stack and using atoi to change it to integer. #include<iostream> #include<stack> #include<cstdlib> using namespace std; int compute(char* srt); int main(){ int n; cout<<"please enter the size of string and then the string:"<<endl; cin>>n;//for example 3 char *str=new char[n]; cin>>str;//for example 12 compute(str); } int compute(char* str){ stack <char> stck; stck.push(*str); stck.push(*(str+1)); while(!stck.empty()){ int first=atoi(&stck.top()); cout<<"this is first:"<<first<<endl; stck.pop(); int sec=atoi(&stck.top()); // WHY THIS CAUSE ERROR AND RETURN 12 INTEAD OF 1?????????? stck.pop(); cout<<"this is sec:"<<sec<<endl; } return 0; }
-
in the name of god hello why this causes error????? atoi(&stck.top()); error when top from stack and using atoi to change it to integer. #include<iostream> #include<stack> #include<cstdlib> using namespace std; int compute(char* srt); int main(){ int n; cout<<"please enter the size of string and then the string:"<<endl; cin>>n;//for example 3 char *str=new char[n]; cin>>str;//for example 12 compute(str); } int compute(char* str){ stack <char> stck; stck.push(*str); stck.push(*(str+1)); while(!stck.empty()){ int first=atoi(&stck.top()); cout<<"this is first:"<<first<<endl; stck.pop(); int sec=atoi(&stck.top()); // WHY THIS CAUSE ERROR AND RETURN 12 INTEAD OF 1?????????? stck.pop(); cout<<"this is sec:"<<sec<<endl; } return 0; }
You don't need the
&
inside theatoi
function.«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
in the name of god hello why this causes error????? atoi(&stck.top()); error when top from stack and using atoi to change it to integer. #include<iostream> #include<stack> #include<cstdlib> using namespace std; int compute(char* srt); int main(){ int n; cout<<"please enter the size of string and then the string:"<<endl; cin>>n;//for example 3 char *str=new char[n]; cin>>str;//for example 12 compute(str); } int compute(char* str){ stack <char> stck; stck.push(*str); stck.push(*(str+1)); while(!stck.empty()){ int first=atoi(&stck.top()); cout<<"this is first:"<<first<<endl; stck.pop(); int sec=atoi(&stck.top()); // WHY THIS CAUSE ERROR AND RETURN 12 INTEAD OF 1?????????? stck.pop(); cout<<"this is sec:"<<sec<<endl; } return 0; }
khomeyni wrote:
int n; cout<<"please enter the size of string and then the string:"<>n;//for example 3 char *str=new char[n]; cin>>str;//for example 12
This is fragile (what if the user inserts a longer string?).
khomeyni wrote:
int compute(char* str){ stack stck; stck.push(*str); stck.push(*(str+1)); while(!stck.empty()){ int first=atoi(&stck.top()); cout<<"this is first:"< This is a mess. The function is confident that <code>(str+1)</code> is a valid address. That may be not true. If you need to convert each single character into an integer then don't use <code>atoi</code>. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
khomeyni wrote:
int n; cout<<"please enter the size of string and then the string:"<>n;//for example 3 char *str=new char[n]; cin>>str;//for example 12
This is fragile (what if the user inserts a longer string?).
khomeyni wrote:
int compute(char* str){ stack stck; stck.push(*str); stck.push(*(str+1)); while(!stck.empty()){ int first=atoi(&stck.top()); cout<<"this is first:"< This is a mess. The function is confident that <code>(str+1)</code> is a valid address. That may be not true. If you need to convert each single character into an integer then don't use <code>atoi</code>. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
in the name of god hello why this causes error????? atoi(&stck.top()); error when top from stack and using atoi to change it to integer. #include<iostream> #include<stack> #include<cstdlib> using namespace std; int compute(char* srt); int main(){ int n; cout<<"please enter the size of string and then the string:"<<endl; cin>>n;//for example 3 char *str=new char[n]; cin>>str;//for example 12 compute(str); } int compute(char* str){ stack <char> stck; stck.push(*str); stck.push(*(str+1)); while(!stck.empty()){ int first=atoi(&stck.top()); cout<<"this is first:"<<first<<endl; stck.pop(); int sec=atoi(&stck.top()); // WHY THIS CAUSE ERROR AND RETURN 12 INTEAD OF 1?????????? stck.pop(); cout<<"this is sec:"<<sec<<endl; } return 0; }
atoi()
expects achar*
rather than achar
."Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons