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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:06:59+00:00 2026-05-29T17:06:59+00:00

A novice at C++, i am trying to create a stats program to practice

  • 0

A novice at C++, i am trying to create a stats program to practice coding. i am hoping to get a text file, read it and store values into arrays on which i can perform mathematical operations. i am stuck here

 main ()
 {
      char output[100];
      char *charptr;
      int age[100];
      ifstream inFile;
      inFile.open("data.txt");
      if(!inFile)
      {
            cout<<"didn't work";
            cin.get();
            exit (1);
      }

      inFile.getline(output,100);
      charptr = strtok(output," ");
      for (int x=0;x<105;x++)
      {
           age[x] = atoi(charptr);
           cout<<*age<<endl;

      }

     cin.get();
}

in the code above, I am trying to store subject ages into the int array ‘age’, keeping ages in the first line of the file. I intend to use strtok as mentioned, but i am unable to convert the tokens into the array.

As you can obviously see, I am a complete noob please bear with me as I am learning this on my own. 🙂

Thanks

P.S: I have read similar threads but am unable to follow the detailed code given there.

  • 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-29T17:07:01+00:00Added an answer on May 29, 2026 at 5:07 pm

    There are a few issues with the for loop:

    • Possibility of going out-of-bounds due to age having 100 elements, but terminating condition in for loop is x < 105
    • No check on charptr being NULL prior to use
    • No subsequent call to strtok() inside for loop
    • Printing of age elements is incorrect

    The following would be example fix of the for loop:

    charptr = strtok(output, " ");
    int x = 0;
    while (charptr && x < sizeof(age)/sizeof(age[0]))
    {
        age[x] = atoi(charptr);
        cout << age[x] << endl;
        charptr = strtok(NULL, " ");
        x++;
    }
    

    As this is C++, suggest:

    • using std::vector<int> instead of a fixed size array
    • use the std::getline() to avoid specifying a fixed size buffer for reading a line
    • use std::copy() with istream_iterator for parsing the line of integers

    For example:

    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <iterator>
    
    int main ()
    {
        std::vector<int> ages;
        std::ifstream inFile;
        inFile.open("data.txt");
        if(!inFile)
        {
            std::cout<<"didn't work";
            std::cin.get();
            exit (1);
        }
    
        std::string line;
        std::getline(inFile, line);
    
        std::istringstream in(line);
    
        std::copy(std::istream_iterator<int>(in),
                  std::istream_iterator<int>(),
                  std::back_inserter(ages));
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm an encryption novice trying to pass some values back and forth between systems.
I am a novice in android and trying to create a small app. my
I'm a JavaScript novice trying to build a basic share button as a learning
I am a novice programmer who is trying to teach myself to code, specifically
I'm a Python novice, trying to use pyCurl. The project I am working on
I am a Delphi novice, but I'm trying to understand the relationship between TApplication
I'm a novice when it comes to SQL and PHP, and I'm trying to
I am a novice programmer at best, but I am trying to play a
As a novice developer who is getting into the rhythm of my first professional
I'm trying to create some cascading drop downs. I have my States loading via

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.