Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,676 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,199 people online right now. Registration is fast and FREE... Join Now!




Error msg in Code

 
Reply to this topicStart new topic

Error msg in Code

ShangKiShaq
post 1 Sep, 2007 - 06:52 PM
Post #1


New D.I.C Head

*
Joined: 26 Aug, 2007
Posts: 8


My Contributions


I cannot figure out where I am going wrong with this code. I get two error msgs but not sure where to begin to fix them. Please help.

(11) : error C2449: found '{' at file scope (missing function header?)
(13) : error C2059: syntax error : '}'



CODE
# include <stdio.h>

void Mystery(char *Str)
{
    if (*Str != '\0');
    {
        Mystery(Str + 1);
        printf("%s\n",Str);
    }
}
Mystery("abcd");
User is offlineProfile CardPM

Go to the top of the page

William_Wilson
post 1 Sep, 2007 - 07:50 PM
Post #2


lost in compilation

Group Icon
Joined: 23 Dec, 2005
Posts: 3,970



Thanked 15 times

Dream Kudos: 3275

Expert In: Java, C, Javascript

My Contributions


i'm guessing it's that it should be:
#include <stdio.h>

remove the space

also you need to have a main method, or it will not do anything.
User is offlineProfile CardPM

Go to the top of the page

Bench
post 2 Sep, 2007 - 02:47 AM
Post #3


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 602



Thanked 10 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


whitespace should be allowed between # and include (It looks a bit odd though). You certainly do need a main function however.


Be careful of the semicolon after the if statement, which will stop your if condition having any effect on the code which follows it
CODE
if (*Str != '\0');


This post has been edited by Bench: 2 Sep, 2007 - 02:49 AM
User is offlineProfile CardPM

Go to the top of the page

ShangKiShaq
post 2 Sep, 2007 - 06:43 AM
Post #4


New D.I.C Head

*
Joined: 26 Aug, 2007
Posts: 8


My Contributions


QUOTE(ShangKiShaq @ 1 Sep, 2007 - 07:52 PM) *

I cannot figure out where I am going wrong with this code. I get two error msgs but not sure where to begin to fix them. Please help.

(11) : error C2449: found '{' at file scope (missing function header?)
(13) : error C2059: syntax error : '}'



# include <stdio.h>

void Mystery(char *Str)
{
if (*Str != '\0');
{
Mystery(Str + 1);
printf("%s\n",Str);
}
}
Mystery("abcd");

I have tried several types of main i.e.

int main()
void main ()
void main (void)
main()
None seem to work. Is there others that I am missing? Thank you for the info on the semicolon. It originally was not there but I had an error msg that said the ; was missing. When I inserted it the error msg went away. Any help on what else I am missing or doing wrong.

Thnak you guys for your generous help.

User is offlineProfile CardPM

Go to the top of the page

Bench
post 2 Sep, 2007 - 07:14 AM
Post #5


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 602



Thanked 10 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


You were OK first time with int main(). (The others may work on some compilers, but int main() is standard & portable).

int main() is always the entry point of a C or C++ program - so any code you wish to run needs to start there somehow.

Put your call to Mystery("abcd") inside the curly brackets of the main function - and your program should compile OK.
CODE
int main()
{
    Mystery("abcd");
}


This post has been edited by Bench: 2 Sep, 2007 - 07:15 AM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:29AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month