468,457 Members | 1,685 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

function should have a prototype

Hi all,

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

#include<iostream.h>
#include<conio.h>
#include<string>
#include<stdio.h>

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;
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...
Dec 31 '07 #1
10 29727
Savage
1,764 Expert 1GB
Hi all,

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

#include<iostream.h>
#include<conio.h>
#include<string>
#include<stdio.h>

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;
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:

Expand|Select|Wrap|Line Numbers
  1. using namespace std;
PS:Please use [code=cpp] tags around your c++ code

Savage
Dec 31 '07 #2
Hi all,

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

#include<iostream.h>
#include<conio.h>
#include<string>
#include<stdio.h>

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;
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
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
instead of
Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2.  
.

You have used the notation for string but have not done so in the case of iostream.
Jan 1 '08 #3
Andr3w
42
Well even if he didn't use the namespace he could easily do:

Expand|Select|Wrap|Line Numbers
  1. // instead of
  2. cout >> "something"
  3.  
  4. // he could write
  5. std::cout >> "something else"
  6.  
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
Expand|Select|Wrap|Line Numbers
  1. using namespace namsepace_name;
  2.  
Becareful tho if you include namespaces that have functions with the same name ;)
Jan 1 '08 #4
manjuks
72
Look at String mystr; That declaration should be string mystr; all letters in small letters.
Jan 2 '08 #5
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
Expand|Select|Wrap|Line Numbers
  1. using std::cout;
  2.  
also makes sure that you don't go around typing std:: each time you use the function/class

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using std::string;
  5. using std::cin;
  6. using std::cout;
  7.  
  8. int main() {
  9.     string mystr;
  10.     cout << "What is your name?";
  11.     getline(cin,mystr);
  12.     cout << "my name is:" << mystr;
  13.     cout << "What is your favourite team?";
  14.     getline(cin, mystr);
  15.     cout << "My favourite team is " << mystr;
  16.     return 0;
  17. }
  18.  
Regards,
Keerthi Ramanarayan
Jan 2 '08 #6
manjuks
72
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
Expand|Select|Wrap|Line Numbers
  1. using std::cout;
  2.  
also makes sure that you don't go around typing std:: each time you use the function/class

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using std::string;
  5. using std::cin;
  6. using std::cout;
  7.  
  8. int main() {
  9.     string mystr;
  10.     cout << "What is your name?";
  11.     getline(cin,mystr);
  12.     cout << "my name is:" << mystr;
  13.     cout << "What is your favourite team?";
  14.     getline(cin, mystr);
  15.     cout << "My favourite team is " << mystr;
  16.     return 0;
  17. }
  18.  
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;
Jan 2 '08 #7
Savage
1,764 Expert 1GB
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
Jan 2 '08 #8
oler1s
671 Expert 512MB
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.
Jan 2 '08 #9
Savage
1,764 Expert 1GB
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.
Jan 2 '08 #10
U can use the following code and execute . U would not get any error while compiling and executing the code. So proceed:-

#include<iostream.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;
}
Feb 21 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Alfonso Morra | last post: by
3 posts views Thread by steven.ke | last post: by
4 posts views Thread by ygao | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.