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