function should have a prototype

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sicba2222
    New Member
    • Dec 2007
    • 1

    function should have a prototype

    Hi all,

    I am new to C++...I tried to compile the following program...

    #include<iostre am.h>
    #include<conio. h>
    #include<string >
    #include<stdio. h>

    int main() {
    String mystr;
    cout << "What is your name?";
    getline(cin,mys tr);
    cout << "my name is:" << mystr;
    cout << "What is your favourite team?";
    getline(cin, mystr);
    cout << "My favourite team is " << mystr;
    getch();
    return 0;
    }

    but i am getting the error 'function getline should have a prototype'...I am getting other errors as well apart from this ...but i think if i can add the relevant "#include.. ." i can fix this error..can anyone please tell me what is it for 'getline' function...thnk in advnce...
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by sicba2222
    Hi all,

    I am new to C++...I tried to compile the following program...

    #include<iostre am.h>
    #include<conio. h>
    #include<string >
    #include<stdio. h>

    int main() {
    String mystr;
    cout << "What is your name?";
    getline(cin,mys tr);
    cout << "my name is:" << mystr;
    cout << "What is your favourite team?";
    getline(cin, mystr);
    cout << "My favourite team is " << mystr;
    getch();
    return 0;
    }

    but i am getting the error 'function getline should have a prototype'...I am getting other errors as well apart from this ...but i think if i can add the relevant "#include.. ." i can fix this error..can anyone please tell me what is it for 'getline' function...thnk in advnce...
    It's namespace that you are missing.You can use std::getline or add this line after includes:

    [CODE="cpp"]using namespace std;[/CODE]

    PS:Please use [code=cpp] tags around your c++ code

    Savage

    Comment

    • keerthiramanarayan
      New Member
      • Nov 2007
      • 13

      #3
      Originally posted by sicba2222
      Hi all,

      I am new to C++...I tried to compile the following program...

      #include<iostre am.h>
      #include<conio. h>
      #include<string >
      #include<stdio. h>

      int main() {
      String mystr;
      cout << "What is your name?";
      getline(cin,mys tr);
      cout << "my name is:" << mystr;
      cout << "What is your favourite team?";
      getline(cin, mystr);
      cout << "My favourite team is " << mystr;
      getch();
      return 0;
      }

      but i am getting the error 'function getline should have a prototype'...I am getting other errors as well apart from this ...but i think if i can add the relevant "#include.. ." i can fix this error..can anyone please tell me what is it for 'getline' function...thnk in advnce...
      I would like to add to the above reply. You should refrain from using the .h when including standard c++ libraries. So preferably use
      [code=cpp]
      #include <iostream>
      [/code]

      instead of
      [code=cpp]
      #include<iostre am.h>
      [/code].

      You have used the notation for string but have not done so in the case of iostream.

      Comment

      • Andr3w
        New Member
        • Nov 2007
        • 42

        #4
        Well even if he didn't use the namespace he could easily do:

        [code=cpp]
        // instead of
        cout >> "something"

        // he could write
        std::cout >> "something else"
        [/code]

        That's for all namespaces in C++, either you include them and you don't use the global operator if you want to use a function of a property that's in them or you use the using operator the namespace's name and add global operator and the function or property that's declared in it.

        generally it's
        [code=cpp]
        using namespace namsepace_name;
        [/code]

        Becareful tho if you include namespaces that have functions with the same name ;)

        Comment

        • manjuks
          New Member
          • Dec 2007
          • 72

          #5
          Look at String mystr; That declaration should be string mystr; all letters in small letters.

          Comment

          • keerthiramanarayan
            New Member
            • Nov 2007
            • 13

            #6
            I repaired the code to this and it works fine. (on VC and gcc atleast). This avoids the name clash problem as pointed out above. The
            Code:
            using std::cout;
            also makes sure that you don't go around typing std:: each time you use the function/class

            Code:
            #include <iostream>
            #include <string>
            
            using std::string;
            using std::cin;
            using std::cout;
            
            int main() {
            	string mystr;
            	cout << "What is your name?";
            	getline(cin,mystr);
            	cout << "my name is:" << mystr;
            	cout << "What is your favourite team?";
            	getline(cin, mystr);
            	cout << "My favourite team is " << mystr;
            	return 0;
            }
            Regards,
            Keerthi Ramanarayan

            Comment

            • manjuks
              New Member
              • Dec 2007
              • 72

              #7
              Originally posted by keerthiramanara yan
              I repaired the code to this and it works fine. (on VC and gcc atleast). This avoids the name clash problem as pointed out above. The
              Code:
              using std::cout;
              also makes sure that you don't go around typing std:: each time you use the function/class

              Code:
              #include <iostream>
              #include <string>
              
              using std::string;
              using std::cin;
              using std::cout;
              
              int main() {
              	string mystr;
              	cout << "What is your name?";
              	getline(cin,mystr);
              	cout << "my name is:" << mystr;
              	cout << "What is your favourite team?";
              	getline(cin, mystr);
              	cout << "My favourite team is " << mystr;
              	return 0;
              }
              Regards,
              Keerthi Ramanarayan
              Hi keerthi,

              why you have used
              (code : cpp)
              using std::string;
              using std::cin;
              using std::cout;

              Instead you can use
              (code : cpp)
              using namespace std;

              Comment

              • Savage
                Recognized Expert Top Contributor
                • Feb 2007
                • 1759

                #8
                Originally posted by manjuks
                Hi keerthi,

                why you have used
                (code : cpp)
                using std::string;
                using std::cin;
                using std::cout;

                Instead you can use
                (code : cpp)
                using namespace std;
                No need to use whole namespace,when you can use just parts of it.This makes *.obj and .exe files smaller.

                Savage

                Comment

                • oler1s
                  Recognized Expert Contributor
                  • Aug 2007
                  • 671

                  #9
                  No need to use whole namespace,when you can use just parts of it.This makes *.obj and .exe files smaller.
                  Uhh...what? I'm going to assume this was said in a muddled state of mind, as this is most certainly a wrong thing to say.

                  Comment

                  • Savage
                    Recognized Expert Top Contributor
                    • Feb 2007
                    • 1759

                    #10
                    Originally posted by oler1s
                    Uhh...what? I'm going to assume this was said in a muddled state of mind, as this is most certainly a wrong thing to say.
                    Ooops.......... sorry for that.

                    OP,when said using std::cout(e.g) you can now just use cout without std prefix,while others you must still prefix with std.

                    Only two good reasons for doing this is evading possible name conflicts.What if there is (for e.g) variable x inside std(there is no variable x inside std just a example) and you put :

                    using namespace std;

                    and declare a variable(or function) called x,there will be name conflict.But with using just some things from the namespace you can possible avoid these conflicts.

                    Comment

                    • bineet276
                      New Member
                      • Feb 2008
                      • 1

                      #11
                      U can use the following code and execute . U would not get any error while compiling and executing the code. So proceed:-

                      #include<iostre am.h>
                      #include<conio. h>
                      #include<stdio. h>

                      int main()
                      {
                      clrscr();
                      char mystr[50];
                      cout<< "What is your name?\n";
                      gets(mystr);
                      cout<<"my name is:"<<mystr<<"\ n";
                      cout<<"What is your favourite team?\n";
                      gets(mystr);
                      cout<<"My favourite team is"<<mystr<<"\n ";
                      getch();
                      return 0;
                      }

                      Comment

                      Working...