beginner vc++ q (please redirect me if this is the wrong place) 
Author Message
 beginner vc++ q (please redirect me if this is the wrong place)

Hello

There are a few problems, see my inline comments.

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

using namespace std;

char* monkey();        // Prototype for monkey()

 int main()
{
    char sout[49152];

    strcpy( sout, "\tHello world from\n " );
    strcat( sout, monkey() );    // Need the brackets after monkey()

    ofstream fout("filename.txt");
    fout << sout << endl;
    fout.close();

     return 0;

Quote:
}

char* monkey()    // Needs to return a null-terminated string
{
    char sout[20];
    strcpy( sout, "planet earth");    // Should be strcpy(), not strcopy()
    return sout;

Quote:
}

Even after correcting these things, there is a further problem which will
bite you sooner or later.  monkey() is returning a pointer to something
which is on the stack, which means that it will be destroyed at the end of
the monkey() function.

There are at least a couple of solutions:

1. Use a static buffer inside monkey().

char* monkey()    // Needs to return a null-terminated string
{
    static char sout[20];
    strcpy( sout, "planet earth");    // Should be strcpy(), not strcopy()
    return sout;

Quote:
}

2. Have monkey() return an allocated buffer.

char* monkey()    // Needs to return a null-terminated string
{
    char* sout = new char[20];
    strcpy( sout, "planet earth");    // Should be strcpy(), not strcopy()
    return sout;

Quote:
}

This leaves the problem of the allocated buffer needing to be deleted after
use.

I have added microsoft.public.vc.language to the list of newsgroups, since
that would be a better place to continue.  comp.lang.c++ might also be a
place to continue, but no machine or OS specific questions should ever be
asked there, on pain of death :-).

Best Regards
Julian Nicholls



Quote:
> I'm working on a very basic project - purpose is to learn C++. I'm using
> MSVC++ 6.0 Enterprise. If this isn't where I need to be, let me know.
Maybe
> you can help here anyway.

> Ok. What I want to do is create a function to return a string of
characters
> and save it to a file. Ultimately, will build a variety of functions I
will
> pass variables to and create a file. Compiler error is at the end. Looked
> all over the web to try and figure this out before I came here.

> Here's the code I'm starting with. The problem seems to be with the monkey
> function.

> ----

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

> using namespace std;

> int main()
> {
>  char sout[49152];

>  strcpy( sout, "\tHello world from\n " );
>  strcat( sout, monkey );

>  ofstream fout("filename.txt");
>  fout << sout << endl;
>  fout.close();

>     return 0;
> }

> char monkey()
> {
>  char sout[20];
>  strcopy( sout, "planet earth");
>  return sout;
> }

> --- compiler errors

> ...\12\1.cpp(12) : error C2065: 'monkey' : undeclared identifier
> ..\12\1.cpp(23) : error C2373: 'monkey' : redefinition; different type
> modifiers
> ..\12\1.cpp(25) : error C2065: 'strcopy' : undeclared identifier
> ..\12\1cpp(26) : error C2440: 'return' : cannot convert from 'char [20]'
to
> 'char'
>         This conversion requires a reinterpret_cast, a C-style cast or
> function-style cast
> Error executing cl.exe.

> Thanks in advance for your assistance,
> Bob



Tue, 06 Sep 2005 17:27:52 GMT  
 beginner vc++ q (please redirect me if this is the wrong place)
Julian,

Thank you for getting that monkey() off my back. ;)

Bob



Quote:
> Hello

> There are a few problems, see my inline comments.

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

> using namespace std;

> char* monkey();        // Prototype for monkey()

>  int main()
> {
>     char sout[49152];

>     strcpy( sout, "\tHello world from\n " );
>     strcat( sout, monkey() );    // Need the brackets after monkey()

>     ofstream fout("filename.txt");
>     fout << sout << endl;
>     fout.close();

>      return 0;
> }

> char* monkey()    // Needs to return a null-terminated string
> {
>     char sout[20];
>     strcpy( sout, "planet earth");    // Should be strcpy(), not strcopy()
>     return sout;
> }

> Even after correcting these things, there is a further problem which will
> bite you sooner or later.  monkey() is returning a pointer to something
> which is on the stack, which means that it will be destroyed at the end of
> the monkey() function.

> There are at least a couple of solutions:

> 1. Use a static buffer inside monkey().

> char* monkey()    // Needs to return a null-terminated string
> {
>     static char sout[20];
>     strcpy( sout, "planet earth");    // Should be strcpy(), not strcopy()
>     return sout;
> }

> 2. Have monkey() return an allocated buffer.

> char* monkey()    // Needs to return a null-terminated string
> {
>     char* sout = new char[20];
>     strcpy( sout, "planet earth");    // Should be strcpy(), not strcopy()
>     return sout;
> }

> This leaves the problem of the allocated buffer needing to be deleted
after
> use.

> I have added microsoft.public.vc.language to the list of newsgroups, since
> that would be a better place to continue.  comp.lang.c++ might also be a
> place to continue, but no machine or OS specific questions should ever be
> asked there, on pain of death :-).

> Best Regards
> Julian Nicholls



> > I'm working on a very basic project - purpose is to learn C++. I'm using
> > MSVC++ 6.0 Enterprise. If this isn't where I need to be, let me know.
> Maybe
> > you can help here anyway.

> > Ok. What I want to do is create a function to return a string of
> characters
> > and save it to a file. Ultimately, will build a variety of functions I
> will
> > pass variables to and create a file. Compiler error is at the end.
Looked
> > all over the web to try and figure this out before I came here.

