Ok, so this is going to be a basic question that I should probably know, but it's baffling me right now... Also, be prewarned that this is a homework assignment I'm working on. I don't want answers, just guidance. I understand what I need top do(for the most part. obviously there's something I don't understand else I wouldn't be here.. ANYWAYS:

I have said code below:

#include <iostream>
#include <iomanip.h>
#include <fstream.h>
#include <cstdlib>

using std::cout;
using std::ios;
using std::endl;

int main()
{
  int day;
  char temp [5];
  char marker [5];
  int counter1 = 0;
  int counter2 = 1;
  ifstream output;

  output.open("TEMPS.DAT", ios::in);
  if(output)
  {
    while(!output.eof())
    {
        cout << "Day " << marker[counter2] << ":\n";
        output << temp[counter1] << endl;
        //output.ignore(80,'\n');
        //temp = atoi(day[counter1]);
        cout << temp[counter1] << '\n';
        counter1++;
        counter2++;
    }
  }
  else
  {
      cout << "ERROR. TERMINATING PROCESS.\n";
  }
  output.close();
    return 0;
}

inside TEMPS.DAT is the following:

65
42
31
24

all I want to do right now is to put each of the temperatures inside this file into a variable and print it out to the screen. I think that I have a problem with the way I've done my character arrays, and I'm looking into that, but right now, the only two errors I'm getting are:

h:\__Online Course Work\Chapter 11\Book Projects\HIGHTEMP2.CPP||In function 'int main()':|
h:\__Online Course Work\Chapter 11\Book Projects\HIGHTEMP2.CPP|33|error: no match for 'operator<<' in 'output << temp[counter1]'|
||=== Build finished: 1 errors, 0 warnings ===|

I've done some stuff with file output before... stuff more complicated than this, so the fact that I can't figure this out is kind of degrading for me >_<

If someone can point me in the right direction, that would be very helpful. :)

Recommended Answers

All 9 Replies

output.open("TEMPS.DAT", ios::in);
output << temp[counter1] << endl;

Do not make sense as you are trying to write to an in-stream.

This should be what you're looking for:

output >> temp[counter1];

Aside from the issue of reading data from file, on line 24 you appear to be using the array of char called marker, but you've never put any values into that array.

This should be what you're looking for:

output >> temp[counter1];

I'm trying to output data, not input it, and when I compile it, it gives me a bunch of errors. Here's the portion of code you were talking about beforehand:

while(!output.eof())
    {
        cout << "Day " << marker[counter2] << ":\n";
        output << temp[counter1] << endl;
        //output.ignore(80,'\n');
        //temp = atoi(day[counter1]);
        cout << temp[counter1] << '\n';
        counter1++;
        counter2++;
    }

and here's what I changed it to:

while(!output.eof())
    {
        cout << "Day " << marker[counter2] << ":\n";
        [b]output >> temp[counter1] >> endl;[/b]
        //output.ignore(80,'\n');
        //temp = atoi(day[counter1]);
        cout << temp[counter1] << '\n';
        counter1++;
        counter2++;
    }

when I compile, these are the error messages I get:

h:\__Online Course Work\Chapter 11\Book Projects\HIGHTEMP2.CPP||In function 'int main()':|
h:\__Online Course Work\Chapter 11\Book Projects\HIGHTEMP2.CPP|29|error: no match for 'operator>>' in 'std::operator>> [with _CharT = char, _Traits = std::char_traits<char>](((std::basic_istream<char, std::char_traits<char> >&)(& output.std::basic_ifstream<char, std::char_traits<char> >::<anonymous>)), ((char&)(& temp[counter1]))) >> std::endl'|
f:\_other\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|119|note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>& (*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]|
f:\_other\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|123|note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]|
f:\_other\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|130|note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]|
f:\_other\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|166|note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>]|
f:\_other\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|170|note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>]|
f:\_other\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|173|note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]|
f:\_other\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|177|note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char, _Traits = std::char_traits<char>]|
f:\_other\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|180|note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]|
f:\_other\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|184|note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char, _Traits = std::char_traits<char>]|
f:\_other\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|188|note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]|
f:\_other\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|193|note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char, _Traits = std::char_traits<char>]|
f:\_other\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|197|note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]|
f:\_other\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|202|note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char, _Traits = std::char_traits<char>]|
f:\_other\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|206|note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char, _Traits = std::char_traits<char>]|
f:\_other\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|210|note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char, _Traits = std::char_traits<char>]|
f:\_other\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|214|note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char, _Traits = std::char_traits<char>]|
f:\_other\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|238|note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]|
f:\_other\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\iomanip|179|note:                 std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setfill<_CharT>) [with _CharT = char, _Traits = std::char_traits<char>]|
f:\_other\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\istream.tcc|853|note:                 std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&) [with _CharT = char, _Traits = std::char_traits<char>]|
f:\_other\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|741|note:                 std::basic_istream<_CharT2, _Traits2>& std::operator>>(std::basic_istream<_CharT2, _Traits2>&, _CharT2*) [with _CharT2 = char, _Traits2 = std::char_traits<char>, _CharT = char, _Traits = std::char_traits<char>]|
||=== Build finished: 1 errors, 0 warnings ===|

I seriously must be missinsg something pretty obvious...

Line 17 you declare an ifstream....like Insensus said, that is an input file stream, he assumed you were actually trying to read data because of this.

Use ofstream for writing to files. You will also need to change ios::in to ios::out or ios::binary depending on what you want to do.

http://www.cplusplus.com/doc/tutorial/files/

Did you change

ifstream output;

to

ofstream output; ?

Your original code will compile if you do that. If you're trying to write out, you need an output stream. Not an input stream.

all I want to do right now is to put each of the temperatures inside this file into a variable

I'm trying to output data, not input it,

These two statements are contradictory. Are you reading data in, or writing data out?

Yeah... I wasa rushing through this and didn't see that... >_<

I have now declared it as an ofstream, and the portion of my code above has been changed to reflect reading data in... although now I have a new problem. When I try to execute my program, it runs fine... kinda, except that it eternally reads from the file... and it actually writes data to the file, as well...


in the original post, I had said that "TEMPS.DAT" looks like this:

65
42
31
24

but when I execute, my computer starts beeping, and keeps outputting machine code to me.... At the Same time, about the time that I Ctrl+C my program, "TEMPS.DAT" is now changed to:

w
B
N
Ã
w
ð
4
A

I'm pretty sure it's this line here that's causing my problem:

26. while(!output.eof())

Is there a better way to use this function and/or is there another funtion that I can use to make it read from the file until the end, and then stop?

Ok, soo... I'm dumb.

I figured out one of my mistakes. I do actually want to use infile, and I was going about this all wrong.

Thanks guys, I'll post here with any other problems :)

Writing to files can be seen as Outputting to files,
Reading from files can be seen as Inputting from files(i.e. inputting the data into variables)

Ok, I'll make sure to get my word usage right from now on. I can see how me saying what I said can be confusing. :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.