program accepts only one value

Oct 22, 2011 at 2:07am
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()?
Oct 22, 2011 at 2:09am
Post code and output, please. Your description isn't quite descriptive enough.
Oct 22, 2011 at 2:20am
------------------------------------------------
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?
Oct 22, 2011 at 2:35am
Please post your full code and output. I feel like I'm missing things, including apparently some custom variable type named sal?
Oct 22, 2011 at 2:37am
sal is the class. e1 is the object.
Sorry but I'm not sure if I can post the code because it is from my text book and might be a copyright issue.

there is no issue in my code. The only thing is I need the for loop to accept more than one input; currently it terminates after 1 iteration.
Last edited on Oct 22, 2011 at 2:38am
Oct 22, 2011 at 2:42am
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.
Oct 22, 2011 at 2:45am
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 :)
Oct 22, 2011 at 2:47am
#include <iostream>
#include<string>

using namespace std;

class sal
{
private:
string ename;
double esalary;
public:
sal ()
{ }
string getName()
{ return ename;}
double getSalary()
{ return esalary;}
};

sal read_employee()
{
string name;
cout << "Please enter the name: ";
getline(cin, name);
double salary;
cout << "Please enter the salary: ";
cin >> salary;
sal r(name, salary);
return r;
}

int main()
{
sal e1;
for(int i=0;i<5;i++) // Loop to read in 5 employees
{ 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;
}



Oct 22, 2011 at 3:02am
Hokay, so...
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
#include <iostream>
#include<string>

using namespace 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;
}


Here is what you gave me, but I cleaned it up to actually look like code. I also added comments, they should give you a little bit of direction.
Oct 22, 2011 at 3:08am
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?
Oct 22, 2011 at 3:10am
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.
Oct 22, 2011 at 3:14am
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??
Last edited on Oct 22, 2011 at 3:16am
Oct 22, 2011 at 3:47am
Try:
1
2
int i;
for(i = 0; i < 5; i++)
Topic archived. No new replies allowed.