Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

simple stack and string prog's

Posted on 2003-11-01
6
Medium Priority
?
380 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Accepted Solution

by:
n_fortynine earned 80 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

Technology Partners: 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

662 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