Solved

Writing to a sequential access file using a program defined function

Posted on 2004-04-18
3
1,382 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

688 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