Anyone know how to use this formula?

This is a discussion on Anyone know how to use this formula? within the C++ Programming forums, part of the General Programming Boards category; address of the first record in the array + (account number - 1000) * sizeof(int). I am supposed to read ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    23

    Anyone know how to use this formula?

    address of the first record in the array + (account number - 1000) * sizeof(int).

    I am supposed to read this file into 4 separate arrays. And, be able to request a user-entered account number and display the corresponding name and account balance. This is all I come up with so far, but not really sure how to use that formula.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string filename = "bank.dat";
    	const int CUSTOMERS=5;
    	int i=0, acctNum[CUSTOMERS];
    	string firstName[CUSTOMERS], lastName[CUSTOMERS];
    	float balance[CUSTOMERS];
    
    	ifstream inFile;
    
    	inFile.open(filename.c_str());
    
    	if(inFile.fail())
    	{
    		cout<<"\nThe file named "<<filename<<" was not successfully opened"
    			<<"\nPlease check that the file currently exists."
    			<<endl;
    		exit(1);
    	}
    	cout<<"\nThe file has been successfully opened for reading."
    		<<endl<<endl;
    	
    	inFile>>acctNum>>firstName>>lastName>>balance;
    
    	cout<<"\nEnter the account number for the customer you are looking for: "<<endl;
    	
        
    	
    	
    	inFile.close();
    
    	return 0;
    
    
    
    }

    this line also gives me this error: error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'int [5]' (or there is no acceptable conversion)

    Code:
    inFile>>acctNum>>firstName>>lastName>>balance;

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You will need to use a loop to read each entry from the file [and I presume that the file looks something like this:
    Code:
    1001 John Smith 183.75
    1002 Adam Boone 3291.40
    ...
    Assuming you were successfull in reading five items at a time, the file would need to look like this:
    Code:
    1001 1002 1003 1004 1005
    John Adam ...
    Smith Boone ... 
    183.75 3291.40 ...
    I doubt this is the actual format of the file.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    The error message means that the >> operator isn't (and can't be)
    overloaded for the purpose of reading into an array itself, such
    as you try to do with this:

    inFile>>acctNum>>firstName>>lastName>>balance;

    as acctNum, firstname, lastname and balance are all arrays,
    or the address of the first element of an array, depending how used.
    You should read into any given element of the array like this:

    inFile>>acctNum[x]>>firstName[x]>>lastName[x]>>balance[x];

    since acctNum[x], firstName[x],lastName[x] and balance[x] are all
    individual objects of type int, string, string, and double respectively.
    Use a loop to get all the different elements of each array
    stored in the file in place.


    The formula looks like it might be used to calculate an offset, but
    that's a guess.
    You're only born perfect.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Location
    Stoneham,Québec
    Posts
    10
    well wut this formula seems to do : it gives you the size of memory this information uses in ur vector wich seem to be a vector of int...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Zeller's Formula
    By unejam2005 in forum C++ Programming
    Replies: 6
    Last Post: 11-14-2005, 09:48 PM
  2. how to change char to formula
    By kosong in forum C Programming
    Replies: 2
    Last Post: 06-09-2003, 05:31 AM
  3. Math formula (js -->C++)
    By Demon1s in forum C++ Programming
    Replies: 20
    Last Post: 05-02-2003, 06:22 AM
  4. Need help with this formula -b/2a..Please read.
    By foofoo in forum C Programming
    Replies: 4
    Last Post: 06-05-2002, 12:59 AM
  5. Formula string conversion
    By Gr3g in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2002, 09:28 PM

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