Solved

simple stack and string prog's

Posted on 2003-11-01
6
376 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

756 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