Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. stack and atoi

stack and atoi

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelptutorialquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    khomeyni
    wrote on last edited by
    #1

    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; }

    _ CPalliniC D 3 Replies Last reply
    0
    • K khomeyni

      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; }

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      You don't need the & inside the atoi function.

      «_Superman_» I love work. It gives me something to do between weekends.
      Microsoft MVP (Visual C++)

      1 Reply Last reply
      0
      • K khomeyni

        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; }

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        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 testa che avete, signor di Ceprano?

        K 1 Reply Last reply
        0
        • CPalliniC CPallini

          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]

          K Offline
          K Offline
          khomeyni
          wrote on last edited by
          #4

          thanks i will take in mind your advises. thanks. valhamdolelah.

          1 Reply Last reply
          0
          • K khomeyni

            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; }

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            atoi() expects a char* rather than a char.

            "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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups