954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Cn't figure out error

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void writeArray();
void readArray();
const int capacity = 4;
//const int spec = 4;
double numbers[]={12456.89,234.00,3456.78,21212.50};

//string A12345;
//string A45678;
//string B21223;
//string B21435;
string names[]={"A12345","A45678","B21223","B21435"};

int main()
{
writeArray();
readArray();

}

void readArray()
{


ifstream infile;


infile.open ("accounts.txt");

infile >>  numbers[capacity];

infile >>  names[capacity];

cout   << names[0] << endl; 
cout   << numbers[0] << endl;

cout   << names[1] << endl; 
cout   << numbers[1] << endl;

cout   << names[2] << endl; 
cout   << numbers[2] << endl;

cout   << names[3] << endl; 
cout   << numbers[3] << endl;

infile.close();

}

void writeArray()
{

ofstream outfile;


outfile.open("accounts.txt");

outfile << numbers[capacity];

outfile << names[capacity];

outfile.close();

}


Can some1 please help, i can't figure what went wrong with my program!! I think it has something to do with string names array but don't know how to fix it!!

Seamus McCarthy
Light Poster
35 posts since Apr 2008
Reputation Points: 11
Solved Threads: 0
 

> infile >> numbers[capacity];
This doesn't fill the array for you, you have to do that yourself with a loop.
All this does it trash beyond the end of the array.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

yes, if you know the number of items in the file, try using a 'for' loop, if not, try a while loop. either way, the read command you used will not fill the array properly.

compumasta
Newbie Poster
9 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You