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 7489165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:12:33+00:00 2026-05-29T15:12:33+00:00

I’m having a bit of trouble with a school assignment for C++. The specific

  • 0

I’m having a bit of trouble with a school assignment for C++. The specific problem I’m having involves reading lines from a file that contain a series of between 5 and 6 grades. The grades for each student appear together in a single line following the student’s name and id number. The challenge here is that the grades can have a variable number of spaces between them, and that if there are only 5 grades present, an error message needs to be generated to screen but the program is to continue running and average the 5 grades. Example of input: 23 46 68 85 98

I got the student name and id easily, but the string of digits is giving me problems. My plan was to getline and then tokenize the string and assign each token to a cell in an array. This works fine for 6 grades, but when only 5 grades are present it is assigning garbage to the sixth cell.

Here is the snippet of code that concerns this section:

fin.getline(gradeList, 200);

grade = strtok (gradeList, " ");


while (grade != '\0')
{         
      gradeArr[cycler] = atoi(grade);
      grade = strtok(NULL, " ");
      cycler++;
}

I tried doing an isdigit check on each token before converting it to an int, and writing a 0 in for any token that failed the isdigit check, but that didn’t work at all. It seems like it is pulling the name from the next line when only 5 grades are present and then when it atoi’s it, it changes it to a huge number.

I thought that when the program did getline, it would only grab the line until it saw the endline terminator. Is this not what is happening?

  • 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-29T15:12:33+00:00Added an answer on May 29, 2026 at 3:12 pm

    If you use strtol or strtod, it gives you back a pointer to the end of what you just processed, so you can continue from there. No tokenization necessary 🙂

    But it sounds like your problem is that you’re reading the wrong line. Print out the gradeList variable before you start parsing.

    You should end up with something like this:

    fin.getline(grade_text, 200);
    const char* grade = grade_text;
    const char* next_grade;
    
    double grade_sum = 0;
    for( int grade_count = 0; grades[grade_count] = strtol(grade, &next_grade, 10), next_grade > grade; ++grade_count )
        grade_sum += grades[grade_count];
    double mean_grade = grade_sum / grade_count;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.