Cn't figure out error

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 34
Reputation: Seamus McCarthy is an unknown quantity at this point 
Solved Threads: 0
Seamus McCarthy Seamus McCarthy is offline Offline
Light Poster

Cn't figure out error

 
0
  #1
May 9th, 2008
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. void writeArray();
  6. void readArray();
  7. const int capacity = 4;
  8. //const int spec = 4;
  9. double numbers[]={12456.89,234.00,3456.78,21212.50};
  10.  
  11. //string A12345;
  12. //string A45678;
  13. //string B21223;
  14. //string B21435;
  15. string names[]={"A12345","A45678","B21223","B21435"};
  16.  
  17. int main()
  18. {
  19. writeArray();
  20. readArray();
  21.  
  22. }
  23.  
  24. void readArray()
  25. {
  26.  
  27.  
  28. ifstream infile;
  29.  
  30.  
  31. infile.open ("accounts.txt");
  32.  
  33. infile >> numbers[capacity];
  34.  
  35. infile >> names[capacity];
  36.  
  37. cout << names[0] << endl;
  38. cout << numbers[0] << endl;
  39.  
  40. cout << names[1] << endl;
  41. cout << numbers[1] << endl;
  42.  
  43. cout << names[2] << endl;
  44. cout << numbers[2] << endl;
  45.  
  46. cout << names[3] << endl;
  47. cout << numbers[3] << endl;
  48.  
  49. infile.close();
  50.  
  51. }
  52.  
  53. void writeArray()
  54. {
  55.  
  56. ofstream outfile;
  57.  
  58.  
  59. outfile.open("accounts.txt");
  60.  
  61. outfile << numbers[capacity];
  62.  
  63. outfile << names[capacity];
  64.  
  65. outfile.close();
  66.  
  67. }

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!!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Cn't figure out error

 
0
  #2
May 9th, 2008
> 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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 9
Reputation: compumasta is an unknown quantity at this point 
Solved Threads: 0
compumasta compumasta is offline Offline
Newbie Poster

Re: Cn't figure out error

 
0
  #3
May 9th, 2008
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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC