Thread: [Dev-C++] Please help me...why doesn't the code compile?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Jose S. <dyo...@ho...> - 2001-08-10 23:56:14
|
Please help me anyone... :-)
There are actually three files here; main.cpp, module1.cpp, and main.cpp.
I'm trying to separate the function prototypes into a header file, and put
the function body in a source file, since the function is generic enough to
be used in many programs, and I want to make it into a module, but it
doesn't compile... :-(
THANK YOU in advance!!!
//////////////////////////////////////////////////////
// Module : main.cpp
//
// Purpose : Shows the usage of modular functions
//////////////////////////////////////////////////////
#include <iostream.h>
#include "module1.h"
void main()
{
cout << "This is a test\n";
cout << UCase("This is a test of UCase\n");
cout << LCase("This is a test of LCase\n");
}
//////////////////////////////////////////////////////
// Module : module1.cpp
//
// Purpose : Shows the usage of modular functions
//////////////////////////////////////////////////////
#include "module1.h"
//////////////////////////////////////////////////////
// UCase()
char* UCase(char *str)
{
// Convert each char in the string to uppercase
//
int len = strlen(str);
for (int i = 0; i < len; i++)
str[i] = toupper(str[i]);
return str;
}
//////////////////////////////////////////////////////
// LCase()
char* LCase(char *str)
{
// Convert each char in the string to lowercase
//
int len = strlen(str);
for (int i = 0; i < len; i++)
str[i] = tolower(str[i]);
return str;
}
//////////////////////////////////////////////////////
// Module : module1.h
//
// Purpose : Interface for module1.cpp
//////////////////////////////////////////////////////
#ifndef __MODULE1_H__
#define __MODULE1_H__
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// Function prototypes
//
extern char* LCase(char *str);
extern char* UCase(char *str);
#endif //__MODULE1_H__
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
|
|
From: Vassili <v.v...@bt...> - 2001-08-11 16:00:42
|
If your going to use pointers and arrays, always have an address to store
data, ditto for char, int, float,...,etc.
try using something like:-
#include <iostream.h>
#include "module1.h"
void main()
{
// Initialised plus somewhere to store data
char str1[] = "This is a test of UCase";
char str2[] = "This is a test of LCase";
// Pass the address of strings and display the
// modified data, which is the returned address
cout << "This is a test\n"
<< UCase(str1) << endl
<< LCase(str2) << endl;
}
----- Original Message -----
From: "Jose Samonte" <dyo...@ho...>
To: <dev...@li...>
Sent: Saturday, August 11, 2001 12:56 AM
Subject: [Dev-C++] Please help me...why doesn't the code compile?
> Please help me anyone... :-)
> There are actually three files here; main.cpp, module1.cpp, and main.cpp.
> I'm trying to separate the function prototypes into a header file, and put
> the function body in a source file, since the function is generic enough
to
> be used in many programs, and I want to make it into a module, but it
> doesn't compile... :-(
> THANK YOU in advance!!!
>
>
>
> //////////////////////////////////////////////////////
> // Module : main.cpp
> //
> // Purpose : Shows the usage of modular functions
> //////////////////////////////////////////////////////
>
> #include <iostream.h>
> #include "module1.h"
>
> void main()
> {
> cout << "This is a test\n";
> cout << UCase("This is a test of UCase\n");
> cout << LCase("This is a test of LCase\n");
> }
>
> //////////////////////////////////////////////////////
> // Module : module1.cpp
> //
> // Purpose : Shows the usage of modular functions
> //////////////////////////////////////////////////////
>
> #include "module1.h"
>
> //////////////////////////////////////////////////////
> // UCase()
>
> char* UCase(char *str)
> {
> // Convert each char in the string to uppercase
> //
> int len = strlen(str);
> for (int i = 0; i < len; i++)
> str[i] = toupper(str[i]);
>
> return str;
> }
>
> //////////////////////////////////////////////////////
> // LCase()
>
> char* LCase(char *str)
> {
> // Convert each char in the string to lowercase
> //
> int len = strlen(str);
> for (int i = 0; i < len; i++)
> str[i] = tolower(str[i]);
>
> return str;
> }
>
> //////////////////////////////////////////////////////
> // Module : module1.h
> //
> // Purpose : Interface for module1.cpp
> //////////////////////////////////////////////////////
>
> #ifndef __MODULE1_H__
> #define __MODULE1_H__
>
> #include <stdlib.h>
> #include <ctype.h>
> #include <string.h>
>
> // Function prototypes
> //
> extern char* LCase(char *str);
> extern char* UCase(char *str);
>
> #endif //__MODULE1_H__
>
> _________________________________________________________________
> Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|
|
From: Luigi S. <gi...@li...> - 2001-08-12 01:30:31
Attachments:
Jose.zip
|
At 23.56 10/08/01 +0000, you wrote:
Hello Jose!
I tried your code (after storing it in 3 different files) with Dev C++. I
made a project and asked the IDE to compile it: everything seemed fine (no
compilation errors), but when I tried to execute it ... I had troubles! :-)
Unlike me, you had some compilation error, I guess it is because you turned
on some extra warnings, while I used only the "plain" Dev C++ options.
When I tried to compile your program from the command line (see the file
main.mk) I got these kind of errors:
- main should return an integer
- you are passing a "const char *" where a "char *" is expected
Maybe the second kind of error needs some comment.
Your prototype says: "extern char* LCase(char *str);" (by the way the
"extern" is not necessary), so a "char *" is expected. In your test
program, when you call the function by the line: "LCase("This is a test of
LCase\n");", you are handing it a "const char *" (a "string constant" to be
more specific). Usually compilers store string constants in a sort of read
only memory, so when you attempt to modify them you'll get (at least) a run
time error.
I suggest that you modify main.cpp in this way:
- build a "char *" using a constant string, and use hand it to your functions
I've changed your main.cpp according to the above, I also used "C style
strings". Since you are using C++ I suggest you use "C++ style strings"
(see your documentation on the standard library).
Hope this helps.
>Please help me anyone... :-)
>There are actually three files here; main.cpp, module1.cpp, and main.cpp.
>I'm ...
--
gisan
gi...@li... |