Very confused. Program skipping all lines of input

So im coding a project for school. Im really confused as to why when I run this, it does not work like it should. It prompts me to enter the name of the first employee, but then skips right to the end of the program. Im sure there are a lot of errors but I have no idea what they are so help would be much appreciated. This is a beginners class and it seems a little too difficult to me.
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;

    float const Fweigh=0.21;
    float const Spweigh=0.22;
    float const Suweigh=0.23;
    float const Yweigh=0.34;
    
    const int EMP_NUM = 10;
    const int EMP_SCORES = 9;
    

    char name[EMP_NUM][EMP_SCORES];
    char id[EMP_NUM][EMP_SCORES];
    float feval[EMP_NUM][EMP_SCORES];
    float speval[EMP_NUM][EMP_SCORES];
    float sueval[EMP_NUM][EMP_SCORES];
    float ceval[EMP_NUM][EMP_SCORES];
    float final[EMP_NUM][EMP_SCORES];
    
    ofstream ofile;
    ifstream ifile;

void getData()
{
    
    
    
    ofile.open ("EricInputData.txt");
      
      cout << "GET DATA FUNCTION\n";
      for (int num = 0; num < 10; num++)
      {
      cout << "Enter Employee's name: \n";
      cin >> name[num][0];
      ofile << name[num][0] << endl;
      
     
      cout << "Enter Employee ID#: \n";
      cin >> id[num][1];
      ofile << id[num][1] << endl;
      
      
     
      cout << "Enter Fall Evaluation: \n";
      cin >> feval[num][2];
      while (feval[num][2] > 100 || feval[num][2] < 0)
      {
            cout << "Please enter a valid evaluation number (0-100).\n";
            cin >> feval[num][2];
      } 
      ofile << feval[num][2] << endl;
      
      
      cout << "Enter Spring Evaluation: \n";
      cin >> speval[num][3];
      while (speval[num][3] > 100 || speval[num][3] < 0)
      {
            cout << "Please enter a valid evaluation number (0-100).\n";
            cin >> speval[num][3];
      } 
      ofile << speval[num][3] << endl;
      
     
      cout << "Enter Summer Evaluation: \n";
      cin >> sueval[num][4];
      while (sueval[num][4] > 100 || sueval[num][4] < 0)
      {
            cout << "Please enter a valid evaluation number (0-100).\n";
            cin >> sueval[num][4];
      } 
      ofile << sueval[num][4] << endl;
      
     
      cout << "Enter Comprehensive Evaluation: \n";
      cin >> ceval[num][5];
      while (ceval[num][5] > 100 || ceval[num][5] < 0)
      {
            cout << "Please enter a valid evaluation number (0-100).\n";
            cin >> ceval[num][5];
      } 
      ofile << ceval[num][5] << endl;
      cin.ignore();
      }   
     
      ofile.close();
}

int getFinal()
{
    cout << "GET FINAL FUNCTION\n";
     for (int num = 0; num < 10; num++)
     {
     final[num][6] = (feval[num][2]*Fweigh)+(speval[num][3]*Spweigh)+
     (sueval[num][4]*Suweigh)+(ceval[num][5]*Yweigh);
     }
     
}

