Welcome to Dream.In.Code
Become a C++ Expert!

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




Error c2601 'count sentences':local function definitions illeg

 
Reply to this topicStart new topic

Error c2601 'count sentences':local function definitions illeg

gator6688
30 Oct, 2007 - 03:40 PM
Post #1

D.I.C Head
**

Joined: 23 Sep, 2007
Posts: 84


My Contributions
Does anyone know why I am receiving this error?

CODE
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    string str;
    int count=0, i;

    cout << "Enter some sentences: ";
    getline(cin,str);
    

    int countSentences(string str) \\This is line receiving error!
    {
        
        for(i=0;i<str.length();i++)
        {
            if(str.at(i)=='.'||str.at(i)=='?'||str.at(i)=='!')
                count++;
        }

    return count;
}


User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Error C2601 'count Sentences':local Function Definitions Illeg
30 Oct, 2007 - 03:53 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
You have defined your function countSentences inside the main function. Move the definition so that it is external to the main function, and simply call if from the main function. Also, you've not closed your main function.
User is offlineProfile CardPM
+Quote Post

gator6688
RE: Error C2601 'count Sentences':local Function Definitions Illeg
30 Oct, 2007 - 04:03 PM
Post #3

D.I.C Head
**

Joined: 23 Sep, 2007
Posts: 84


My Contributions
I moved it up and now I have a whole list of errors.

CODE
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int countSentences(string str);

int _tmain(int argc, _TCHAR* argv[])
{
    string str;
    int count=0, i;

    cout << "Enter some sentences: ";
    getline(cin,str);
    
    
            
        for(i=0;i<str.length();i++)
        {
            if(str.at(i)=='.'||str.at(i)=='?'||str.at(i)=='!')
                count++;
        }
    }

    return count;
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Error C2601 'count Sentences':local Function Definitions Illeg
30 Oct, 2007 - 04:18 PM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
That is because you simply moved the declaration, not the definition, and also now have the added trouble of extra braces within your code.
CODE

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int countSentences(string str);

int _tmain(int argc, _TCHAR* argv[])
{
    string str;
    int count=0, i;

    cout << "Enter some sentences: ";
    getline(cin,str);
    cout<<countSentences(str)<<endl;
    return 0;
}

int countSentences(string str)
{
        for(i=0;i<str.length();i++)
        {
            if(str.at(i)=='.'||str.at(i)=='?'||str.at(i)=='!')
                count++;
        }
   return count;
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:40PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month