Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Sep 13th, 2005, 3:16 PM   #1
Kazy
Newbie
 
Join Date: Sep 2005
Posts: 21
Rep Power: 0 Kazy is on a distinguished road
cin.getline() error

For some reason I'm getting an error when I try to use getline(). Its very simple code and for some reason I can't figure out why its giving me the error.. its stopping at:

cout << "Enter a number: ";
cin.getline(data, 101); // read in number into variable

and giving me the error:
sender.cpp: In function `int main()':
sender.cpp:33: error: no matching function for call to `
std::basic_istream<char, std::char_traits<char> >::getline(std::string&, int
)'
/usr/include/c++/3.3.4/bits/istream.tcc:594: error: candidates are:
std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,
_Traits>::getline(_CharT*, int, _CharT) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.3.4/istream:401: error:
std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,
_Traits>::getline(_CharT*, int) [with _CharT = char, _Traits =
std::char_traits<char>]


any ideas on why i'm getting this error?
Kazy is offline   Reply With Quote
Old Sep 13th, 2005, 3:20 PM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
firstly, what type has data? probably a string? If so, use this code:
getline(cin, data, '\n');

secondly, use code tags
Polyphemus_ is offline   Reply With Quote
Old Sep 13th, 2005, 3:21 PM   #3
Kazy
Newbie
 
Join Date: Sep 2005
Posts: 21
Rep Power: 0 Kazy is on a distinguished road
data is a string
Kazy is offline   Reply With Quote
Old Sep 13th, 2005, 3:22 PM   #4
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by Kazy
data is a string
yap, i already saw it in your compile errors. see the edit of my post
Polyphemus_ is offline   Reply With Quote
Old Sep 13th, 2005, 3:26 PM   #5
Kazy
Newbie
 
Join Date: Sep 2005
Posts: 21
Rep Power: 0 Kazy is on a distinguished road
I'm sure this is a stupid question, but what is a code tag?
Kazy is offline   Reply With Quote
Old Sep 13th, 2005, 3:38 PM   #6
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by Kazy
I'm sure this is a stupid question, but what is a code tag?
code tags are to create such blocks of code:
void piece_of_code(void) {

}

Just put the piece of code between [ code] and [/ code] (without spaces).
Polyphemus_ is offline   Reply With Quote
Old Sep 13th, 2005, 3:57 PM   #7
Kazy
Newbie
 
Join Date: Sep 2005
Posts: 21
Rep Power: 0 Kazy is on a distinguished road
ahh, well here is my code: maybe this will help find the problem

#include <iostream>
#include <string>  // For string class usage
#include <fstream> // For file I/O


using namespace std;

#define SEND 1;
#define CLOSE 2;

int main( void )
{


        string data;       // String to store input from user
        ofstream outData;  // Output stream to store data to file
        ofstream myPipe;   // Output stream for named pipe
        bool flag=false;   // For checking correct input
        bool exitFlag=false;
        // Create the named pipe to communicate with other app
        system("mkfifo myPipe");



        // Output Header Info
        cout << "Sender" << endl;
        cout << "Written by Brian Kays" << endl;
        cout << endl;
        while (!exitFlag)
        {
                cout << "Enter a number: ";

                cin.getline(data, 101); // read in number into variable
                cout << "Data: " << data << " : Data Length: " << data.length() <<  endl;
                if (data == "")
                {
                        myPipe.open("myPipe");
                        myPipe << CLOSE;
                        exit(1);
                }
                for (int i=0; i<data.length(); i++)
                {
                        if (flag == false && data.substr(i, 1) >= "a")
                        {
                                flag = true;
                                cout << "Not valid input!" << endl;
                        }
                }

                outData.open("data.txt"); // open the file

                if(!outData)   // Check to see if file was opened.
                {
                        cout << "Error opening the file! Aborting..." << endl;
                        exit(1); // Exit program if file was not successfully opened.
                }

                if (flag == false)
                {
                        outData << data << endl;
                        cout << "Writing number to file..." << endl;

                        outData.close();

                        myPipe.open("myPipe");
                        myPipe << SEND; // Send msg to receiver
                }
                else
                {
                        outData.close();
                        flag = false;
                }
                myPipe.close(); // Close pipe
        }
        return 0;
}
Kazy is offline   Reply With Quote
Old Sep 13th, 2005, 4:01 PM   #8
Kazy
Newbie
 
Join Date: Sep 2005
Posts: 21
Rep Power: 0 Kazy is on a distinguished road
oh sorry, didn't see your update, let me try that
Kazy 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 4:02 PM.

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