Solved

simple stack and string prog's

Posted on 2003-11-01
6
366 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
  • 2
  • 2
  • 2
6 Comments
 
LVL 4

Expert Comment

by:n_fortynine
Comment Utility
>>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
Comment Utility
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
Comment Utility
>> 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Accepted Solution

by:
n_fortynine earned 20 total points
Comment Utility
>> 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
Comment Utility
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
Comment Utility
thanx guys, boths the probs worked.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now