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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:18:49+00:00 2026-06-18T14:18:49+00:00

I opened a .txt file with an ifstream object named input . If a

  • 0

I opened a .txt file with an ifstream object named input. If a new line starts with a “(” then it doesn’t read it how I want it to. The expected output doesn’t get printed, then it exits the loops. I want it to only jump out of the while loop when it reaches the end of the file. What am I doing wrong? My do while loop and my .txt file are below.

char c;
int i;
do
{
    if(input.peek( ) == '(' || input.peek( ) == ')')
    {
        input >> c;
        cout << c;
    }else if(input.peek( ) == '+' || input.peek( ) == '-' || input.peek( ) == '*' || input.peek( ) == '/')
    {
        input >> c;
        cout << c;
    }else
    {
        input >> i;
        cout << i;
    }

}while(input && input.peek( ) != EOF);

Here is the .txt file, each on a separate line:

(3)
(3)
4
(5+7)-(5*3)

This is my output:

 (3)3
  • 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-18T14:18:50+00:00Added an answer on June 18, 2026 at 2:18 pm

    So, I’m pretty certain that the problem is that input.peek() is returning a newline after ')' has been read. Then input >> i; doesn’t read a number, and i remains the value it had before, so the output is 3. You could quickly try this by adding i = 42; before input >> i; – if the output becomes (3)42, then I’m right.

    If I’m right, you will want to add a bit of code to handle isspace() or something similar.

    May I also suggest that you do something like cpeek = input.peek();, before the first if, and then use if (cpeek == '(' || cpeek == ')')... etc.

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

Sidebar

Related Questions

I'd like to add new line with text to my date.txt file, but instead
I have a sample txt file and want to read the contents of the
I have opened one file with following way: fp = fopen(some.txt,r); Now in this
I had .txt file and I opened it and saved it as .csv file.
Suppose you want to read the data from large text file (~300mb) to array
I am trying to write a program that read a file using fstream then,
I have opened a .txt file many times using the C language's file handling
I'm creating a file connection via path <- file(C:/test.txt) and when printing the object
I've created a small program wich can read a .txt file. This file contains
I have a txt file that has a change-log.I'm trying to display the new

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.