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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:41:55+00:00 2026-05-17T17:41:55+00:00

I think this has happened to me before. This is A3.txt : %INSERT MARK

  • 0

I think this has happened to me before. This isA3.txt:

%INSERT
MARK 29 
DAVID 21
JOHN 44
JOHN 51
LARRY 39
MARK 21
DAVID 18
JOHN 28
MARK 35
DONALD 41
PHIL 26

Even though I use sourcefile >> reader at the end of the loop, the program keeps outputting "reader: MARK", meaning the sourcefile >> reader; statement isn’t working (i.e., it keeps getting the same input over and over again, or it’s not getting any input).

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

using namespace std;  

struct data
{
 string name;
 int id;
 data* link;
};

data* start;
data* node;
ifstream sourcefile;

int main()
{
 data* start = new data;

 start -> link = NULL;

 string input;
 string reader;
 sourcefile.open("A3.txt");

 bool firstnode = true;

 sourcefile >> input;

 node = start;

 cout << "Recieved command: " << input << endl;

 if(input == "%INSERT")
 {
  // unlike the other ones, this function keeps reading until it hits another command
  sourcefile >> reader;

  cout << "Insert name: " << reader << endl;


  // iterates through the link list until it hits the final node
  while(node -> link != NULL)
    node = node -> link;


  while(reader[0] != '%')
  {
   if(firstnode)
    start -> link = new data;
   else
    node -> link = new data;


   sourcefile >> node -> name;
   sourcefile >> node -> id;
   node -> link = NULL;

   sourcefile >> reader;
   cout << "reader: " << reader << endl;
  }
 }
 else
  return 0;

}

Also… offtopic. The compiler said that switch statements can’t be used with strings, is that really true, or was I doing something else wrong?

  • 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-17T17:41:55+00:00Added an answer on May 17, 2026 at 5:41 pm

    sourcefile >> node -> id; fails, and after that none of the input operations from sourcefile succeed, as failbit becomes set in the sourcefile stream. sourcefile >> node -> id; fails because it tries to read an integer but encounters “DAVID” in the stream. That happens because sourcefile >> reader; consumes “MARK”, sourcefile >> node -> name; consumes “29”, so sourcefile >> node -> id; is then left with “DAVID”. Try replacing sourcefile >> node -> name; with node -> name = reader.

    And yes, you can’t use strings in switch, only integral and enumeration expressions.

    On another offtopic note, you don’t seem to release the memory allocated for nodes (easy solution: just use std::list).

    EDIT: Here’s how your program might look like when if you used std::list:

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <list>
    
    using namespace std;  
    
    struct data
    {
     string name;
     int id;
    };
    
    ifstream sourcefile;
    
    int main()
    {
     list< data > datalist;
    
     string input;
     string reader;
     sourcefile.open("A3.txt");
    
     sourcefile >> input;
    
     cout << "Received command: " << input << endl;
    
     if(input == "%INSERT")
     {
      // unlike the other ones, this function keeps reading until it hits another command
      sourcefile >> reader;
    
      cout << "Insert name: " << reader << endl;
    
      while(reader[0] != '%')
      {
       data d;
       d.name = reader;
       sourcefile >> d.id;
       datalist.push_back( d );
    
       sourcefile >> reader;
       cout << "reader: " << reader << endl;
      }
     }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think the answer to this question is so obivous that noone has bothered
I think this is specific to IE 6.0 but... In JavaScript I add a
I think this is a multi-part question, so bear with me. Currently all of
I think this must be simple but I can't get it right... I have
I think this mostly applies to web applications, since you often see things like
I think this is pretty typical, you have the same website project with an
I think this will be easy but I can't see how to do it!
I think this should be easy, but it's evading me. I've got a many-to-many
I think this must be a stupid question, but why do the results of
I think this question is rather easy for you shell scripting monsters. I am

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.