Reading in INformation.

This is a discussion on Reading in INformation. within the C++ Programming forums, part of the General Programming Boards category; ok im trying to read in this information which is in an input file. Ray L. Jones 3942 6 4 ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    43

    Reading in INformation.

    ok im trying to read in this information which is in an input file.

    Ray L. Jones 3942 6 4 1974 3 2 2001
    Alice K. Brown 8300 4 8 1969 2 1 1999
    Gill R. Alante 4301 8 2 1972 6 5 2000


    Now here is my code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using std::string;
    using std::cout;
    using std::endl;
    
    int main()
    {
    	
    
    	ifstream inFile("input.dat", ios::in);
    
      if(!inFile)
       {
        cout << "Could not open the file "
             << "for input.\n";
        exit(1);
       }
      else
       cout << "The file input.dat is open for 
    input.\n\n";
    
     
    
    
      string firstName;
      string lastName;
      string middleInitial;
      string employeeID;
      string bmn;  // Birth Month
      string bdy;  // Birth Day
      string byr   // Birth Year
      string hmn;  // Hire Month
      string hdy;  // Hire Day
      string hyr;  // Hire Year
    
      for(int index = 0 index < 3; index++)
       {
        inFile >> firstName >> middleInitial >> lastName >> employeeID>>bmn
    		>>bdy>>byr>>hmn>>hdy>>hyr;
        
       }
      return 0;
     }
    I am using vc++ compiler and i keep gettin errors like 32 of them.
    Can someone help me please.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Other than maybe that exit() may not be in your header files as listed, and maybe trying

    using namespace std;

    as a simplyfing statement instead of the individual using statements until you can figure out what is going on

    and maybe declaring index outside the for loop (I've heard it can cause problems sometimes when declared inside the loop like that)

    your code looks like it should work so it would be nice to have a listing of the first error report or two. Often if you can fix the first one or two errors the others go away, too.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    43
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    
    
    int main()
    {
    	
    
    	ifstream inFile("input.dat", ios::in);
    
      if(!inFile)
       {
        cerr << "Could not open the file "
             << "for input.\n";
        return 1;
       }
      else
       cout << "The file input.dat is open for 
    input.\n\n";
    
     int index;
    
    
      string firstName;
      string lastName;
      string middleInitial;
      string employeeID;
      string bmn;  // Birth Month
      string bdy;  // Birth Day
      string byr   // Birth Year
      string hmn;  // Hire Month
      string hdy;  // Hire Day
      string hyr;  // Hire Year
    
      for(int index = 0; index < 3; index++)
       {
        inFile >> firstName >> middleInitial >> lastName >> employeeID>>bmn
    		>>bdy>>byr>>hmn>>hdy>>hyr;
        
       }
      return 0;
     }
    ompiling...
    test.cpp
    C:\Documents and Settings\Massa\test.cpp(11) : error C2065: 'ifstream' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(11) : error C2146: syntax error : missing ';' before identifier 'inFile'
    C:\Documents and Settings\Massa\test.cpp(11) : error C2065: 'inFile' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(11) : error C2653: 'ios' : is not a class or namespace name
    C:\Documents and Settings\Massa\test.cpp(11) : error C2065: 'in' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(15) : error C2065: 'cerr' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(16) : error C2297: '<<' : illegal, right operand has type 'char [25]'
    C:\Documents and Settings\Massa\test.cpp(20) : error C2065: 'cout' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(20) : error C2001: newline in constant
    C:\Documents and Settings\Massa\test.cpp(21) : error C2297: '<<' : illegal, right operand has type 'char [32]'
    C:\Documents and Settings\Massa\test.cpp(21) : error C2146: syntax error : missing ';' before identifier 'input'
    C:\Documents and Settings\Massa\test.cpp(21) : error C2065: 'input' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(21) : error C2017: illegal escape sequence
    C:\Documents and Settings\Massa\test.cpp(21) : error C2017: illegal escape sequence
    C:\Documents and Settings\Massa\test.cpp(21) : error C2228: left of '.n' must have class/struct/union type
    C:\Documents and Settings\Massa\test.cpp(21) : error C2001: newline in constant
    C:\Documents and Settings\Massa\test.cpp(21) : error C2146: syntax error : missing ';' before identifier 'n'
    C:\Documents and Settings\Massa\test.cpp(21) : error C2143: syntax error : missing ';' before 'string'
    C:\Documents and Settings\Massa\test.cpp(23) : error C2144: syntax error : missing ';' before type 'int'
    C:\Documents and Settings\Massa\test.cpp(26) : error C2065: 'string' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(26) : error C2146: syntax error : missing ';' before identifier 'firstName'
    C:\Documents and Settings\Massa\test.cpp(26) : error C2065: 'firstName' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(27) : error C2146: syntax error : missing ';' before identifier 'lastName'
    C:\Documents and Settings\Massa\test.cpp(27) : error C2065: 'lastName' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(28) : error C2146: syntax error : missing ';' before identifier 'middleInitial'
    C:\Documents and Settings\Massa\test.cpp(28) : error C2065: 'middleInitial' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(29) : error C2146: syntax error : missing ';' before identifier 'employeeID'
    C:\Documents and Settings\Massa\test.cpp(29) : error C2065: 'employeeID' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(30) : error C2146: syntax error : missing ';' before identifier 'bmn'
    C:\Documents and Settings\Massa\test.cpp(30) : error C2065: 'bmn' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(31) : error C2146: syntax error : missing ';' before identifier 'bdy'
    C:\Documents and Settings\Massa\test.cpp(31) : error C2065: 'bdy' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(33) : error C2146: syntax error : missing ';' before identifier 'byr'
    C:\Documents and Settings\Massa\test.cpp(33) : error C2065: 'byr' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(33) : error C2146: syntax error : missing ';' before identifier 'string'
    C:\Documents and Settings\Massa\test.cpp(33) : error C2146: syntax error : missing ';' before identifier 'hmn'
    C:\Documents and Settings\Massa\test.cpp(33) : error C2065: 'hmn' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(34) : error C2146: syntax error : missing ';' before identifier 'hdy'
    C:\Documents and Settings\Massa\test.cpp(34) : error C2065: 'hdy' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(35) : error C2146: syntax error : missing ';' before identifier 'hyr'
    C:\Documents and Settings\Massa\test.cpp(35) : error C2065: 'hyr' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(37) : error C2086: 'index' : redefinition
    C:\Documents and Settings\Massa\test.cpp(40) : warning C4552: '>>' : operator has no effect; expected operator with side-effect
    Error executing cl.exe.

    test.exe - 42 error(s), 1 warning(s)

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>using namespace std;
    or use another similar method
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    43
    i don't understand what your saying

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    /* .... */
    The compiler doesn't know what the classes are because it can't find them within its scope. By using namespace std, it will look at the std classes. This will help solve some of your problems. There are others ways to do this as well.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    43
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    
    using std :: string;
    
    int main()
    {
    	
    
    	ifstream inFile("input.dat", ios::in);
    
      if(!inFile)
       {
        cout << "Could not open the file ";
             
        return 1;
       }
      else
       cout << "The file is open";
    
    
    
      string firstName;
      string lastName;
      string middleInitial;
      string employeeID;
      string bmn;  // Birth Month
      string bdy;  // Birth Day
      string byr;   // Birth Year
      string hmn;  // Hire Month
      string hdy;  // Hire Day
      string hyr;  // Hire Year
    
      while(inFile >> firstName >> middleInitial >> lastName >> employeeID>>bmn
    		>>bdy>>byr>>hmn>>hdy>>hyr)
       {
        
        cout<<firstName<<endl;
       }
      return 0;
     }
    Still getting errors.

    Compiling...
    test.cpp
    C:\Documents and Settings\Massa\test.cpp(5) : error C2653: 'std' : is not a class or namespace name
    C:\Documents and Settings\Massa\test.cpp(5) : error C2873: 'string' : symbol cannot be used in a using-declaration
    C:\Documents and Settings\Massa\test.cpp(24) : error C2065: 'string' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(24) : error C2146: syntax error : missing ';' before identifier 'firstName'
    C:\Documents and Settings\Massa\test.cpp(24) : error C2065: 'firstName' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(25) : error C2146: syntax error : missing ';' before identifier 'lastName'
    C:\Documents and Settings\Massa\test.cpp(25) : error C2065: 'lastName' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(26) : error C2146: syntax error : missing ';' before identifier 'middleInitial'
    C:\Documents and Settings\Massa\test.cpp(26) : error C2065: 'middleInitial' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(27) : error C2146: syntax error : missing ';' before identifier 'employeeID'
    C:\Documents and Settings\Massa\test.cpp(27) : error C2065: 'employeeID' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(28) : error C2146: syntax error : missing ';' before identifier 'bmn'
    C:\Documents and Settings\Massa\test.cpp(28) : error C2065: 'bmn' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(29) : error C2146: syntax error : missing ';' before identifier 'bdy'
    C:\Documents and Settings\Massa\test.cpp(29) : error C2065: 'bdy' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(30) : error C2146: syntax error : missing ';' before identifier 'byr'
    C:\Documents and Settings\Massa\test.cpp(30) : error C2065: 'byr' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(31) : error C2146: syntax error : missing ';' before identifier 'hmn'
    C:\Documents and Settings\Massa\test.cpp(31) : error C2065: 'hmn' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(32) : error C2146: syntax error : missing ';' before identifier 'hdy'
    C:\Documents and Settings\Massa\test.cpp(32) : error C2065: 'hdy' : undeclared identifier
    C:\Documents and Settings\Massa\test.cpp(33) : error C2146: syntax error : missing ';' before identifier 'hyr'
    C:\Documents and Settings\Massa\test.cpp(33) : error C2065: 'hyr' : undeclared identifier
    Error executing cl.exe.

    test.exe - 23 error(s), 0 warning(s)

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I would replace

    using std:: string;

    with

    using namespace std;

    and recompile. If most of your errors go away, and I suspect they will, then go back to trying to reduce contamination of global space, but try proving the problem is here first.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    43
    still got the same errors

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I notice in your most recent post that the headers have all obtained a .h extension. When using namespace std it is best to avoid .h extensions whenever possible, and all the files included are available without the .h extension. If you truly tried compiling with the .h extensions and with the using namespace std line, the errors would persist.

    The style as I understand it is to list individual functions or variables but not individual files:

    so

    #include <iostream>
    #include <string>

    using namespace std;
    .
    .
    .


    or

    #include <iostream>
    #include <string>

    using std::cout;

    .
    .
    .


    but not

    #include <iostream>
    #include <string>

    using std:: string;
    .
    .
    .

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    43
    well im done with using c++ style i\o im using c now but im getting 2 errors

    Code:
    #include<fstream.h>                                 // ifstream,ofstream
    #include<iomanip.h> 
    #include<stdio.h>                                // setw
    #include<iostream.h>                                // cout, cin, cerr
    #include<stdlib.h>                                  // exit()
    #include<string.h>
                                      // string
    
    
    
    int main(void)
    {
    	
      char *firstName;
      char *lastName;
      char *middleInitial;
      char *employeeID;
      char *bmn;  // Birth Month
      char *bdy;  // Birth Day
      char *byr;   // Birth Year
      char *hmn;  // Hire Month
      char *hdy;  // Hire Day
      char *hyr;  // Hire Year
    
    	ifstream inFile("input.dat", ios::in);
    
      if(!inFile)
       {
        cout << "Could not open the file ";
             
        return 1;
       }
      else
       cout << "The file is open";
    
    
    
    
      while(!inFile)
       {
    		fscanf(inFile,"%s %s,%s,%s,%s.%s,%s,%s,%s,%s",firstName,
    			lastName,middleInitial,employeeID,bmn,bdy.byr,hmn,hdy,hyr);
    		printf("%s\n",firstName);
       }
      return 0;
     }
    Compiling...
    test.cpp
    C:\Documents and Settings\Massa\test.cpp(42) : error C2228: left of '.byr' must have class/struct/union type
    C:\Documents and Settings\Massa\test.cpp(42) : error C2664: 'fscanf' : cannot convert parameter 1 from 'class ifstream' to 'struct _iobuf *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    Error executing cl.exe.

    test.exe - 2 error(s), 0 warning(s)

  12. #12
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
    	
    
    	ifstream inFile("input.dat", ios::in);
    
      if(!inFile)
       {
        cout << "Could not open the file "
             << "for input.\n";
        exit(1);
       }
      else
       cout << "The file input.dat is open for input.\n\n";
    
     
    
    
      string firstName;
      string lastName;
      string middleInitial;
      string employeeID;
      string bmn;  // Birth Month
      string bdy;  // Birth Day
      string byr;  // Birth Year
      string hmn;  // Hire Month
      string hdy;  // Hire Day
      string hyr;  // Hire Year
    
      for(int index = 0; index < 3; index++)
       {
        inFile >> firstName >> middleInitial >> lastName >> employeeID>>bmn
    		>>bdy>>byr>>hmn>>hdy>>hyr;
        
       }
      return 0;
     }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  13. #13
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    now i hope you realise that the bottom for loop will not quite be doing what you think.You will need to alter that to read in the values correctly.
    As for your last attempt its an abortion. you are mixing c and c++ io. if you want to use fscanf() then you need to open the file with fopen() and not ifstreams constructor.
    The other error is due to a typo in the fscanf(). A . instead of a , but you should have seen that yourself.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    43
    thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-28-2008, 02:07 AM
  2. reading information from cd
    By jverkoey in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2004, 06:31 PM
  3. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM
  4. Getting information on system
    By sean in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2003, 03:36 PM
  5. Replies: 4
    Last Post: 03-31-2003, 11:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21