I have written a small program to allow the user to enter 10 student data. Now, my loop exits after the first data. So basically the input stops after entering the first set of values...what is the function that I could use so that the compiler accpets input as long as the user needs to enter input?
is it cin.ignore()?
------------------------------------------------
int main()
{
sal e1;
for(int i=0;i<5;i++)
{
cout<< "Name: " << e1.getName()<< endl;
cout<< "Salary for employee "<< i+1 << " is: " << e1.getSalary()<< endl;
cout<< "Press enter to continue....." <<endl;
}
return 0;
}
----------------------------------------------------------
Above is the code for the main function. I haven't included the class andm ember functions here.
So once the loop goes through one iteration, it terminates. will the cin.ignore help?
Well, from what I can see, you're only using five iterations to put in ten students, and that's going to cause a problem. Also, I'm pretty sure that cout << "Press enter to continue....." << endl;
on it's own won't do jack, except just type the words on the screen, it won't halt the program.
In addition, without knowing what the sal class' member functions (i.e. getName() and getSalary()) actually do, I'm pretty sure I can't provide you with the most accurate solution to your frustration.
Ok, I agree with you on the cout statement. I'm trying to just enter 5 inputs, so that's not an issue.
I think I"ll go ahead and post my program here, hoping that it causes no issues :)
#include <iostream>
#include<string>
usingnamespace std;
class sal{
private:
string ename;
double esalary;
public:
sal (){}
string getName(){ return ename;}
double getSalary(){ return esalary;}
};
sal read_employee(){ // This function looks good in theory, but it's not part of the class.
string name;
cout << "Please enter the name: ";
getline(cin, name);
double salary;
cout << "Please enter the salary: ";
cin >> salary;
sal r(name, salary); // Function call to a function that doesn't exist?
return r;
}
int main(){
sal e1; // Static declarations allow only one object. If you only use the object once,
// that's fine, but if you want to remember it, bad juju.
for(int i=0;i<5;i++){
e1 = read_employee();
cout<< "Name: " << e1.getName()<< endl;
cout<< "Salary for employee "<< i+1 << " is: " << e1.getSalary()<< endl;
cout<< "Press enter to continue....." <<endl;
}
return 0;
}
ok got it!
Thanks. this program works fine, However, it accepts only the first input and then exits....do you get what I mean?
If I don't want static declarations then I use the new operator?
Can you post your output? It would help to know if it even gets to the point where it's accepting the salary, or if it prints out the name that you entered in to it.
Please enter the name: erewrwr
Please enter the salary: 3333
Name: erewrwr
Salary for employee 1 is: 3333
Press enter to continue.....
-------------------
this is the output. It exits after accepting this output. So yes it accepts name and salary. Looks like I need a cin.ignore() before return 0??