473,654 Members | 3,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

i am having trouble with arrays

14 New Member
I have tried to develop a code using arrays and void functions to take the current earnings of employees and using it to calculate their future income in ten years. This was performed by a 8% percent increase every year. below i've posted my attempt and for some reason the code will compile successfully however, when trying to run the program there are errors or bugs. If i can get any help it would be greatly appreciated.

Ric712:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream> 
  2. #include <fstream> 
  3. using namespace std; 
  4. void input(char name[6][6], double salary[6], double &prog, int &j); 
  5. void output(char name[6][6], double salary[6], double prog, int j); 
  6. int main() 
  7. char name[6][6]; 
  8. double salary[6], prog; 
  9. int j; 
  10. input(name, salary, prog, j); 
  11. output(name, salary, prog, j); 
  12. return 0; 
  13. void input(char name[6][6], double salary[6], double &prog, int &j) 
  14. double p = 0.0; 
  15. ifstream sam; 
  16. sam.open("book.dat"); 
  17. while(!(sam.eof())) 
  18. int i(0); 
  19. j=j+1; 
  20. sam >> name[j] >> salary [j]; 
  21. for(i=1; i <= 10; i++) 
  22. double inc = 0.0; 
  23. inc = salary[j]*(1.08); 
  24. p = inc + p; 
  25.  
  26. prog=p; 
  27. sam.close(); 
  28. }// end input 
  29. void output(char name[6][6], double salary[6], double prog, int j) 
  30. cout << "People and progressed salaries:\n"; 
  31. cout << "--------------------------------------------------\n"; 
  32. for(j=1;j<=6;j++) 
  33. cout << name[j] << " " << salary [j] << endl; 
  34. cout << "\nThe future salary in 10 years is " << prog << endl; 
  35. }
Jul 10 '07 #1
2 1195
scruggsy
147 New Member
j is not initialized before you try to use it as an index into the name array. Judging by your code, you should be initializing it to 0. Otherwise you'll very likely crash when you get to this line:
Expand|Select|Wrap|Line Numbers
  1. sam >> name[j] >> salary [j]; 
Also, since you only have room for six names in your name array, you should probably avoid using eof() as your loop terminator, as there could be more than 6 sets of data in the file. A for-loop would make more sense.
Jul 10 '07 #2
ericstein81
14 New Member
j is not initialized before you try to use it as an index into the name array. Judging by your code, you should be initializing it to 0. Otherwise you'll very likely crash when you get to this line:
Expand|Select|Wrap|Line Numbers
  1. sam >> name[j] >> salary [j]; 
Also, since you only have room for six names in your name array, you should probably avoid using eof() as your loop terminator, as there could be more than 6 sets of data in the file. A for-loop would make more sense.
Thank you Scruggsy, the program is running now. i forgot to mention that the data file used had only six names with individual payrolls. Thanks for your help
Jul 10 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1699
by: Ian Arnold | last post by:
Alright, so I know that you can use char pointers to store strings, and I'm trying to make a very simple program to see how it all works that will have the user enter 5 characters, then the computer will show those five characters back to the user. So far I have: #include <iostream.h> int main() { char* buffer = " "; //Five spaces
6
4718
by: grunes | last post by:
I wish to fread a few thousand bytes at a time in turn from several very large files from a DVD data disk, under Redhat Fedora Linux. When this is done the DVD drive wastes a lot of time and almost shakes itself to pieces. I tried using setvbuf, with large buffers, e.g. (example only, not checked): #include <stdio.h>
5
2506
by: Krumble Bunk | last post by:
Hello! First things first (but not necessarily in that order), this is a really great group, and has helped me understand more and more C everytime I read the postings, so thanks for a great learning environment! I am having difficulty understanding this bitwise macro (i've recently moved into working on these, using OpenSolaris source code - magnificent learning resource btw!)
7
9684
by: Mike Joseph | last post by:
I have a data file that was produced in MicroSoft Access. It's a table that was exported as a fixed format, sequential access ASCII file. It contains over 80,000 records. Basically, all the stats for every player in Major League Baseball history in alphabetical order and then by each year they played. I want to take this data and display to the user the career totals for each player. I don't seem to understand how to use StreamReader...
9
7329
suzee_q00
by: suzee_q00 | last post by:
I will admit that lots of times the obvious eludes me and this is most likely one of those times but if anyone has any ideas on what I can do to make this code work, I would greatly appreciate it. Here's the code: while ( ( fgets ( buf, BUFLEN, fin ) ) != NULL ) /* pulls a line from "fin" up to the length of BUFLEN and stores it in "buf" */ { rmNl ( buf ); /* remove new line */ if ( !lineWithBlanks ( buf ) ) { for (i = 0; buf !=...
1
2438
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are of different lengths (e.g. usually between 25 and 45 rows. The columns in each file have the same length). The text files have been numbered sequentially e.g. cb0, cb1, cb2 and so on. I would like to read the data from each text file into...
15
1633
Ajm113
by: Ajm113 | last post by:
Ok I got my stuff to split, but how can I have the while work correctly so it will show all of the arrays correctly? Like if you where using Mysql to retrieve data. But this time it is retrieving all the page meta tags and displaying them in a list. Here is my own Meta Tag I made that I am using for the script: <META NAME="pages" CONTENT="Web Developer;http://www.bombinaid.com/Web-Developer"> Here is the script where I want all my page's...
5
1433
by: polas | last post by:
Afternoon everyone. I am having some trouble and thought a few of you might be able to help me... I have a simple function to copy the contents of one array into another - the arguments to the function are the first array (to copy into), the second array (copy from) and the number of items to copy, the prototype is void cpyarray (float * a, float * b, int numbertocopy) - this worked fine initially. However, I would now like to use this...
24
4200
by: GesterX | last post by:
First of all I'm new to this site but it certainly looks like a place that i will be visiting more often! Onto my problem. I am creating a Hotel Bussiness project in java using BlueJ The classes are as follows: Hotel - Deals with the major functions of the hotel (i.e guests booking in, finding rooms etc.) Room - Used to decide whether a room has a sea view and stores the guest that is in the room. Occupier - For information about the...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8594
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2716
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1596
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.