#include <stdio.h>
#include <iostream>
#include <math.h>
#include <sstream>
main ()
{
float sueldo,nomina,suma;
int i;
std::string empresa;
std::cout<<"Introduce el nombre de la empresa:"<<"\n";
std::getline(std::cin,empresa);
for (i=1;i<=12;i++)
{
std::cout<<"Introduce el sueldo del trabajador"<<i<<":"<<"\n";
std::cin>>sueldo;
suma=suma+sueldo;
}
nomina=suma;
std::cout<<"La empresa "<<empresa<<" debe pagar una nomina de: "<<suma;
return 0;
}
I don't have an idea of how to pass this same program using the While cycle, could someone help me, the problem says that 12 salaries are entered and that in the end the payroll to be paid by said company is printed, which was previously requested name too.
These codes are equivalent:
I think with this example you should be able to understand the subtle difference between the two and replace the for in your code with a while.
The advantage of for is being able to write the same code more compactly.
If it is not obvious to you, I will explain how each one is used/works.
Every loop has the purpose of repeating the code in its body whenever a condition is met. The body in all cases is enclosed in braces.
In the case of the while, between parentheses you indicate the condition.
In the case of the for, between parentheses are: