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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:12:32+00:00 2026-06-11T20:12:32+00:00

im struggling with this part of code , no matter what i try i

  • 0

im struggling with this part of code , no matter what i try i cant get it to read into a record after two lines

the text file contains

Mickey Mouse 
12121
Goofy
24680
Andy Capp
01928
Quasi Modo
00041
end

and the code is

#include<iostream>
#include<string.h>
#include <stdio.h>
#include <windows.h>
#include<iomanip>
#include<conio.h>
#include<fstream>
#include<string>
using namespace std;

struct record          
{               
char name[20];
int number;
 };



void main()
{


record credentials[30];
    int row=0; 
fstream textfile;//fstream variable
textfile.open("credentials.txt",ios::in);
textfile.getline (credentials[row].name,30);
//begin reading from test file, untill it reads end
while(0!=strcmp(credentials[row].name,"end"))
{ 

    textfile>>credentials[row].number;

    row++;
    //read next name ....if its "end" loop will stop
    textfile.getline (credentials[row].name,30);
}
textfile.close();

}

the record is only taking the first two lines and the rest is empty
any ideas ??

  • 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-11T20:12:33+00:00Added an answer on June 11, 2026 at 8:12 pm

    The problem is that:

    textfile>>credentials[row].number;
    

    while not consume the newline character. The subsequent call to textfile.getline() reads a blank line and the next:

    textfile>>credentials[row].number;
    

    attempts to read "Goofy" into an int which fails and set the failbit of the textfile stream meaning all further attempts to read fail. Check the return value to detect failure:

    if (textfile >> credentials[row].number)
    {
        // Success.
    }
    

    I am not entirely sure how the program ends as "end" will never be read but I suspect it ends abnormally as there is no mechanism to prevent overruning the end of the credentials array (i.e no row < 30 as part of the loop terminating condition).


    Other:

    • Instead of using a fixed sized char[] to read the names into you can use std::getline():

      #include <string>
      
      struct record
      {
          std::string name;
          int number;
      };
      
      if (std::getline(textfile, credentials[row].name))
      {
      }
      
    • Instead of using a fixed sized record[] you could use a std::vector<record> which will grow as required.

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

Sidebar

Related Questions

I've been struggling with this for a while. Basically, I want to have two
I have been struggling with this error for more than two hours: error: aggregate
This code is from an Access 2007 project I've been struggling with. The actual
Really struggling with this simple code here. I'm checking if the section with #setactionsuccess
I'm struggling to find an addon/plugin for this and its a key part of
Im struggling with this one. I have a list of items and on a
Been struggling with this simple selector problem a couple of hours now and must
Been struggling with this for quite some time so any help is appreciated. Spent
I have been struggling with this for a while now. given a set of
I've been struggling with this error for quite some time: Redis::ProtocolError: Got 'i' as

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.