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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:14:21+00:00 2026-05-28T08:14:21+00:00

This is so stupid. I’ve been stuck literally for an hour trying to read

  • 0

This is so stupid. I’ve been stuck literally for an hour trying to read in a .txt file of numbers that are separated by a single whitespace. The while loops only gets executed once for some reason!

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

using namespace std;

int main(int argc, char* argv[])
{
    string line;
    string str(argv[1]);
    ifstream myfile((str).c_str());
    int num;
    stack<int> x;

    while (myfile >> num);
    {
        x.push(num);
    }

    return(0);
}
  • 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-28T08:14:21+00:00Added an answer on May 28, 2026 at 8:14 am

    Hmm, look at this line more closely:

    while (myfile >> num);
    

    Eventually, you’ll notice the semi-colon. The compiler thinks this means you want a loop that does nothing (the semi-colon here indicates a single, empty statement). So, the loop reads in all the numbers, but does nothing with them.

    The next section is interpreted separately as a statement in its own scope (denoted by the braces), to be executed after the loop:

    {
        x.push(num);
    }
    

    All that does is push the last number read onto the stack, leading you to think the loop only executes once.

    Remove the ; and you’re OK! Once bitten by this, you’ll never forget 😉

    On an unrelated note, it’s a bit silly to take argv[1] (a C-style string), put it into a string object, then use c_str() to turn that back into a C-string for the ifstream constructor. Just use argv[1] directly, since you’re not doing anything else with it. Also, it would be a good idea to check argc first and make sure that a filename was passed in. Finally, you should check that the file was successfully opened instead of assuming it — at the very least make your assumption explicit with an assert(myfile.is_open());. Oh, and you don’t use the line variable at all.

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

Sidebar

Related Questions

i have been stressing for an hour at this stupid script i am trying
This sounds really stupid, but I was told that you could drag-and-drop visual components
I feel stupid asking this question, but I've been wrestling with it for hours
This is currently a homework project that me and my teammate are stuck on.
I have been having alot of trouble with this stupid struct. I don't see
Sorry if this is stupid... but it confuses me?... I'm trying a new storyboard
Hi guys i'm almost done writing my code and i'm stuck with this stupid
Please forgive this stupid question. (I'm originally an ASP.NET programmer.) I'm trying to add
ok i know this is stupid but atleast im trying :) $result = shell_exec('C:/cygwin/bin/bash.exe
Maybe this is stupid, but are there any drawing elements in Xcode 4.2 ?

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.