Solved

Writing to a sequential access file using a program defined function

Posted on 2004-04-18
3
1,370 Views
Last Modified: 2012-06-21
I rewrote the program as follows and am getting 11 errors.  I would very much appreciate some insight.  Thanks!!

Compiling...
students.cpp
C:\students.dat\students.cpp(58) : error C2065: 'outFile' : undeclared identifier
C:\students.dat\students.cpp(58) : error C2784: 'class std::basic_ostream<_E,_Tr> &__cdecl std::operator <<(class std::basic_ostream<_E,_Tr> &,const class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument for 'class std::basic_os
tream<_E,_Tr> &' from 'int'
C:\students.dat\students.cpp(58) : error C2784: 'class std::basic_ostream<_E,_Tr> &__cdecl std::operator <<(class std::basic_ostream<_E,_Tr> &,const short *)' : could not deduce template argument for 'class std::basic_ostream<_E,_Tr> &' from 'int'
C:\students.dat\students.cpp(58) : error C2784: 'class std::basic_ostream<_E,_Tr> &__cdecl std::operator <<(class std::basic_ostream<_E,_Tr> &,const unsigned char)' : could not deduce template argument for 'class std::basic_ostream<_E,_Tr> &' from '
int'
C:\students.dat\students.cpp(58) : error C2784: 'class std::basic_ostream<_E,_Tr> &__cdecl std::operator <<(class std::basic_ostream<_E,_Tr> &,const unsigned char *)' : could not deduce template argument for 'class std::basic_ostream<_E,_Tr> &' from
 'int'
C:\students.dat\students.cpp(58) : error C2784: 'class std::basic_ostream<_E,_Tr> &__cdecl std::operator <<(class std::basic_ostream<_E,_Tr> &,const signed char)' : could not deduce template argument for 'class std::basic_ostream<_E,_Tr> &' from 'in
t'
C:\students.dat\students.cpp(58) : error C2784: 'class std::basic_ostream<_E,_Tr> &__cdecl std::operator <<(class std::basic_ostream<_E,_Tr> &,const signed char *)' : could not deduce template argument for 'class std::basic_ostream<_E,_Tr> &' from '
int'
C:\students.dat\students.cpp(58) : error C2784: 'class std::basic_ostream<_E,_Tr> &__cdecl std::operator <<(class std::basic_ostream<_E,_Tr> &,_E)' : could not deduce template argument for 'class std::basic_ostream<_E,_Tr> &' from 'int'
C:\students.dat\students.cpp(58) : error C2784: 'class std::basic_ostream<_E,_Tr> &__cdecl std::operator <<(class std::basic_ostream<_E,_Tr> &,const _E *)' : could not deduce template argument for 'class std::basic_ostream<_E,_Tr> &' from 'int'
C:\students.dat\students.cpp(58) : error C2677: binary '<<' : no global operator defined which takes type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)
C:\students.dat\students.cpp(63) : error C2228: left of '.close' must have class/struct/union type
Error executing cl.exe.

students.exe - 11 error(s), 0 warning(s)




//writes records to a sequential access file

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

//function prototype
void addRecords();


int main()
{
     //declare variables
          string name = "";
          int test1 = 0;
          int test2 = 0;
          int test3 = 0;


     //create file object and open file
     ofstream outFile;
     outFile.open("student.dat", ios::app);
     //determine if the file was opened successfully
if (outFile.is_open())
     {

          //enter input items
          addRecords();

     }
     else
     cout << "File could not be opened." << endl;
     //end ifs

     return 0;
} //end main function

//*****Program-defined function*****
void addRecords(string name, int test1, int test2, int test3)
{
     //get student information

          cout << "Name (x to stop): ";
          getline(cin, name);
               while (name != "X" && name != "x")
               {
                    cout << "Test 1: ";
                    cin >> test1;
                    cin.ignore(1);
                    cout << "Test 2: ";
                    cin >> test2;
                    cin.ignore(1);
                    cout <<"Test 3: ";
                    cin >> test3;
                    cin.ignore(1);
                    //write record to file
                    outFile << name << "#" << test1 << "#" << test2 << "#" << test3 << endl;
                    cout << "Name (x to stop) : ";
                    getline(cin, name);
                    } //end while
               //close file
               outFile.close();
}//end program defined function
0
Comment
Question by:SinSual
  • 2
3 Comments
 
LVL 15

Expert Comment

by:efn
ID: 10855562
The name "outFile" is only defined in the main function.  If you want to use it in the the addRecords function, you have to make it global or pass a reference to it to the function.

--efn
0
 
LVL 11

Expert Comment

by:avizit
ID: 10890317
also your function prototype and function definition dont match

//function prototype
void addRecords();

//*****Program-defined function*****
void addRecords(string name, int test1, int test2, int test3)
{


/abhijit/
0
 
LVL 11

Accepted Solution

by:
avizit earned 500 total points
ID: 10890441
I tried your program by changing the prototype to

//function prototype
void addRecords(string name, int test1, int test2, int test3);

and also the function call in main()
i.e changed
if (outFile.is_open())
     {          //enter input items
          addRecords();

to

if (outFile.is_open())
     {          //enter input items
          addRecords( name, test1, test2,  test3);


and also making "outFile" as global

and it seemed to work

/abhijit/
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question