User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 332,649 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,327 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 88 | Replies: 2
Reply
Join Date: Apr 2008
Posts: 15
Reputation: Seamus McCarthy is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Seamus McCarthy Seamus McCarthy is offline Offline
Newbie Poster

Cn't figure out error

  #1  
7 Days Ago
#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!!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 2,645
Reputation: Salem is a splendid one to behold Salem is a splendid one to behold Salem is a splendid one to behold Salem is a splendid one to behold Salem is a splendid one to behold Salem is a splendid one to behold Salem is a splendid one to behold 
Rep Power: 16
Solved Threads: 275
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: Cn't figure out error

  #2  
7 Days Ago
> 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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Reply With Quote  
Join Date: May 2008
Posts: 9
Reputation: compumasta is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
compumasta compumasta is offline Offline
Newbie Poster

Re: Cn't figure out error

  #3  
7 Days Ago
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 2:59 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC