average is outputting as inf










0















I am trying to calculate the average for a class from scores given using a data file that was given.



The formula I'm using is grade_Average = sum / i;



The data file that was given is :



Joe Johnson 89
Susie Caldwell 67
Matt Baker 100
Alex Anderson 87
Perry Dixon 55


The output I am getting is



Johnson,Joe B
Caldwell,Susie D
Baker,Matt A
Anderson,Alex B
Dixon,Perry F


Class average inf



I am not sure if I have the formula wrong or if the formula is in the wrong place.



#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;

while (!din.eof())


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;

i++;


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;










share|improve this question
























  • stackoverflow.com/questions/5605125/…, and use a debugger.

    – user657267
    Nov 11 '18 at 6:16











  • Definitely use a debugger if you do c++ :)

    – L.C.
    Nov 11 '18 at 6:19











  • i is only incremented if an invalid score is entered. Which means it will have a value of zero if all scores are valid. Then it is used in the statementgrade_average = sum/i where sum andgrade_average are of type float. With IEEE floating point format (not guaranteed, but probably what you have) division by zero results in an infinity.

    – Peter
    Nov 11 '18 at 9:03















0















I am trying to calculate the average for a class from scores given using a data file that was given.



The formula I'm using is grade_Average = sum / i;



The data file that was given is :



Joe Johnson 89
Susie Caldwell 67
Matt Baker 100
Alex Anderson 87
Perry Dixon 55


The output I am getting is



Johnson,Joe B
Caldwell,Susie D
Baker,Matt A
Anderson,Alex B
Dixon,Perry F


Class average inf



I am not sure if I have the formula wrong or if the formula is in the wrong place.



#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;

while (!din.eof())


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;

i++;


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;










share|improve this question
























  • stackoverflow.com/questions/5605125/…, and use a debugger.

    – user657267
    Nov 11 '18 at 6:16











  • Definitely use a debugger if you do c++ :)

    – L.C.
    Nov 11 '18 at 6:19











  • i is only incremented if an invalid score is entered. Which means it will have a value of zero if all scores are valid. Then it is used in the statementgrade_average = sum/i where sum andgrade_average are of type float. With IEEE floating point format (not guaranteed, but probably what you have) division by zero results in an infinity.

    – Peter
    Nov 11 '18 at 9:03













0












0








0








I am trying to calculate the average for a class from scores given using a data file that was given.



The formula I'm using is grade_Average = sum / i;



The data file that was given is :



Joe Johnson 89
Susie Caldwell 67
Matt Baker 100
Alex Anderson 87
Perry Dixon 55


The output I am getting is



Johnson,Joe B
Caldwell,Susie D
Baker,Matt A
Anderson,Alex B
Dixon,Perry F


Class average inf



I am not sure if I have the formula wrong or if the formula is in the wrong place.



#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;

while (!din.eof())


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;

i++;


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;










share|improve this question
















I am trying to calculate the average for a class from scores given using a data file that was given.



The formula I'm using is grade_Average = sum / i;



The data file that was given is :



Joe Johnson 89
Susie Caldwell 67
Matt Baker 100
Alex Anderson 87
Perry Dixon 55


The output I am getting is



Johnson,Joe B
Caldwell,Susie D
Baker,Matt A
Anderson,Alex B
Dixon,Perry F


Class average inf



I am not sure if I have the formula wrong or if the formula is in the wrong place.



#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;

while (!din.eof())


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;

i++;


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;







c++ arrays while-loop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 '18 at 7:11









bolov

31k669130




31k669130










asked Nov 11 '18 at 6:11









Wessley Ray ConeWessley Ray Cone

12




12












  • stackoverflow.com/questions/5605125/…, and use a debugger.

    – user657267
    Nov 11 '18 at 6:16











  • Definitely use a debugger if you do c++ :)

    – L.C.
    Nov 11 '18 at 6:19











  • i is only incremented if an invalid score is entered. Which means it will have a value of zero if all scores are valid. Then it is used in the statementgrade_average = sum/i where sum andgrade_average are of type float. With IEEE floating point format (not guaranteed, but probably what you have) division by zero results in an infinity.

    – Peter
    Nov 11 '18 at 9:03

















  • stackoverflow.com/questions/5605125/…, and use a debugger.

    – user657267
    Nov 11 '18 at 6:16











  • Definitely use a debugger if you do c++ :)

    – L.C.
    Nov 11 '18 at 6:19











  • i is only incremented if an invalid score is entered. Which means it will have a value of zero if all scores are valid. Then it is used in the statementgrade_average = sum/i where sum andgrade_average are of type float. With IEEE floating point format (not guaranteed, but probably what you have) division by zero results in an infinity.

    – Peter
    Nov 11 '18 at 9:03
















