Solved

Writing to a sequential access file using a program defined function

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Which IDE to use to begin C++ training? 5 70
PDF library for Delphi 2 135
Getting IP address 8 104
object oriented programming on screen browser tutorial lesson 2 87
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

733 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