void makeOutFile()
{
          cout << "MAKEOUT FILE FUNCTION\n";
          ifile.open ("EricInputData.txt");
          
          ofile.open ("EricOutputFile.txt");
          
          
          for (int num = 0; num < 10; num++)
          {
          //Reads the employee name from the input file
          ifile >> name[num][0];
          //Formats the output file and puts the employee name in it
          ofile << right << setw(40) << "Name of the Employee: "<< left << setw(15) << name[num][0] << endl;
          
          //Reads the employee id from the input file
          ifile >> id[num][1];
          //Formats the output file and puts the employee id in it
          ofile << right << setw(40) << "Employee ID #: "<< left << setw(15) << id[num][1] << endl;
          
          //Reads the employee fall semester evaluation
          ifile >> feval[num][2];
          //Formats the output file and puts the employee fall semester evaluation in it
          ofile << right << setw(40) << "Fall Semester Evaluation: "<< left << setw(15) << fixed << setprecision(2) << feval[num][2] << endl;
          
          //Reads the employee spring semester evaluation
          ifile >> speval[num][3];
          //Formats the output file and puts the employee spring semester evaluation in  it
          ofile << right << setw(40) << "Spring Semester Evaluation: "<< left << setw(15) << fixed << setprecision(2) << speval[num][3] << endl;
          
          //Reads the employee summer semester evaluation
          ifile >> sueval[num][4];
          //Formats the output file and puts the employee summer semester evaluation in it
          ofile << right << setw(40) << "Summer Semester Evaluation: "<< left << setw(15) << fixed << setprecision(2) << sueval[num][4] << endl;
          
          //Reads the employee comprehensive annual evaluation
          ifile >> ceval[num][5];
          //Formats the output file and puts the employee comprehensive annual evaluation in it
          ofile << right << setw(40) << "Comprehensive Annual Evaluation: "<< left << setw(15) << fixed << setprecision(2) << ceval[num][5] << endl;
          
          //Formats the output file and puts the final weighted evaluation in it
          ofile << right << setw(40) << "Final Weighted Evaluation: "<< left << setw(15) << fixed << setprecision(2) << final[num][6] << endl;
          
          //Starts an if else statement
            if (final[num][6] >= 90)
            {ofile << right << setw(40) << "Quality Grade: " << left << setw(10) << "Excellent" << endl;
            ofile << right << setw(40) << "Recommendation: " << left << setw(10) << "High Raise" << endl;}
  
            else if (final[num][6] >= 80)
            {ofile << right << setw(40) << "Quality Grade: " << left << setw(10) << "Good"<<endl;
             ofile << right << setw(40) << "Recommendation: " << left << setw(10) << "Good Raise"<<endl;}
  
             else if (final[num][6] >= 70)
             {ofile << right << setw(40) << "Quality Grade: " << left << setw(10) << "Satisfactory"<<endl;
             ofile << right << setw(40) << "Recommendation: "<< left << setw(10) << "No Raise"<<endl;}
  
             else if (final[num][6] < 70)
             {ofile << right << setw(40) << "Quality Grade: " << left << setw(10) << "Unsatisfactory"<<endl;
             ofile << right << setw(40) << "Recommendation: "<< left << setw(10) << "Fire"<<endl;}
             ofile << endl;
             
             }
             
             ifile.close();
             ofile.close();
}


int main ()
{
    getData();
    makeOutFile();
    getFinal();
    
    
    system("pause>nul");
    return 0;
}
Surely you don't mean that it does not even ask you to "Enter Employee ID#: \n";?

correct. all it does is ask me to enter the first employees name, and once i do it just skips every other input and goes to the end. Like it shows "Enter Employee ID#: " but it skips the input. it loops correctly, 10 sets of employees and ids and scores, but doesn't let me input anything after the first one.
Well, I see that you have several problems.

It looks like you wanted to use strings, but in fact are using cstrings (as in the language C, not C++. C++ can use cstrings though).


1
2
    char name[EMP_NUM][EMP_SCORES];
    char id[EMP_NUM][EMP_SCORES]; 
Defines a two dimensional array (arrays in C/C++ are fixed lenth) of characters, not of strings. If you WANTED to use cstrings you need to be aware that a cstring is an array of characters ending with a null character '\0'.

So the cstring declaration char hi[]="Hello"; actually defines an array of 6 character elements
hi[0] == 'H'
hi[1] == 'e'
hi[2] == 'l'
hi[3] == 'l'
hi[4] == 'o'
hi[5] == '\0'


That can be treated (sort of) like the strings of other languages.

To use cstrings you would need to define name and id as three dimensional arrays like this:
1
2
    char name[MAX_NAME_LENGTH][EMP_NUM][EMP_SCORES];
    char id[MAX_ID_LENGTH][EMP_NUM][EMP_SCORES]; 


But could still access it as a cstring like this: name[num][0];. Weird, huh?

I recommend that you go with strings, not cstrings.

To do this, since you have already #include <string> , you will have to simply change your name and id variables from char to string:
1
2
    string name[EMP_NUM][EMP_SCORES];
    string id[EMP_NUM][EMP_SCORES]; 


Whichever type of string you choose to use, you'll need to use the getline() function, rather than cin, because cin stops at the first whitespace it runs into (' ', '\t', '\n').
1
2
cin.getline(name, MAX_NAME_LENGTH);  // for cstrings
getline(cin, name);                 // for strings 


So the employee's first name got put into name, the last name got put into ID, and then the carrage-return was causing the other the other cin's trouble because they were looking for an int and could only find a carrage-return. Or something like that.

Here are links on cstring getline
http://www.cplusplus.com/reference/iostream/istream/getline/

and the string getline
http://www.cplusplus.com/reference/string/getline/

