Solved

Writing to a sequential access file using a program defined function

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

746 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

8 Experts available now in Live!

Get 1:1 Help Now