1

this is giving me a wicked headache and was hoping I could find some help. The program is supposed to read in a program of 19 integers, then output the smallest (2nd integer) and largest (5th integer) to the screen. However, all my results yield garbage.

#include iostream>
#include <fstream>
#include <cstdlib>

using std::ifstream;
using std::ofstream;
using std::cout;
using std::endl;

//the goal of this program is to read in numbers from a file, then output the 
//highest number and the lowest number to the screen
int main() {

ifstream fileInput;
int nOne, nTwo, nThree, nFour, nFive, nSix, nSeven, nEight, nNine, nTen,     //there are 19 numbers in the file
    nEleven, nTwelve, nThirteen, nFourteen, nFifteen, nSixteen, nSeventeen,
    nEighteen, nNineteen;


cout << "Opening File" << endl;

fileInput.open("Lab12A.txt");            //the file is opened
if (fileInput.fail())
{
    cout << "Input file opening failed. \n"; //the fail check doesnt pop up, so the file has been opened.
    exit(1);
}

fileInput >> nOne >> nTwo >> nThree >> nFour >> nFive >> nSix >> nSeven >> nEight
    >> nNine >> nTen >> nEleven >> nTwelve >> nThirteen >> nFourteen >> nFifteen   //this is where they should be extracted
    >> nSixteen >> nSeventeen >> nEighteen >> nNineteen;




cout << "The highest number is " << nTwo << endl;
cout << "The lowest number is " << nFive << endl;

fileInput.close();

system("pause");
return 0;
}
14
  • 4
    That's not how you write code to solve the problem... Have you learned about arrays yet? Commented Apr 2, 2018 at 23:17
  • Typo on the first line. Commented Apr 2, 2018 at 23:19
  • What output are you getting? Commented Apr 2, 2018 at 23:20
  • What is the format of the input file and the exact output? I cannot reproduce this with space separated input numbers. Commented Apr 2, 2018 at 23:23
  • 2
    Just as a wild guess, is it possible that the text file editor you used to create Lab12A.txt inserted a Unicode BOM mark into the file? Then, maybe that caused the standard library to put the input stream into error mode on trying to read the first number. Commented Apr 2, 2018 at 23:44

3 Answers 3

1

I wished to add only a comment but since I can't do that, I leave it as an answer.

I have copied your file and created a text file to try to reproduce your issue. At first everything went well (No issue at all). But with comment from Daniel Schepler, I changed file encoding to UTF8-BOM (You can do that easily from Notepad++ Encoding menu) and tried again. I got same values you posted. I can't give more explanation to exactly how values are to be interpreted but I wish someone with more experience enlighten us here.

Sign up to request clarification or add additional context in comments.

1 Comment

It's just causing undefined behavior (uninitialized variables) because it's not able to successfully read any values. So the exact values you're getting in this case are meaningless, and would also be likely to vary based on optimization settings or platform.
1

First I wanted to thank everyone who looked at and commented on this I greatly appreciate it, the issue was ultimately pinned down to needing a full path to the .txt file as opposed to the relative path I initially posted. For what ever reason, my compiler couldn't recognize the file without it. Seems like a silly mistake but I'm relatively new to this so those are sure to squeak by. Thanks again everyone!

Comments

0

You can use class std::vector pushing the values then sorting the container and finally print the second and fifth elements:

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>


int main(){

    std::ifstream in("test.txt");

    std::vector<int> vecInt;
    int value;

    while(in >> value)
        vecInt.push_back(value);
    in.close();

    std::sort(vecInt.begin(), vecInt.end());

    // second value is at index 1 and fifth value is at index 4
    for(auto i(0); i != vecInt.size(); ++i)
        if(i == 1 || i == 4)
            std::cout << vecInt[i] << std::endl;


    std::cout << std::endl << std::endl;
    return 0;
}
  • I am not sure about what you mean with "largest fifth integer".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.