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

  • Home
  • SEARCH
  • 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 8487609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:17:41+00:00 2026-06-10T21:17:41+00:00

The code is as follow : The Code: #include <iostream> #include <fstream> using namespace

  • 0

The code is as follow :

The Code:

#include <iostream>
#include <fstream>

using namespace std;

int main(void)
{
    int id;
    char name[50];
    ifstream myfile("savingaccount.txt");  //open the file
    myfile >> id;

    myfile.getline(name , 255 , '\n');   //read name **second line of the file
    cout << id ;
    cout << "\n" << name << endl; //Error part : only print out partial name 
    return 0;
}

The File Content:

1800567
Ho Rui Jang
21
Female
Malaysian
012-4998192
20 , Lorong 13 , Taman Patani Janam
Melaka
Sungai Dulong

The Problem :

1.)I expect the getline will read the name into the char array name and then I can print out the name , the thing is instead of getting the full name , I only get partial of the name , why this happen?

Thank you!

  • 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-10T21:17:42+00:00Added an answer on June 10, 2026 at 9:17 pm

    The problem is that myfile >> id does not consume the newline (\n) at the end of the first line. Thus, when you call getline it will read from the end of the ID until the end of that line, and you will get an empty string. If you call again getline it will actually return the name.

    std::string name; // By using std::getline() you can use std::string
                      // instead of a char array
    
    myfile >> id;
    std::getline(myfile, name); // this one will be empty
    std::getline(myfile, name); // this one will contain the name
    

    My advise would be to just use std::getline for all the lines and if a line contains a number you can just convert it using std::stoi (if your compiler supports C++11) or boost::lexical_cast.

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

Sidebar

Related Questions

The code is as follow : The Code : #include <iostream> using namespace std;
#include <iostream> using namespace std; class dummyA { int x; public: dummyA () {
I found this sample code on the msdn library #include <iostream> int main() {
#include <iostream> using namespace std; class Base { public: Base() { cout << In
#include <iostream> using namespace std; class Base { public: Base() { cout << In
Here is the code: #include <iostream> #include <string> #include <map> #include <stdexcept> #include <boost/ptr_container/ptr_vector.hpp>
I compile the c++ code using the follow command to disable return value. g++
Lets say we have the following code: #include <iostream> #include <string> struct A {
Using gcc version 4.4.3 as follows: gcc -g -x c++ -lstdc++ -std=c++98 -o ./main
Consider the follow code: TMyList = class(TList<IMyItem>, IMyList) Delphi shows me the error: [DCC

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.