Efficient C++ Homework Help: Troubleshooting Infile Command Issues

  • Context: Comp Sci 
  • Thread starter Thread starter giritrobbins
  • Start date Start date
  • Tags Tags
    C++ Homework
Click For Summary
SUMMARY

The discussion focuses on troubleshooting issues with the infile command in C++ when reading data from a text file. The user is experiencing problems with resetting the infile stream after closing it, specifically using commands like infile.close() and infile.clear(). The user has attempted various solutions, including changing the variable used in the while loop and reopening the file, but these have not resolved the issue. The libraries currently in use are cstdlib, iostream, and fstream, indicating a need for proper stream management in C++.

PREREQUISITES
  • Understanding of C++ file I/O operations
  • Familiarity with the fstream library for file handling
  • Knowledge of stream state management using clear() and close()
  • Basic programming concepts such as loops and conditionals
NEXT STEPS
  • Research C++ file stream state management techniques
  • Learn about the proper usage of infile.clear() and infile.seekg()
  • Explore error handling in C++ file I/O operations
  • Investigate best practices for reading data from text files in C++
USEFUL FOR

C++ students, software developers, and anyone troubleshooting file input issues in C++ programming.

giritrobbins
Messages
8
Reaction score
0

Homework Statement


Well I have to read in data from a text file test some conditions on certain columns and count up the failures. I am having a problem with the infile command, I have tried using infile.close(); but it does not seem to reset it.

while(infile>>y)
{
for(i;i<=(j/4);i++)
{
total=total+1;
if((2+4*i==total) && ((150>y) || (y>170)))
temp=temp+1;

if((3+4*i==total) && ((60>y)||(y>70)))
pressure=pressure+1;

if((4+4*1==total)&&((2>y)||(y>2.5)))
dwell=dwell+1;
}}

Homework Equations


n/a

The Attempt at a Solution



I have attemped infile.close(); but it did not seem to reset the counter saying it was the end of the file.

I also tried change the value of while(infile>>y) it was >>x before. Also I have tried reopening the file before this loop but it also does not seem to work.

Just to preface this there is a second loop before this that counts the total number of elements a number that I need and cannot nest within this loop.
 
Physics news on Phys.org
infile.clear();
 
infile.clear(); does not seem to work, is there a library that I need to include to use it. Right now I am using cstdlib, iostream, and fstream.
 

Similar threads

  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 23 ·
Replies
23
Views
9K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K