Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 30th, 2005, 1:41 PM   #1
darkone916
Hobbyist Programmer
 
darkone916's Avatar
 
Join Date: Jul 2005
Location: Oman
Posts: 125
Rep Power: 4 darkone916 is on a distinguished road
Send a message via MSN to darkone916
getline() while including <string.h>

Hello...

i tried to use this code:

#include <iostream.h>
#include <string.h>
int main()
{
    std::string quote;
    std::cin.getline(quote,50);
    if(quote=="The one and the only one...darkone.")
    std::cout<<"\a";
}

getline() gives these errors:

C:\C++\C++.cpp In function `int main()':

6 C:\C++\C++.cpp no matching function for call to `std::basic_istream<char, std::char_traits<char> >::getline(std::string&, int)'

note C:\Dev-Cpp\include\c++\3.4.2\bits\istream.tcc:582 candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]

note C:\Dev-Cpp\include\c++\3.4.2\bits\istream.tcc:582 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]

In the other hand getline() works well with a variable decleared as char.

thank you 4 helping.
__________________
From the bottom of the stone steps...
...i'm calling still.
darkone916 is offline   Reply With Quote
Old Jul 30th, 2005, 1:45 PM   #2
skuinders
Hobbyist Programmer
 
skuinders's Avatar
 
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4 skuinders is on a distinguished road
#include <iostream>
#include <string>
int main()
{
   std::string quote;
   std::cin >> quote;
   if(quote=="The one and the only one...darkone.")
	 std::cout<<"\a";
}
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand."
- B. Russell

http://web.bryant.edu/~srk2
skuinders is offline   Reply With Quote
Old Jul 30th, 2005, 1:49 PM   #3
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
If your heart is set on the getline(), then you should check it's prototype depending on library: either this or this .
__________________
got math? yumm...

"All men by nature desire to know" -Aristotle, Metaphysics

Last edited by EverLearning; Jul 30th, 2005 at 2:12 PM. Reason: note on libraries
EverLearning is offline   Reply With Quote
Old Jul 30th, 2005, 1:51 PM   #4
darkone916
Hobbyist Programmer
 
darkone916's Avatar
 
Join Date: Jul 2005
Location: Oman
Posts: 125
Rep Power: 4 darkone916 is on a distinguished road
Send a message via MSN to darkone916
Thank you for helping... but using this code will take only the characters before the first space.
e.g. if i typed in :"The one and the only one...darkone." it will only take the "the".

thanx again.
__________________
From the bottom of the stone steps...
...i'm calling still.
darkone916 is offline   Reply With Quote
Old Jul 30th, 2005, 1:52 PM   #5
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
skuinders that will only read in one word not what darkone916 wanted.

@ darkone916 in <string> there's a more useful getline function, that you dont have to specify the length of the input:

#include <iostream>
#include <string>
int main()
{
   std::string quote;
   std::getline( std::cin, quote );
   if(quote=="The one and the only one...darkone.")
	 std::cout<<"\a";
}

This will read in the input up to a '\n'.
Animatronic is offline   Reply With Quote
Old Jul 30th, 2005, 1:57 PM   #6
darkone916
Hobbyist Programmer
 
darkone916's Avatar
 
Join Date: Jul 2005
Location: Oman
Posts: 125
Rep Power: 4 darkone916 is on a distinguished road
Send a message via MSN to darkone916
Yep!!! it works!!!
thanx everyone!!!

__________________
From the bottom of the stone steps...
...i'm calling still.
darkone916 is offline   Reply With Quote
Old Jul 30th, 2005, 1:59 PM   #7
The Cyndicate
Newbie
 
The Cyndicate's Avatar
 
Join Date: Jul 2005
Posts: 3
Rep Power: 0 The Cyndicate is on a distinguished road
Nice job.. i making aprograms app of my own Very easy.. and debugg mod
The Cyndicate is offline   Reply With Quote
Old Jul 30th, 2005, 2:01 PM   #8
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Darkone, just a note, you can use "using namespace std;" instead of "std::bla" like his:
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string quote;
   getline(cin, quote );
   if(quote=="The one and the only one...darkone.")
	 cout<<"\a";
}
OpenLoop is offline   Reply With Quote
Old Jul 30th, 2005, 2:10 PM   #9
skuinders
Hobbyist Programmer
 
skuinders's Avatar
 
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4 skuinders is on a distinguished road
woops sorry... I forgot about that :o
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand."
- B. Russell

http://web.bryant.edu/~srk2
skuinders is offline   Reply With Quote
Old Jul 30th, 2005, 2:12 PM   #10
darkone916
Hobbyist Programmer
 
darkone916's Avatar
 
Join Date: Jul 2005
Location: Oman
Posts: 125
Rep Power: 4 darkone916 is on a distinguished road
Send a message via MSN to darkone916
Quote:
Darkone, just a note, you can use "using namespace std;" instead of "std::bla"
Does it have any side effects using "using namespace std; ?
__________________
From the bottom of the stone steps...
...i'm calling still.
darkone916 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:12 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC