c++ program

Sep 5, 2011 at 3:40am
i have a assignment that i don't have any idea how to do this question.
QUESTION:

Write a complete c++ program to do the following:
a) declare an array to store the salary of 100 employees.
b) read data into the array
c) calculate and display the number of employees with salary less than RM1000.00
d) calculate and display the number of employees with salary more than RM2000.00
e) determine and display the highest salary
Sep 5, 2011 at 3:50am
well, are you given any data about the employees wages?
Sep 5, 2011 at 3:55am
no. and maybe the user have to enter the salary by himself.
Sep 5, 2011 at 3:57am
I would guess that you should make a text file (for instance, 100 lines with one salary on each line). I don't know about you but I wouldn't want to enter 100 salaries every time the program is run.
Sep 5, 2011 at 4:13am
i was thinking make an array thats 100 ints big, then have a for loop put in random numbers for it and use an if clause to catch ones above and below the trigger points, setting them into their own respective arrays. then set up a while loop to compare them until you have the largest one, and have it printed out at the end. unless you want it to show the same values every time, then you are going to have to make a file to read from *shrugs* either way it sounds like a fun little challenge
Sep 5, 2011 at 4:18am
i forgot about the question..the program should read the data into array..

shacktar: i don't understand.can you help me with the coding??
Sep 5, 2011 at 5:22am
It would actually be easier to randomly generate the 100 salaries then to read them from a file. If "read data into the array" means reading from a file, then do that. Otherwise, I would ask for clarification.

Here's information about the rand() function.
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

shacktar: i don't understand.can you help me with the coding??

OK, first you'd make a text file (.txt). Then, on each line put a salary.

(salaries.txt)
1
2
3
4
5
6
1500
2500
700
1200
3000
...


And here's code to read the salaries line by line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <fstream>
#include <string>
using namespace std;

int main()
{
     ifstream inputFile("salaries.txt"); //this should contain the full path to salaries.txt

     if( inputFile.is_open() )
     {
          //the file was opened successfully

          //read the file line by line
          while( inputFile.good() )
          {
               //read the current line as a string
               string currentLine;
               getline( inputFile, currentLine );

               //processing code for the current line goes here
          }

          //close the file
          inputFile.close();
     }

     return 0;
}


http://www.cplusplus.com/doc/tutorial/files/
Sep 5, 2011 at 6:04am
If your Teacher/Professor expects the user to input 100 numbers, then he's out of his mind. I think you're missing some information.

You can take Shacktar's idea, read up on the fstream class, getline(), and vectors.

If you can at least show me some code you've written, I'll try to help you.

Edit: Looks like Shacktar got to it before me.
Last edited on Sep 5, 2011 at 7:17am
Sep 11, 2011 at 3:53am
yaa thanks a lot guys..thanks for showing me the coding and all the ideas
Topic archived. No new replies allowed.