why the first line of datafile is being skipped c++
up vote
1
down vote
favorite
While running this loop for my school assignment, it seems to be skipping the first line of the data file I am pulling from .
Here is the data file
Joe Johnson 89
Susie Caldwell 67
Matt Baker 100
Alex Anderson 87
Perry Dixon 55
The output I am getting is :
Caldwell,Susie D
Baker,Matt A
Anderson,Alex B
Dixon,Perry F
Class average 77.25
Here is the code.
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
// Variable declarations:
string fName[10];
string lName[10];
float grade_Average;
string file;
string name;
int scores[10];
float sum = 0;
char grade;
int i = 0;
ifstream din;
// Function body:
cout << "Enter the name of the file. " << endl;
cin >> file;
din.open(file.c_str());
if (!din)
{
cout << " Cannot open the input file. Please try again." << endl;
return 0;
}
cout << setw(10) << setfill(' ') << "Name" <<setw(20)<<setfill(' ')<< "Grade" << endl;
din >> fName[i];
din >> lName[i];
din >> scores[i];
while (!din.eof())
{
i++;
din >> fName[i];
din >> lName[i];
din >> scores[i];
sum = sum + scores[i];
switch (static_cast<int> (scores[i]/10))
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
grade = 'F';
break;
case 6:
grade = 'D';
break;
case 7:
grade = 'C';
break;
case 8:
grade = 'B';
break;
case 9:
grade = 'A';
break;
case 10:
grade = 'A';
break;
default:
cout << "Invalid score." << endl;
}
name = lName[i] + ',' + fName[i];
cout << setw(10) << setfill(' ') << name << setw(20) << setfill(' ')<<(" ") << grade << endl;
}
grade_Average = sum / i;
cout << "Class average " << grade_Average << endl;
din.close();
return 0;
}
// end function main
c++ arrays while-loop data-files
add a comment |
up vote
1
down vote
favorite
While running this loop for my school assignment, it seems to be skipping the first line of the data file I am pulling from .
Here is the data file
Joe Johnson 89
Susie Caldwell 67
Matt Baker 100
Alex Anderson 87
Perry Dixon 55
The output I am getting is :
Caldwell,Susie D
Baker,Matt A
Anderson,Alex B
Dixon,Perry F
Class average 77.25
Here is the code.
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
// Variable declarations:
string fName[10];
string lName[10];
float grade_Average;
string file;
string name;
int scores[10];
float sum = 0;
char grade;
int i = 0;
ifstream din;
// Function body:
cout << "Enter the name of the file. " << endl;
cin >> file;
din.open(file.c_str());
if (!din)
{
cout << " Cannot open the input file. Please try again." << endl;
return 0;
}
cout << setw(10) << setfill(' ') << "Name" <<setw(20)<<setfill(' ')<< "Grade" << endl;
din >> fName[i];
din >> lName[i];
din >> scores[i];
while (!din.eof())
{
i++;
din >> fName[i];
din >> lName[i];
din >> scores[i];
sum = sum + scores[i];
switch (static_cast<int> (scores[i]/10))
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
grade = 'F';
break;
case 6:
grade = 'D';
break;
case 7:
grade = 'C';
break;
case 8:
grade = 'B';
break;
case 9:
grade = 'A';
break;
case 10:
grade = 'A';
break;
default:
cout << "Invalid score." << endl;
}
name = lName[i] + ',' + fName[i];
cout << setw(10) << setfill(' ') << name << setw(20) << setfill(' ')<<(" ") << grade << endl;
}
grade_Average = sum / i;
cout << "Class average " << grade_Average << endl;
din.close();
return 0;
}
// end function main
c++ arrays while-loop data-files
You read once right before the loop, and then again the first thing in the loop, overwriting any previously read values. This has an effect of discarding the first line - you read it, and then immediately read the second line on top of it.
– Igor Tandetnik
Nov 11 at 2:58
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
While running this loop for my school assignment, it seems to be skipping the first line of the data file I am pulling from .
Here is the data file
Joe Johnson 89
Susie Caldwell 67
Matt Baker 100
Alex Anderson 87
Perry Dixon 55
The output I am getting is :
Caldwell,Susie D
Baker,Matt A
Anderson,Alex B
Dixon,Perry F
Class average 77.25
Here is the code.
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
// Variable declarations:
string fName[10];
string lName[10];
float grade_Average;
string file;
string name;
int scores[10];
float sum = 0;
char grade;
int i = 0;
ifstream din;
// Function body:
cout << "Enter the name of the file. " << endl;
cin >> file;
din.open(file.c_str());
if (!din)
{
cout << " Cannot open the input file. Please try again." << endl;
return 0;
}
cout << setw(10) << setfill(' ') << "Name" <<setw(20)<<setfill(' ')<< "Grade" << endl;
din >> fName[i];
din >> lName[i];
din >> scores[i];
while (!din.eof())
{
i++;
din >> fName[i];
din >> lName[i];
din >> scores[i];
sum = sum + scores[i];
switch (static_cast<int> (scores[i]/10))
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
grade = 'F';
break;
case 6:
grade = 'D';
break;
case 7:
grade = 'C';
break;
case 8:
grade = 'B';
break;
case 9:
grade = 'A';
break;
case 10:
grade = 'A';
break;
default:
cout << "Invalid score." << endl;
}
name = lName[i] + ',' + fName[i];
cout << setw(10) << setfill(' ') << name << setw(20) << setfill(' ')<<(" ") << grade << endl;
}
grade_Average = sum / i;
cout << "Class average " << grade_Average << endl;
din.close();
return 0;
}
// end function main
c++ arrays while-loop data-files
While running this loop for my school assignment, it seems to be skipping the first line of the data file I am pulling from .
Here is the data file
Joe Johnson 89
Susie Caldwell 67
Matt Baker 100
Alex Anderson 87
Perry Dixon 55
The output I am getting is :
Caldwell,Susie D
Baker,Matt A
Anderson,Alex B
Dixon,Perry F
Class average 77.25
Here is the code.
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
// Variable declarations:
string fName[10];
string lName[10];
float grade_Average;
string file;
string name;
int scores[10];
float sum = 0;
char grade;
int i = 0;
ifstream din;
// Function body:
cout << "Enter the name of the file. " << endl;
cin >> file;
din.open(file.c_str());
if (!din)
{
cout << " Cannot open the input file. Please try again." << endl;
return 0;
}
cout << setw(10) << setfill(' ') << "Name" <<setw(20)<<setfill(' ')<< "Grade" << endl;
din >> fName[i];
din >> lName[i];
din >> scores[i];
while (!din.eof())
{
i++;
din >> fName[i];
din >> lName[i];
din >> scores[i];
sum = sum + scores[i];
switch (static_cast<int> (scores[i]/10))
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
grade = 'F';
break;
case 6:
grade = 'D';
break;
case 7:
grade = 'C';
break;
case 8:
grade = 'B';
break;
case 9:
grade = 'A';
break;
case 10:
grade = 'A';
break;
default:
cout << "Invalid score." << endl;
}
name = lName[i] + ',' + fName[i];
cout << setw(10) << setfill(' ') << name << setw(20) << setfill(' ')<<(" ") << grade << endl;
}
grade_Average = sum / i;
cout << "Class average " << grade_Average << endl;
din.close();
return 0;
}
// end function main
c++ arrays while-loop data-files
c++ arrays while-loop data-files
edited Nov 11 at 5:59
asked Nov 11 at 2:48
Wessley Ray Cone
12
12
You read once right before the loop, and then again the first thing in the loop, overwriting any previously read values. This has an effect of discarding the first line - you read it, and then immediately read the second line on top of it.
– Igor Tandetnik
Nov 11 at 2:58
add a comment |
You read once right before the loop, and then again the first thing in the loop, overwriting any previously read values. This has an effect of discarding the first line - you read it, and then immediately read the second line on top of it.
– Igor Tandetnik
Nov 11 at 2:58
You read once right before the loop, and then again the first thing in the loop, overwriting any previously read values. This has an effect of discarding the first line - you read it, and then immediately read the second line on top of it.
– Igor Tandetnik
Nov 11 at 2:58
You read once right before the loop, and then again the first thing in the loop, overwriting any previously read values. This has an effect of discarding the first line - you read it, and then immediately read the second line on top of it.
– Igor Tandetnik
Nov 11 at 2:58
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
The first line isn't being skipped, but you never print out any results for it:
din >> fName[i];
din >> lName[i];
din >> scores[i];
while (!din.eof())
{
i++;
Note that in the first three lines of the above code, you read in values for fName[0], lName[0], and scores[0], but then you increment i (inside the while loop), and never look at those values again.
You'd be better off deleting those three lines, and moving the i++; line to the end of your while-loop, instead.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
The first line isn't being skipped, but you never print out any results for it:
din >> fName[i];
din >> lName[i];
din >> scores[i];
while (!din.eof())
{
i++;
Note that in the first three lines of the above code, you read in values for fName[0], lName[0], and scores[0], but then you increment i (inside the while loop), and never look at those values again.
You'd be better off deleting those three lines, and moving the i++; line to the end of your while-loop, instead.
add a comment |
up vote
2
down vote
The first line isn't being skipped, but you never print out any results for it:
din >> fName[i];
din >> lName[i];
din >> scores[i];
while (!din.eof())
{
i++;
Note that in the first three lines of the above code, you read in values for fName[0], lName[0], and scores[0], but then you increment i (inside the while loop), and never look at those values again.
You'd be better off deleting those three lines, and moving the i++; line to the end of your while-loop, instead.
add a comment |
up vote
2
down vote
up vote
2
down vote
The first line isn't being skipped, but you never print out any results for it:
din >> fName[i];
din >> lName[i];
din >> scores[i];
while (!din.eof())
{
i++;
Note that in the first three lines of the above code, you read in values for fName[0], lName[0], and scores[0], but then you increment i (inside the while loop), and never look at those values again.
You'd be better off deleting those three lines, and moving the i++; line to the end of your while-loop, instead.
The first line isn't being skipped, but you never print out any results for it:
din >> fName[i];
din >> lName[i];
din >> scores[i];
while (!din.eof())
{
i++;
Note that in the first three lines of the above code, you read in values for fName[0], lName[0], and scores[0], but then you increment i (inside the while loop), and never look at those values again.
You'd be better off deleting those three lines, and moving the i++; line to the end of your while-loop, instead.
answered Nov 11 at 2:58
Jeremy Friesner
37.8k1077157
37.8k1077157
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245422%2fwhy-the-first-line-of-datafile-is-being-skipped-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You read once right before the loop, and then again the first thing in the loop, overwriting any previously read values. This has an effect of discarding the first line - you read it, and then immediately read the second line on top of it.
– Igor Tandetnik
Nov 11 at 2:58