stackoverflow.com/questions/5605125/…, and use a debugger.

– user657267
Nov 11 '18 at 6:16





stackoverflow.com/questions/5605125/…, and use a debugger.

– user657267
Nov 11 '18 at 6:16













Definitely use a debugger if you do c++ :)

– L.C.
Nov 11 '18 at 6:19





Definitely use a debugger if you do c++ :)

– L.C.
Nov 11 '18 at 6:19













i is only incremented if an invalid score is entered. Which means it will have a value of zero if all scores are valid. Then it is used in the statementgrade_average = sum/i where sum andgrade_average are of type float. With IEEE floating point format (not guaranteed, but probably what you have) division by zero results in an infinity.

– Peter
Nov 11 '18 at 9:03





i is only incremented if an invalid score is entered. Which means it will have a value of zero if all scores are valid. Then it is used in the statementgrade_average = sum/i where sum andgrade_average are of type float. With IEEE floating point format (not guaranteed, but probably what you have) division by zero results in an infinity.

– Peter
Nov 11 '18 at 9:03












2 Answers
2






active

oldest

votes


















0














Your i++ is inside the default block. The i variable is most probably 0. Either you put i++ outside of the switch block or you put it before every break statement.






share|improve this answer






























    0














    i++ is inside the switch block and in the default case. It will never be executed for the given input. Therefore throughout the run i will just be 0. Dividing by 0 gives you inf.



    Your program will also fail if more than 10 entries are given (and the first issue is corrected). You should use std::vector<std::string>, std::vector<int> and push_back instead of the raw arrays of std::string and int, if these arrays are needed at all. (For just calculating the average, the individual entries don't really need to be saved.)






    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',
      autoActivateHeartbeat: false,
      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%2f53246322%2faverage-is-outputting-as-inf%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Your i++ is inside the default block. The i variable is most probably 0. Either you put i++ outside of the switch block or you put it before every break statement.






      share|improve this answer



























        0














        Your i++ is inside the default block. The i variable is most probably 0. Either you put i++ outside of the switch block or you put it before every break statement.






        share|improve this answer

























          0












          0








          0







          Your i++ is inside the default block. The i variable is most probably 0. Either you put i++ outside of the switch block or you put it before every break statement.






          share|improve this answer













          Your i++ is inside the default block. The i variable is most probably 0. Either you put i++ outside of the switch block or you put it before every break statement.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 '18 at 6:18









          L.C.L.C.

          310112




          310112























              0














              i++ is inside the switch block and in the default case. It will never be executed for the given input. Therefore throughout the run i will just be 0. Dividing by 0 gives you inf.



              Your program will also fail if more than 10 entries are given (and the first issue is corrected). You should use std::vector<std::string>, std::vector<int> and push_back instead of the raw arrays of std::string and int, if these arrays are needed at all. (For just calculating the average, the individual entries don't really need to be saved.)






              share|improve this answer





























                0














                i++ is inside the switch block and in the default case. It will never be executed for the given input. Therefore throughout the run i will just be 0. Dividing by 0 gives you inf.



                Your program will also fail if more than 10 entries are given (and the first issue is corrected). You should use std::vector<std::string>, std::vector<int> and push_back instead of the raw arrays of std::string and int, if these arrays are needed at all. (For just calculating the average, the individual entries don't really need to be saved.)






                share|improve this answer



























                  0












                  0








                  0







                  i++ is inside the switch block and in the default case. It will never be executed for the given input. Therefore throughout the run i will just be 0. Dividing by 0 gives you inf.



                  Your program will also fail if more than 10 entries are given (and the first issue is corrected). You should use std::vector<std::string>, std::vector<int> and push_back instead of the raw arrays of std::string and int, if these arrays are needed at all. (For just calculating the average, the individual entries don't really need to be saved.)






                  share|improve this answer















                  i++ is inside the switch block and in the default case. It will never be executed for the given input. Therefore throughout the run i will just be 0. Dividing by 0 gives you inf.



                  Your program will also fail if more than 10 entries are given (and the first issue is corrected). You should use std::vector<std::string>, std::vector<int> and push_back instead of the raw arrays of std::string and int, if these arrays are needed at all. (For just calculating the average, the individual entries don't really need to be saved.)







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 11 '18 at 6:23

























                  answered Nov 11 '18 at 6:17









                  user10605163user10605163

                  2,848624




                  2,848624



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246322%2faverage-is-outputting-as-inf%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

                      𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                      How do I collapse sections of code in Visual Studio Code for Windows?

                      ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