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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:47:01+00:00 2026-05-21T08:47:01+00:00

So I’m basically just trying to take in some file input, and then take

  • 0

So I’m basically just trying to take in some file input, and then take that data and put it into several structs. The only issue I’m having is with the naming of the pointers to the structs. The struct’s themselves are supposed to represent students and I wanted to set each pointer as one of their names rather than an arbitrary variable. I tried to do this in a way that I’m assuming is syntactically wrong for it didn’t work. In the code below, I increment the for loop with the temp array because each 4th position is a new student. Any ideas on how I could go about this?

#include<iostream>
#include<iomanip>
#include"student.h"
#include"creditcard.h"
#include<fstream>
using namespace std;

int main ()
{
    string creditcards[20];
    int i;
    int x;
    int amount;
    string temp[20];
    ifstream infile;
    string filename;
    int count;
    int numstudents;
    string newstring="";
    string pointers[20];

    cout<<"enter the file name of which you've stored your"<<endl
        <<"credit card infomation"<<endl;

    getline(cin,filename,'\n');
    infile.open(filename.c_str());

    count=0;
    getline(infile,temp[count],'\n');
    while(! infile.eof())
    {
        count++;
        getline(infile,temp[count],'\n');          

        numstudents= (count/4);
        if(numstudents < 1 || count%4 != 0)
        {
            cout<<"incorrect data file"<<endl;
        }
    }

    cout<<numstudents<<endl;

    for(i=0,x=0; i<numstudents;i++,x+4)
    {
        student *temp[x];
        temp[x] = new student;
        pointers[i] = temp[x];
    }

    for(i=0;i<numstudents;i+4)
    {
        cout<<temp[i]<<endl;
    }

    return 0;
}
  • 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-21T08:47:01+00:00Added an answer on May 21, 2026 at 8:47 am

    Ok, let’s start from the top.

    Your code was (before I reformatted it) a mess. Messy code is harder to read and more likely to have bugs.

    You have 3 arrays, each containing 20 strings. Why do you need so many?

    One of them is named temp; having to use that as a variable name is a good indicator that you’re mishandling data somewhere.

    You’re declaring int count relatively early on, then initializing it to 0 later. While not necessarily a bad thing, that’s not the best method (do both at once, when needed).

    You can declare local variables more than one in a line, but you don’t need to declare them all at the top of the function. That’s not necessary in C++.

    int main ()
    {
        string creditcards[20];
        int i = 0, x = 0, amount = 0;
    

    (legal, but might not be needed)

    It’s typically better to declare and initialize a variable at the same time, just before you need it:

    int count = 0;
    
    getline(infile, temp[count], '\n');
    

    I remember seeing that reading until you hit eof isn’t recommended, although I’m not entirely sure on that. You may want to change this:

    while ( !infile.eof() )
    {
    

    Now, the first actual mistake I see here is that you read a line, increment count, then read another line before acting. Is that intentional, and if so, why is it necessary? Doing the getline and increment inside the loop would be more readable and potentially more reliable.

        count++;
        getline(infile, temp[count], '\n');          
    

    This line is a bug, I think:

     for(i=0,x=0; i<numstudents;i++,x+4)
    

    The last section does i++, x+4. It does not change x.

    The next loop after that handles i in the same way this loop uses x, so you can probably combine those two.

    Now, on top of all that, massive temp arrays are not the solution to this problem (or any other that I can think of).

    To store this kind of data, you’ll want to look into a std::map<std::string, student*> or std::vector<student*>. The vector will allow you to push the new student struct to the back when necessary, and the map will allow you to key them based on name and retrieve that later, something like so:

    typdef map<string, student*> studentmap;
    studentmap students;
    
    studentmap::iterator iter = students.find("Bob");
    if ( iter != students.end() )
    {
        student * bob = iter->second;
        // Work with data
    }
    

    It’s a much better way of handling this, and will take a lot of the guess work out of what you’re doing now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.