container or what do i need to learn?

Hi

i need to create a program where i have to ask for a name then ask for a number. multiple times.

in the program i made before i did:

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# include <iostream>
# include <cstdlib>


using namespace std;

int main()
{

	float  baseSalary, hours, tax, totalAmount;
	char userName [50];

	cout << "Type user name: " ;
	cin.getline (userName,50);
	cout <<  "Type amount of worked hours: ";
	cin >> hours ;
	cout <<  "type salary per hour: " ;
	cin >> baseSalary ;

	system ("cls");

	float salary=0.0;

		if (hours>0 && hours<=40 )
		{
			salary=hours*baseSalary;
		}
		else if ( hours>40 && hours<=50)
		{
			salary=(baseSalary*40+(hours-40)*baseSalary*2);
		}
		else if (hours>50)
		{
			salary=(baseSalary*40+(baseSalary*10*2)+(hours-50)*(baseSalary*3));
		}

		//calculate the tax value
		tax=(salary*0.12);
		//calculate total Amount
		totalAmount=salary-tax;

	cout << userName<< endl;
	cout << "Worked hours: " << hours << endl;
	cout << "Salary: $" << totalAmount << endl;

	system ("pause");
	return 0;
}


now i gotta do something similar but with many users
i think i could add more users like this:

1
2
3
4
5
6
7
float  baseSalary1, baseSalary2, baseSalary3, tax, totalAmount;
	char userName1 [50],userName2 [50],userName3 [50];

	cout << "Type your name: " ;
	cin.getline (userName1,50);
	cout <<  "type salary per hour: " ;
	cin >> baseSalary1 ;

but what i want to know is how to do it in a more efficient way without the need of writing many times
baseSalary1, baseSalary2, baseSalary3...etc


today i was reading about vectors and i saw you could make them grow with push_back or insert. is there any way to make my program in a similar way ?

i was thinking something like

usersName.insert(userName.begin(), "name"); // i know is not the syntax but is the idea. i just don't know how to do it.

i need to be able to get from user input a name to which i will assign a number then ask the user if wants to add another name ... etc

hope someone can point me to the right direction, letting me know about what should i read, may be a container?

thanks in advance

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct User
{
std::string name; //Note the use of string, and not a char array
int salary;
};

std::vector<User> vectorUsers;

do
{
vectorUsers.push_back(getUserInfo());
continue = promptForContinue();
}
while(continue == true);


You'll have to implement the functions yourself.
Vector's push_back method simply sticks the data provided onto the back of the vector. To see how big a vector is use size().
getUserInfo in this case will have to return a struct of type User, which will be pushed onto your vector.
promptForContinue will simply ask the user if he wants to continue and return the corresponding boolean value.
Also, strings are much nicer to work with than character arrays. Get used to using them. You're writing C++ not C.
Some references:
Strings: http://www.cplusplus.com/reference/string/string/
Vectors: http://www.cplusplus.com/reference/stl/vector/

EDIT: You should also learn to use getline for all your input operations. It is much safer. Look up how to convert strings to numbers once you need that.
http://www.cplusplus.com/reference/string/getline/
Last edited on
Topic archived. No new replies allowed.