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









share|improve this question
























  • 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















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









share|improve this question
























  • 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













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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 2:58









        Jeremy Friesner

        37.8k1077157




        37.8k1077157






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly