Re: [Dev-C++] getting unexpected results
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Graham D. <g.d...@nt...> - 2003-07-30 17:59:02
|
And now, written in C++ (no mucking about with arrays, pointers or
new/malloc):
#include <iostream> // NB: _not_ iostream.h
#include <string>
#include <vector>
using namespace std;
class str_misc
{
public:
istream& read(istream& is)
{
is >> str;
return is;
}
ostream& write(ostream& os) const
{
os << str;
return os;
}
private:
string str;
};
istream& operator>>(istream& is, str_misc& s)
{
return s.read(is);
}
ostream& operator<<(ostream& os, const str_misc& s)
{
return s.write(os);
}
int main()
{
vector<str_misc> str_ex(12);
cin >> str_ex[0];
cout << str_ex[0] << " Index = 0" << endl;
cin >> str_ex[1];
cout << str_ex[1] << " Index = 1" << endl;
cout << str_ex[0] << " repeat Index = 0" << endl;
cout << str_ex[1] << " repeat Index = 1" << endl;
system("PAUSE");
return 0;
}
dawood wrote:
> Can anybody let me know, why the below code is not working correctly.
>
> #include <iostream.h>
> #include <stdlib.h>
>
> int main()
> {
> struct str_misc {
> char* cstr;
> };
> str_misc str_ex[12];
> char* buf;
>
> cin>>buf;
> str_ex[0].cstr= buf;
> cout<<str_ex[0].cstr<<"index=0"<<'\n';
>
> cin>>buf;
> str_ex[1].cstr= buf;
> cout<<str_ex[1].cstr<<"index=1"<<'\n';
>
> cout<<str_ex[0].cstr<<" repeat index=0"<<'\n';
> cout<<str_ex[1].cstr<<" repeat index=1"<<'\n';
>
> system("PAUSE");
> return 0;
> }
>
>
>
> -------------------------------------------------------
> This SF.Net email sponsored by: Free pre-built ASP.NET sites including
> Data Reports, E-commerce, Portals, and Forums are available now.
> Download today and enter to win an XBOX or Visual Studio .NET.
> http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|