Problem with pointer

This is a discussion on Problem with pointer within the C++ Programming forums, part of the General Programming Boards category; Hi all, I have the following code: Code: #pragma once #include <iostream> using namespace std; class Bill{ private: int bill; ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    7

    Problem with pointer

    Hi all,

    I have the following code:
    Code:
    #pragma once
    #include <iostream>
    using namespace std;
    class Bill{
    
    private:
             int bill;
    public:
             setBill(int);
            int getBill();
    }
    
    The implement file is as follow:
    #include "Bill.h"
    void Bill::setBill(int bill)
    {
         (*this.bill) = bill;
    }
    int Bill::getBill()
    {
          return bill;
    }
    I have a bat file call data.bat and inside it has this data:
    1234
    1890
    1470
    
    In my main file:
    
    #include "Bill.h"
    #include <fstream>
    
    
    int main()
    {
    		int i=0;
    	Bill *bill[2];
    
    
    	ifstream indata;
    
    	int num; // variable for input value
    	indata.open("data.dat"); // opens the file
    	if(!indata) { // file couldn't be opened
    		  cerr << "Error: file could not be opened" << endl;
    		exit(1);
       }
    	indata >> num;
     while ( !indata.eof() ) { // keep reading until end-of-file
       		   i++;
    	   bill[i]=new Bill;
    			cout << "number is " << num;
    			bill[i]->setBill(num);
       indata.close();
       cout << "End-of-file reached.." << endl;
     
    
       for(int n=0; n < 3; n++)
       {
    	   cout << "bill : " <<bill[n]->getBill<< endl;
    
       }
       cin.ignore();
       cin.ignore();
    
    
    
    return 0;
    
    }
    I want the output to be like below:
    bill:1234
    bill:1890
    bill:1470

    However the output I get is :
    bill: -842150451
    bill: -842150451
    bill: -842150451

    I would not figure out the problem.Any help is much appreciated.
    Thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    Does that even compile? You need to post the actual code that you compiled. It is good that you posted code in code tags, but please indent the code properly too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,754
    you have array of 2 pointers
    how do you expect to put 3 values there?
    To be or not to be == true

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    7

    Solved

    Hi All,

    I have managed to solve the problem.Thank for your reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM

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