Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6872041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:52:42+00:00 2026-05-27T03:52:42+00:00

I am having trouble pinpointing where exactly my file input is going wrong. Here

  • 0

I am having trouble pinpointing where exactly my file input is going wrong. Here is the code:

  char tempFirst[20], tempLast[20], tempCourse[7];
  char c;          // For peeking


  // Find out how many students are in the file
  inFile >> numStudents;

  for (int i = 0; i < numStudents; i++)
    {
      // Get last name from file
      inFile.getline(tempLast, 20, ',');

      // Gets rid of any spaces inbetween last and first
      while (isspace(inFile.peek()))
        c = inFile.get();
      // Get first name from file
      inFile.getline(tempFirst, 20, '\n');

      // New line, get course
      inFile >> tempCourse;

      // PRINT
      cout << tempFirst << "\n" << tempLast << "\n"
             << tempCourse << "\n";

      list[i]->SetGrades(inFile);
    }

SetGrade leads to one of these three inherited functions:

void EnglishStudent::SetGrades(ifstream& inFile)
{
  inFile >> attendance >> project >> midterm >> final;
  cout << attendance << " " << project << " " << midterm << " " << final << "\n\n";
}

void HistoryStudent::SetGrades(ifstream& inFile)
{
  inFile >> paper >> midterm >> final;
  cout << paper << " " << midterm << " " << final << "\n\n";
}

void MathStudent::SetGrades(ifstream& inFile)
{
  inFile >> quiz1 >> quiz2 >> quiz3 >> quiz4 >> quiz5
         >> test1 >> test2 >> final;
  cout << quiz1 << " "<< quiz2 << " " << quiz3 << " " << quiz4 << " " << quiz5
       << " " << test1 << " " << test2 << " " << final << "\n\n";
}

Here is the file I am loading the information in from:

6
Bunny, Bugs
Math 90 86 80 95 100 99 96 93
Schmuckatelli, Joe
History 88 75 90
Dipwart, Marvin
English 95 76 72 88
Crack Corn, Jimmy
Math 44 58 23 76 50 59 77 68
Kirk, James T.
English 40 100 68 88
Lewinsky, Monica
History 60 72 78

Then here is the output:

Bugs

Bunny
Math
90 86 80 95 100 99 96 93

Joe

History
88 75 90

Marvin

English
95 76 72 88

Jimmy

Crack Corn
Math
44 58 23 76 50 59 77 68

James T.

English
40 100 68 88

Monica

History
60 72 78

I am missing most last names and for the first student, firstname has an endline included. How would I go about fixing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:52:43+00:00Added an answer on May 27, 2026 at 3:52 am

    It’s not that the first name has a newline at the end, it’s that the last name has a newline at the beginning. When the ints are read from the input, any whitespace encountered that marks the end of the int is left in the input stream.

    To fix this, either remove whitespace before reading the last name, in the SetGrades methods or at the end of the loop. The latter two will also require removing whitespace after reading numStudents. The easiest way of removing whitespace is with the ws stream manipulator. All it takes is:

    inFile >> ws;
    

    You can also replace your peeking loop with this.

    Replace the character arrays with strings for a more genuine C++ experience. This also necessitates replacing ifstream::getline with the getline free function. As a bonus, your code will work for names longer than 19 characters.

        std::string tempFirst, tempLast, tempCourse;
    
        ...
        for (int i=0; i < numStudents; ++i) {
            inFile >> std::ws;
            getline(inFile, last, ',');
    
            inFile >> std::ws;
            getline(inFile, first, '\n');
            ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble pinpointing exactly why query #1 is returning less records than
Having trouble linking the Stomp.framework into an iPhone SDK application. http://code.google.com/p/stompframework/ I follow the
Having trouble with the following segment of code. I'm getting a parameter count mismatch.
Im having trouble reading from a CSV file final String DELIMITER = ,; Scanner
having trouble getting my directx going I get the following error 1>Linking... 1>main.obj :
Having trouble determining if a file name conforms to a specific convention as follows.
Having trouble rewriting the name of a flash file: /flash/shell.1257347618.swf (the numbers are a
Having trouble where I think I need to provide most of my code to
Having trouble getting the javascript alert to display from my code behind. c# -
Having trouble with grepping and cutting at the same time I have a file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.