Personally it seems that you're jumping ahead of where you should be starting out. There are some very good C++ tutorials on this website, l think you might benefit from checking them out.
Last edited on
Thanks for the response. I agree! I think this is waaay ahead of where a beginners level should be but this is what our teacher is telling us to do. Ok so, it kind of works now. Now its bumping the last name down to employee id and its also not getting the final evaluation calculation (mixed up the functions in the main function). Heres what ive changed. I added the brackets to the variables because I would get an error otherwise.

1
2
3
4
5
6

cout << "Enter Employee's name: \n";
      getline(cin, name[num][0]);

cout << "Enter Employee ID#: \n";
      getline(cin, id[num][1]);
Last edited on
If this is all you've changed is
1
2
3
4
5
cout << "Enter Employee's name: \n";
      getline(cin, name[num][0]);

cout << "Enter Employee ID#: \n";
      getline(cin, id[num][1]);


Then yes, the last name will go into the employee id because you're working with cstrings, not strings. Go back and re-read my last comment. It may be a little long, but I did spell it out for you. You have a choice to make, right now you're hopping back and forth over the line between strings and cstrings and it's confusing you. Pick one.

I'm going to edit it to put some emphasis on some things.
the reason I did that was because I am getting an error whenever I try to compile. this is what I have

1
2
cout << "Enter Employee's name: \n";
      getline(cin, name);


I try to compile but it gives me an error message of

"no matching function for call to `getline(std::istream&, std::string[10][9])' "
no matching function for call to `getline(std::istream&, std::string[10][9])' 


Well, if your original code is still current, that would be because name is not a string.

You declared it as a 10 x 9 array of characters.

1
2
3
    const int EMP_NUM = 10;
    const int EMP_SCORES = 9;
    char name[EMP_NUM][EMP_SCORES];


That is your first problem, because this means that name[0...9] will be a character array 8 characters wide, not long enough for most names. I'm not sure what you think you're doing with EMP_SCORES, but I'm SURE it's not what you want to be doing.

What you ARE doing is creating the following array: (X=unknown character)


name[0] == "XXXXXXXXX"
name[1] == "XXXXXXXXX"
name[2] == "XXXXXXXXX"
name[3] == "XXXXXXXXX"
name[4] == "XXXXXXXXX"
name[5] == "XXXXXXXXX"
name[6] == "XXXXXXXXX"
name[7] == "XXXXXXXXX"
name[8] == "XXXXXXXXX"
name[9] == "XXXXXXXXX"


That's your first problem, as I tried to point out in my second post.

What you've done is create a 2D array of characters.

You can access them as a CSTRING this way, BUT getline is a STRING function, NOT a CSTRING function. This is why you are getting that error.

I should tell you to look it up, but I'll tell you that what you need to do is AT LEAST create an array of strings, (a vector would be better, but we'll stay simple). To do that you need to #include strings, and declare 'name' as a string, like so:
1
2
3
#include<string>
// some code
     string name;


Let me point you to some resources you should read:

http://www.cplusplus.com/doc/tutorial/
http://www.cplusplus.com/doc/tutorial/ntcs/
Last edited on
sorry I really am trying to understand everything. thanks for your help so far. the reason that I am putting everything into arrays is because that is what my teacher is requiring for the project. we are supposed to create different functions and in one of them we are supposed to ask for input and store those different inputs into arrays, and then display that input in an output file along with some other calculations based on the input given by the user.

also i did change name and id to strings they look like this now

1
2
 string name[EMP_NUM][EMP_SCORES];
    string id[EMP_NUM][EMP_SCORES];
Last edited on
Oops, my bad, I didn't declare an array of strings, I created a string.

An array of strings would be like this:
string name[10];

As you're a student in a class like me, then you should have a book. Look up cin.

Here is a start:
http://www.cplusplus.com/reference/iostream/cin/

look at some code examples like this:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main () {
     char fname[20], lname[20];

     cout << "What are your first and last names?\n";
     cin >> fname >> lname;

     cout << "Hello, " << fname << " " << lname << endl;
}

ok so I think I figured it out. The only thing now is I am supposed to change the arrays that I have into one structure of arrays and I really have no idea how to do that
Do you mean you need to use a struct?
ya it says that I need to change the arrays that I used in that program into one structure of arrays using struct. it says its not too hard of a fix but im not really understanding it

Look here to get an idea of how to use struct.
http://www.cplusplus.com/doc/tutorial/structures/
Topic archived. No new replies allowed.