Solved

Writing to a sequential access file using a program defined function

Posted on 2004-04-18
3
1,356 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now