Solved

simple stack and string prog's

Posted on 2003-11-01
6
378 Views
Last Modified: 2010-04-02
i need to know how to work with built-in stacks and strings, i 2 progs r below with their error messages. so plz if u can tell me y they're not working:


#include <iostream.h>
#include <stack.h>

int main()
{
      stack stk;
      for(int i=0; i<5; i++)
            stk.push(i*20%6);

      while(!stk.empty())
      {
            cout<<stk.top()<<endl;
            stk.pop();
      }

      return 0;
      
}

--------------------Configuration: test - Win32 Debug--------------------
Compiling...
main.cpp
c:\c++\test\main.cpp(2) : fatal error C1083: Cannot open include file: 'stack.h': No such file or directory
Error executing cl.exe.

test.exe - 1 error(s), 0 warning(s)


----------------------------------
#include <iostream.h>
#include <string.h>

int main()
{
      string a;
      cin>>a;
      cout<<a<<endl;

      return 0;
      
}

--------------------Configuration: test - Win32 Debug--------------------
Compiling...
main.cpp
c:\c++\test\main.cpp(6) : error C2065: 'string' : undeclared identifier
c:\c++\test\main.cpp(6) : error C2146: syntax error : missing ';' before identifier 'a'
c:\c++\test\main.cpp(6) : error C2065: 'a' : undeclared identifier
Error executing cl.exe.

test.exe - 3 error(s), 0 warning(s)


0
Comment
Question by:minjiber
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9663539
>>c:\c++\test\main.cpp(2) : fatal error C1083: Cannot open include file: 'stack.h': No such file or directory
You mean #include <stack>
using std::stack;
stack<int> stk;

>>#include <string.h>
Try #include <string>
0
 
LVL 2

Author Comment

by:minjiber
ID: 9664146
thanx n_fortynine,

the stack prob worked, but the string didnt. i would appreciate it if u send me an example, simple one like inputing a string and printing it.
0
 
LVL 8

Expert Comment

by:Exceter
ID: 9664289
>> the stack prob worked, but the string didnt.

Try including <string>. For example,

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
    string st;

    cin >> st;
    cout << st << endl;

    return 0;
}

Cheers!
Exceter
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Accepted Solution

by:
n_fortynine earned 20 total points
ID: 9664582
>> the stack prob worked, but the string didnt.
What you are missing is, like Exceter mentioned, using namespace std;
If you don't want to write that, try writing
using std::string;
or put std::string instead of string whenever you declare a string variable.
0
 
LVL 8

Expert Comment

by:Exceter
ID: 9664870
Also, you shouldn'tbe using the .h extensions because these files are non-standard. Just use the same names without he .h part, as I did in my example.
0
 
LVL 2

Author Comment

by:minjiber
ID: 9691820
thanx guys, boths the probs worked.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question