![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2005
Posts: 21
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
firstly, what type has data? probably a string? If so, use this code:
getline(cin, data, '\n'); secondly, use code tags ![]() |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Sep 2005
Posts: 21
Rep Power: 0
![]() |
data is a string
|
|
|
|
|
|
#4 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
![]() |
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Sep 2005
Posts: 21
Rep Power: 0
![]() |
I'm sure this is a stupid question, but what is a code tag?
|
|
|
|
|
|
#6 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
void piece_of_code(void) {
}Just put the piece of code between [ code] and [/ code] (without spaces). |
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: Sep 2005
Posts: 21
Rep Power: 0
![]() |
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;
} |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Sep 2005
Posts: 21
Rep Power: 0
![]() |
oh sorry, didn't see your update, let me try that
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|