-2

So, I copied a program from my textbook to give it a test and am having output problems. The program is simple:

(When i run the program VS says the program is out of date; I don't know if the problem may be there.)

//This program reads data from a file into an array
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    const int Array_size = 10; //array size
    int numbers[Array_size];  //array with 10 elements
    int count = 0;   //loop counter variable
    ifstream inputFile;   //input file stream object

    //opent the file
    inputFile.open("C:/TenNumbers.txt");

    if (!inputFile)
    {
        cout << "Unable to open file" << endl;
        return 0;
    }

    //read the numbers from the file into the array
    while (count < Array_size && inputFile >> numbers[count])
        count++;

    //close the file
    inputFile.close();

    //display the numbers read
    cout << "The numbers are: ";
    for (count = 0; count < Array_size; count++)
        cout << numbers[count] << " ";
    cout << endl;
    system("pause");
    return 0;

}

The output is:

enter image description here

and the console debug is:

'project7.3.exe' (Win32): Loaded 'C:\Visual Studio 2015\Projects\project7.3\Debug\project7.3.exe'. Symbols loaded. 'project7.3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Symbols loaded. 'project7.3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Symbols loaded. 'project7.3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Symbols loaded. 'project7.3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp140d.dll'. Symbols loaded. 'project7.3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140d.dll'. Symbols loaded. 'project7.3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Symbols loaded. 'project7.3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. Symbols loaded. 'project7.3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Symbols loaded. 'project7.3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Symbols loaded. 'project7.3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Symbols loaded. 'project7.3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Symbols loaded. 'project7.3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Symbols loaded. 'project7.3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Symbols loaded. 'project7.3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel.appcore.dll'. Symbols loaded. The thread 0x6e44 has exited with code 0 (0x0). The thread 0x68ac has exited with code 0 (0x0). The thread 0x5978 has exited with code 0 (0x0). The thread 0x56ec has exited with code 0 (0x0). The program '[7220] project7.3.exe' has exited with code 0 (0x0).

Here is the contents of the text file in notepad++. I changed the encoding from UTF8 to ANSI based on info found online:

//This file supports the program7.3 in visual studio 2015 101 102 103 104 105 106 107 108 109 110

5
  • 1
    You're printing "file open" if the file couldn't be opened. You probably need to look up the concept of working directory. Commented May 17, 2016 at 16:40
  • Thank you, that was just one of many things I tried while reading through solutions. I forgot to take it out. Commented May 17, 2016 at 16:45
  • Your leaving it in at least shows that the file couldn't be opened, even though the message is wrong. Commented May 17, 2016 at 16:47
  • Why did you get rid of the file check? You should always check that the file was opened before continuing. Commented May 17, 2016 at 16:52
  • I don't know what numbers you expect to be output. As far as I can tell from your screen-shot of the output, there are 10 numbers. Is the output problem that you did not expect those numbers? I feel like it might be that you're not reading the integers as integers, but rather strings. Does the solution presented in this similar question fix output problems? Commented May 17, 2016 at 16:52

1 Answer 1

1

The message in the following block is misleading.

if (!inputFile)
{
    cout << "file open" << endl;
}

It should be

    cout << "Unable to open file" << endl;

After that, you proceed to read the data anyway when you should bail out.

if (!inputFile)
{
    cout << "Unable to open file" << endl;
    return EXIT_FAILUIRE;
}

You are getting bogus data since nothing is being read from the file.

And then, you proceed to display all the data. You need to display only the data that were successfully read. Use:

//display the numbers read
cout << "The numbers are: ";
for (int i = 0; i < count; i++)
    cout << numbers[i] << " ";
cout << endl;
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you. I put the file directly in the C drive and applied the changes you suggested but it is still outputting the wonky numbers.
@ShaunCampbell, that does not make sense. Please update your post with the additional information.
I think what @RSahu meant by "update with... additional information" is that you should include updated code & the actual text file that you are trying to write into an array, and then display.
@ShaunCampbell, that edit is not good. You have updated the post in such a way that my answer does not make sense at all :) You need to leave the original post and add the updated code at the end.
Oh sorry, first timer.
|

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.