Error during writing

Hi guys: I try to write two string on separate lines in a text file but keep getting errors


#include<iostream>
#include<fstream>
#include<cstdlib>

using namespace test;

int main()
{
    string emp = "Employee";
    string empnam = "Name";
    ofstream outFile;
    outFile.open("febsales.txt", ios::app);
    getline(cin, outFile);
    outFile >> emp;
    getline(outFile);
    outFile >> empname;    
    outFile.close();
    system("pause");
    return 0;
}



Compiler: Default compiler
Executing  g++.exe...
g++.exe "C:\Documents and Settings\Kathy\My Documents\test.cpp" -o "C:\Documents and Settings\Kathy\My Documents\test.exe"    -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include"  -I"C:\Dev-Cpp\include\c++\3.4.2\backward"  -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32"  -I"C:\Dev-Cpp\include\c++\3.4.2"  -I"C:\Dev-Cpp\include"   -L"C:\Dev-Cpp\lib"
C:\Documents and Settings\Kathy\My Documents\test.cpp: In function `int main()':
C:\Documents and Settings\Kathy\My Documents\test.cpp:13: error: no matching function for call to `getline(std::istream&, std::ofstream&)'
C:\Documents and Settings\Kathy\My Documents\test.cpp:14: error: no match for 'operator>>' in 'outFile >> emp'
C:\Documents and Settings\Kathy\My Documents\test.cpp:15: error: no matching function for call to `getline(std::ofstream&)'
C:\Documents and Settings\Kathy\My Documents\test.cpp:16: error: `empname' undeclared (first use this function)
C:\Documents and Settings\Kathy\My Documents\test.cpp:16: error: (Each undeclared identifier is reported only once for each function it appears in.)

Execution terminated
mustish1Asked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
no matching function for call to `getline(std::istream&, std::ofstream&)'

You are calling getline() with two stream arguments. The second parameter to getline() should be a string to hold the data.
no matching function for call to `getline(std::ofstream&)'

getline() takes two parameters, not one.
no match for 'operator>>' in 'outFile >> emp'

You're trying to use the input operator on an output stream.
`empname' undeclared (first use this function)

You declared "empnam", with no "e", but you are trying to use "empname", with an "e".
0
 
mustish1Author Commented:
#include<iostream>
#include<fstream>
#include<cstdlib>

using namespace std;

int main()
{
    string emp = "Employee";
    string empnam = "Name";
    ofstream outFile;
    outFile.open("febsales.txt", ios::app);
    getline(outFile);
    outFile >> emp;
    getline(outFile);
    outFile >> empnam;    
    outFile.close();
    system("pause");
    return 0;
}


C:\Documents and Settings\Kathy\My Documents\test.cpp:14: error: no match for 'operator>>' in 'outFile >> emp'
C:\Documents and Settings\Kathy\My Documents\test.cpp:15: error: no matching function for call to `getline(std::ofstream&)'
C:\Documents and Settings\Kathy\My Documents\test.cpp:16: error: no match for 'operator>>' in 'outFile >> empnam'

Execution terminated
0
 
mustish1Author Commented:
Thanks.

#include<iostream>
#include<fstream>
#include<cstdlib>

using namespace test;

int main()
{
    string emp = "Employee";
    string empnam = "Name";
    ofstream outFile;
    outFile.open("febsales.txt", ios::app);
    outFile << emp;
    outFile << endl;
    outFile << empnam;    
    outFile.close();
    system("pause");
    return 0;
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.