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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:11:49+00:00 2026-06-14T05:11:49+00:00

For my c++ homework, I have an .txt document containing building information ordered like:

  • 0

For my c++ homework, I have an .txt document containing building information ordered like:

Building name
year built
lat coordinate
lon coordinate

ex.

Parking Deck
1993
34.2252
37.5563
Admin Building
1985
34.2356
37.5734

I have to read this into an array of my created struct:

struct list 
{
    char name[50];
    int yearBuilt;
    double latCoord;
    double lonCoord;
} building;

now I’ve created a for loop to read in the data to my created array of type list:

list buildingnumber[SIZE]; //array for buildings

But when I try to print out the “k” earliest buildings made, it shows no data in the array

Here’s my current code:

#include <iostream>
#include <fstream>
#include <istream>
#include <cstdlib>
#include <string>

using namespace std;
const int SIZE = 5000;

//struct for building type
struct list 
{
    char name[50];
    int yearBuilt;
    double latCoord;
    double lonCoord;
}building;

list buildingnumber[SIZE]; //array for buildings

void InsertionSort(list buildingnumber[], int buildingsloaded)
{
    int key = 0, i = 0;
    for(int j = 1; j < buildingsloaded; j++)
    {
        key=buildingnumber[j].yearBuilt;
        i=j-1;
        while(buildingnumber[i].yearBuilt > key && i >= 0)
        {
            buildingnumber[i+1] = buildingnumber[i];
            i--;
        }
        buildingnumber[i+1].yearBuilt = key;
    }
}

int main()
{
    char filePath[50];
    ifstream openFile;
    cout << "Enter the path of the building file: ";
    cin.getline(filePath, 50);
    openFile.open(filePath);

    //verify if file is opened + report buildings loaded
    int buildingsloaded = 0;
    if(!openFile.fail())
    {
        while(openFile >> building.name >> building.yearBuilt >> building.latCoord >> building.lonCoord)
        {
            buildingsloaded++;
        }
        cout << buildingsloaded << " buildings have been loaded." << endl;
    }

    // get how many buildings user wants
    int k = 0;
    cout << "How many buildings are you interested in?: ";
    cin >> k;

    //create array
    // loadKbuildings(building, buildingsloaded);
    for(int i = 0; i < k; i++)
    {
        openFile >> buildingnumber[i].name >> buildingnumber[i].yearBuilt >> buildingnumber[i].latCoord >> buildingnumber[i].lonCoord;  
    }

    // insertion sort
    InsertionSort(buildingnumber, buildingsloaded);

    // display earliest k buildings
    cout << "The " << k << " oldest buildings are: " << endl;
    int i = 0;
    while ( i < k )
    {
        cout << buildingnumber[i].name << endl;
        cout << "Year Built: " << buildingnumber[i].yearBuilt << endl;
        cout << "Coordinates: (" << buildingnumber[i].latCoord << "," << buildingnumber[i].lonCoord << ")" << endl;
        cout << endl;
        i++;
    }
}
  • 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-06-14T05:11:50+00:00Added an answer on June 14, 2026 at 5:11 am

    The problem is here:

    if(!openFile.fail())
    {
        while(openFile >> building.name >> building.yearBuilt >> building.latCoord >> building.lonCoord)
        {
            buildingsloaded++;
        }
        cout << buildingsloaded << " buildings have been loaded." << endl;
    }
    

    You’ve read all of the data already (to count the number of buildings); the next time you try to grab the data, it’s gone.

    You can resolve this issue by storing the buildings in an array the first scan through.

    int c = 0;
    if(!openFile.fail())
    {
        // *
        while(openFile >> buildingnumber[c].name >> buildingnumber[c].yearBuilt >> buildingnumber[c].latCoord >> buildingnumber[c].lonCoord)
        {
            buildingsloaded++;
            c++;
        }
        cout << buildingsloaded << " buildings have been loaded." << endl;
    }
    

    As per WhozCraig’s comment, the line under the star only reads in one word for the building’s name; you should use cin.getline() instead and change around the loop condition.

    You should obviously also take out the data reading section below:

    //create array
    // loadKbuildings(building, buildingsloaded);
    for(int i = 0; i < k; i++)
    {
        openFile >> buildingnumber[i].name >> buildingnumber[i].yearBuilt >> buildingnumber[i].latCoord >> buildingnumber[i].lonCoord;  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I have a homework assignment where I need to read two matrix .txt
I have a homework that include a new operation (a [n] b), given: a
I have a homework in which i have 640x480 image and i want to
I have a homework problem here I've been working on; here is the description:
I have a homework task to prove whether or not a particular variation on
I have a homework assignment where I need to take input from a file
I have a homework question that says: Problem 1: Given the array [ 22
I have this homework to do in C. I'm beginner so it is probably
I have a homework assignment to sort an array in ascending order. Obviously, this
I have a Homework Planner application which I want to create an Alarm for

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.