> > Here's the code I'm starting with. The problem seems to be with the
monkey
> > function.

> > ----

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

> > using namespace std;

> > int main()
> > {
> >  char sout[49152];

> >  strcpy( sout, "\tHello world from\n " );
> >  strcat( sout, monkey );

> >  ofstream fout("filename.txt");
> >  fout << sout << endl;
> >  fout.close();

> >     return 0;
> > }

> > char monkey()
> > {
> >  char sout[20];
> >  strcopy( sout, "planet earth");
> >  return sout;
> > }

> > --- compiler errors

> > ...\12\1.cpp(12) : error C2065: 'monkey' : undeclared identifier
> > ..\12\1.cpp(23) : error C2373: 'monkey' : redefinition; different type
> > modifiers
> > ..\12\1.cpp(25) : error C2065: 'strcopy' : undeclared identifier
> > ..\12\1cpp(26) : error C2440: 'return' : cannot convert from 'char [20]'
> to
> > 'char'
> >         This conversion requires a reinterpret_cast, a C-style cast or
> > function-style cast
> > Error executing cl.exe.

> > Thanks in advance for your assistance,
> > Bob



Tue, 06 Sep 2005 18:24:04 GMT  
 beginner vc++ q (please redirect me if this is the wrong place)
My pleasure.
Best Regards
Julian N.



Quote:
> Julian,

> Thank you for getting that monkey() off my back. ;)

> Bob



> > Hello

> > There are a few problems, see my inline comments.

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

> > using namespace std;

> > char* monkey();        // Prototype for monkey()

> >  int main()
> > {
> >     char sout[49152];

> >     strcpy( sout, "\tHello world from\n " );
> >     strcat( sout, monkey() );    // Need the brackets after monkey()

> >     ofstream fout("filename.txt");
> >     fout << sout << endl;
> >     fout.close();

> >      return 0;
> > }

> > char* monkey()    // Needs to return a null-terminated string
> > {
> >     char sout[20];
> >     strcpy( sout, "planet earth");    // Should be strcpy(), not
strcopy()
> >     return sout;
> > }

> > Even after correcting these things, there is a further problem which
will
> > bite you sooner or later.  monkey() is returning a pointer to something
> > which is on the stack, which means that it will be destroyed at the end
of
> > the monkey() function.

> > There are at least a couple of solutions:

> > 1. Use a static buffer inside monkey().

> > char* monkey()    // Needs to return a null-terminated string
> > {
> >     static char sout[20];
> >     strcpy( sout, "planet earth");    // Should be strcpy(), not
strcopy()
> >     return sout;
> > }

> > 2. Have monkey() return an allocated buffer.

> > char* monkey()    // Needs to return a null-terminated string
> > {
> >     char* sout = new char[20];
> >     strcpy( sout, "planet earth");    // Should be strcpy(), not
strcopy()
> >     return sout;
> > }

> > This leaves the problem of the allocated buffer needing to be deleted
> after
> > use.

> > I have added microsoft.public.vc.language to the list of newsgroups,
since
> > that would be a better place to continue.  comp.lang.c++ might also be a
> > place to continue, but no machine or OS specific questions should ever
be
> > asked there, on pain of death :-).

> > Best Regards
> > Julian Nicholls



> > > I'm working on a very basic project - purpose is to learn C++. I'm
using
> > > MSVC++ 6.0 Enterprise. If this isn't where I need to be, let me know.
> > Maybe
> > > you can help here anyway.

> > > Ok. What I want to do is create a function to return a string of
> > characters
> > > and save it to a file. Ultimately, will build a variety of functions I
> > will
> > > pass variables to and create a file. Compiler error is at the end.
> Looked
> > > all over the web to try and figure this out before I came here.

> > > Here's the code I'm starting with. The problem seems to be with the
> monkey
> > > function.

> > > ----

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

> > > using namespace std;

> > > int main()
> > > {
> > >  char sout[49152];

> > >  strcpy( sout, "\tHello world from\n " );
> > >  strcat( sout, monkey );

> > >  ofstream fout("filename.txt");
> > >  fout << sout << endl;
> > >  fout.close();

> > >     return 0;
> > > }

> > > char monkey()
> > > {
> > >  char sout[20];
> > >  strcopy( sout, "planet earth");
> > >  return sout;
> > > }

> > > --- compiler errors

> > > ...\12\1.cpp(12) : error C2065: 'monkey' : undeclared identifier
> > > ..\12\1.cpp(23) : error C2373: 'monkey' : redefinition; different type
> > > modifiers
> > > ..\12\1.cpp(25) : error C2065: 'strcopy' : undeclared identifier
> > > ..\12\1cpp(26) : error C2440: 'return' : cannot convert from 'char
[20]'
> > to
> > > 'char'
> > >         This conversion requires a reinterpret_cast, a C-style cast or
> > > function-style cast
> > > Error executing cl.exe.

> > > Thanks in advance for your assistance,
> > > Bob



Tue, 06 Sep 2005 18:26:16 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. please help, where am i wrong??

2. Please look at this code n tell me where i am wrong

3. Am I wrong or the compiler is wrong?

4. Am I wrong or compiler is wrong?

5. Please help a vc beginner (with Class Wizard)

6. Please Help/Redirect - installing VC++

7. Inserting some junk in the place of AM/PM

8. Please I am looking for sample code (C++ BUT NO MFC PLEASE)

9. Please Help! Beginner VC++

10. beginner question -- on redirecting

11. Redirect Output -- Rank Beginner Question

12. Am I in the wrong NG???

 

 
Powered by phpBB® Forum Software