0

so i wrote the code above and i have this error below. I see from here that getline only works with char, i tried to write it in other ways, use cin.. but it stll doesn't work, any sugestions?

i really need help with this..

#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    int n;
    double sum[100];
    double source[100];
    int broj;

    ifstream ulaz("dat.txt", ios::in);
    if(ulaz.is_open())
    {
        while(!ulaz.eof())
        {
            cin.sync();
            cin.clear();
            ulaz.getline(source,sizeof(source));
            broj++;
        }
    }
    ulaz.close();

    cout<<"Insert number of days:"<<endl;
    cin>>n;

    for(int i=0; i<broj; i++)
    {
        sum[i]=1000*source[i];
        sum[i]=sum[i]/source[i+1];
    }

    cout<<"Result: "<<sum<<endl;

    system("pause");
    return 0;
}

1>------ Build started: Project: Zadaci_za_1l, Configuration: Debug Win32 ------
1>  zadatak2.cpp
1>c:\users\****.cpp(23): error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize)' : cannot convert parameter 1 from 'double [100]' to 'char *'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
DanielKO
  • 4,194
  • 15
  • 27
user3088738
  • 41
  • 1
  • 2
  • 9
  • 2
    try the stringstream class, and read into the stream. the you need to convert the stream to double. you can't use getline on double. – Claudiordgz Mar 19 '14 at 23:35
  • Note: I've reformatted the code to be more readable. This means that the line number referenced in the error message is not accurate. – Bill Lynch Mar 19 '14 at 23:35
  • 1
    `ulaz.getline` gets a line from `ulaz` and puts it in the char array pointed to by the first parameter, it doesn't make any sense to put a type `double[]` there. – Red Alert Mar 19 '14 at 23:36
  • Also, have you looked at questions like this one? http://stackoverflow.com/questions/2615078/how-to-read-in-a-double-from-a-file-in-c – Bill Lynch Mar 19 '14 at 23:37
  • 1
    you'll probably also find out, sooner or later, that [`while(!ulaz.eof())` is wrong.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – WhozCraig Mar 19 '14 at 23:42
  • You can only `getline` into a string or character array. Do what Claudiordgz said and read into a stringstream, and then read doubles from that stream. – AndyG Mar 19 '14 at 23:46
  • can you just show me an example how to do that? – user3088738 Mar 19 '14 at 23:54
  • Your filestream already is a stream, so you can read double values directly using `operator>>`. – Pixelchemist Mar 20 '14 at 01:12

2 Answers2

1

As others pointed out: getline is not suitable for doubles. If you file contains several doubles, separated by whitespaces or newlines you can make use of std::istream::operator>>(double&) implicitly by using std::istream_iterator<double>. Furthermore, if you want to handle a flexible number of values, do not use C-style arrays but vectors.

A code reading doubles from a file data.txt (only containing doubles) into a vector, summing them up may then look like

#include <fstream>
#include <vector>
#include <string>
#include <iostream>
#include <iterator>

std::vector<double> dv_from_file(std::string const & filename)
{
  std::ifstream input(filename.c_str(), std::ios_base::in);
  std::vector<double> data;
  if (input.good())
  {
    std::copy(std::istream_iterator<double>(input),
      std::istream_iterator<double>(),
      std::back_inserter(data));
  }
  return data;
}

int main(void)
{
  std::vector<double> source(r_dv_from_file("data.txt"));
  std::vector<double>::size_type const N(source.size());
  double sum(0.0);
  for (auto value : source) sum += value;
  std::cout << "File contains " << N << " elements." << std::endl;
  std::cout << "Sum of all elements is: " << sum << std::endl;
}
Daniel
  • 17,154
  • 10
  • 55
  • 72
Pixelchemist
  • 21,390
  • 6
  • 40
  • 70
0

Take a look at the fstream getline reference here: std::istream::getline. And as others have already commented, the getline method is expecting a pointer to a character, and how many characters to read into that memory. After obtaining the line, it will be necessary to then convert the character data to the double and store appropriately.

Alternatively, depending on how the input file content is constructed, you may be able to use the input streaming operator to automatically convert an input line into a given double array element. More details are here: std::istream::operator>>. This can work well regarding a compile time sized array if the input file line quantity is constant or less than the allocated array size. If not, then it will be necessary to use something better like a vector. First read a single line into a local double variable, then call push_back on the vector to store the value for later use. Then proceed to the next line from the input file.

To help reduce confusion, it is not recommended to mix C library string processing with C++ streams. There is more to keep track of and not introduce buffer overruns when attempting to use both libraries in the same design. In other words, play it safe by concentrating on a single library. If C++ is the goal, then iostream, sstrstream, ifstream, ostream, etc. are the ones you want.