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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:05:02+00:00 2026-06-04T17:05:02+00:00

First, I’m very new to coding in C++. So, I have a .txt file,

  • 0

First, I’m very new to coding in C++.
So, I have a .txt file, with names and numbers–here’s an example.

chris 5

tara 7

Sam 13

Joey 15

I would like to use this code to retrieve the names and numbers, but how does one print specific array entries instead of just the variables name and number (I want it to show the name and the number on the screen)?

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
string name;
int number;
struct sEntry
{
    std::string name;
    int number;
};
sEntry entries[256];
std::ifstream fin("input.txt"); // opens the text file
int nb_entries; // Keeps track of the number of entries read.

for (nb_entries = 0; fin.good() && nb_entries < 256;  nb_entries++) // Keep going until we hit the end of the file:
{
    fin >> entries[nb_entries].name;
    fin >> entries[nb_entries].number;
    cout << "Here, "<< name <<" is name.\n";
    cout << "Here, "<< number <<" is number.\n";
}
}
  • 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-04T17:05:03+00:00Added an answer on June 4, 2026 at 5:05 pm

    You’re writing out name and number, but those aren’t the variables you’ve read. You’ve read array entries.

    Getting it working as simply as possible just comes down to changing your cout lines to be:

    cout << "Here, " << entries[nb_entries].name << " is name.\n";
    cout << "Here, " << entries[nb_entries].number << " is number.\n";
    

    No need for a std::vector, ther’s nothing wrong with how you’ve done it.

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

Sidebar

Related Questions

First I want to thank you guys here, because your posts have gotten me
First of all Im new to vb 2010 and so far have enjoyed what
First off...here's the problem... Give an example of a directed graph G = (V,
First i have declare that i am new to javascript. I have created a
first i have a file name readme then i change it to README commit
First of all, I'm really new to the MVC Asp.Net ideology. I would like
First off, let me clarify the platforms we are using. We have an ASP.NET
First, apologies for my English, I hope it makes sense what I`ve written here.
First time posting here, will try to be succinct. This is a classic 'can't
First, I have a dbcontext factory which is defined public class DatabaseFactory : Disposable,

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.