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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:20:12+00:00 2026-06-02T23:20:12+00:00

I’m trying to get my program to loop through the file, taking in a

  • 0

I’m trying to get my program to loop through the file, taking in a load of information each time. However, at the moment after 2 correctly inputted lines it always goes to default, no matter what the contents of the file. Originally it was an eof while loop but I changed it to a for loop to try and fix it. Here’s my code:

ifstream furniture;
furniture.open("h://furniture.txt");


for(int i=0;i<=count;i++)
{
    type=0;
    furniture>>type>>name>>number>>material>>colour>>mattress;

    switch (type)
    {
    case 1:
        {
            Item* item= new Bed(number, name, material, colour, mattress);
            cout<<"working, new bed"<<endl;
            v.push_back(item);
            cout<<"working pushback"<<endl;
            count++;

            break;
        }
    case 2:
        {
            Item* item= new Sofa(number, name, material, colour);
            cout<<"working, new sofa"<<endl;
            v.push_back (item);
            cout<<"working pushback"<<endl;
            count++;

            break;
        }
    case 3:
        {
            Item* item= new Table(number, name, material, colour);
            cout<<"working, new table"<<endl;
            v.push_back(item);
            cout<<"working pushback"<<endl;
            count++;

            break;
        }
    default:
        {
            cout<<"Invalid input"<<endl;
            type=0;
            break;
        }
    }
}

I have tried a series of different solutions but nothing seems to have solved the problem.
Any help would be greatly appreciated.

  • 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-02T23:20:13+00:00Added an answer on June 2, 2026 at 11:20 pm

    ifstream‘s operator>> does not change the variable if the read fails, so type remains 0, thus the default branch is called. It seems like your file has a formatting error that makes the read fail.

    Based on your comment, that you might not have all values in the file all the time, here is what you could do:

    The best way would be to use getline() to read a full line, then use stringstream to extract the values from the line, handling the optional values in the process:

    string line;
    getline(furniture, line);
    stringstream ss(line);
    ss>>type>>name>>number>>material>>colour>>mattress;
    

    If you provide default values to all those variables, they will keep the default if the line is ill-formatted or does not contain all of them.

    The advantage of using getline() here is that the extraction via op>> does not mess up subsequent lines if there is a problem with the current line.

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

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
Basically, what I'm trying to create is a page of div tags, each has
I am trying to render a haml file in a javascript response like so:
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters

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.