0

can someone review my Code and tell me what's wrong I'm so lost? I'm new to C++ and the error I keep getting doesn't make any sense to me. I'm not sure what to change the program runs when I remove the brackets next to the file name but then the program keeps looping

Project1.cpp:34:9: error: invalid operands to binary expression ('std::__1::istream' (aka 'basic_istream<char>') and 'std::__1::string [16]')
    cin >> file_inName;
    ~~~ ^  ~~~~~~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:626:1: note: candidate function template not viable: no known conversion from 'std::__1::string [16]' to 'unsigned char *' for 2nd argument
operator>>(basic_istream<char, _Traits>& __is, unsigned char* __s)
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:634:1: note: candidate function template not viable: no known conversion from 'std::__1::string [16]' to 'signed char *' for 2nd argument
operator>>(basic_istream<char, _Traits>& __is, signed char* __s)
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:678:1: note: candidate function template not viable: no known conversion from 'std::__1::string [16]' to 'unsigned char &' for 2nd argument
operator>>(basic_istream<char, _Traits>& __is, unsigned char& __c)
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:686:1: note: candidate function template not viable: no known conversion from 'std::__1::string [16]' to 'signed char &' for 2nd argument
operator>>(basic_istream<char, _Traits>& __is, signed char& __c)
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:615:1: note: candidate template ignored: deduced conflicting types for parameter '_CharT' ('char' vs. 'std::__1::basic_string<char>')
operator>>(basic_istream<_CharT, _Traits>& __is, _CharT* __s)
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:643:1: note: candidate template ignored: deduced conflicting types for parameter '_CharT' ('char' vs. 'std::__1::basic_string<char> [16]')
operator>>(basic_istream<_CharT, _Traits>& __is, _CharT& __c)
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:1451:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'std::__1::basic_string<char> [16]'
operator>>(basic_istream<_CharT, _Traits>& __is,
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:1591:1: note: candidate template ignored: could not match 'bitset<_Size>' against 'std::__1::basic_string<char> [16]'
operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x)
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/iomanip:302:1: note: candidate template ignored: could not match '__iom_t7<type-parameter-0-2>' against 'std::__1::basic_string<char> [16]'
operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x)
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/iomanip:421:1: note: candidate template ignored: could not match '__iom_t9<type-parameter-0-0>' against 'std::__1::basic_string<char> [16]'
operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x)
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/iomanip:602:33: note: candidate template ignored: could not match '__quoted_proxy<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'std::__1::basic_string<char> [16]'
basic_istream<_CharT, _Traits>& operator>>(
                                ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:220:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'std::__1::basic_istream<char> &(*)(std::__1::basic_istream<char> &)' for 1st argument
    basic_istream& operator>>(basic_istream& (*__pf)(basic_istream&))
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:224:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'basic_ios<std::__1::basic_istream<char>::char_type> &(*)(basic_ios<std::__1::basic_istream<char>::char_type> &)' (aka 'basic_ios<char> &(*)(basic_ios<char> &)') for 1st argument
    basic_istream& operator>>(basic_ios<char_type, traits_type>&
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:229:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'std::__1::ios_base &(*)(std::__1::ios_base &)' for 1st argument
    basic_istream& operator>>(ios_base& (*__pf)(ios_base&))
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:232:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'basic_streambuf<std::__1::basic_istream<char>::char_type> *' (aka 'basic_streambuf<char> *') for 1st argument
    basic_istream& operator>>(basic_streambuf<char_type, traits_type>* __sb);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:233:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'bool &' for 1st argument
    basic_istream& operator>>(bool& __n);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:234:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'short &' for 1st argument
    basic_istream& operator>>(short& __n);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:235:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'unsigned short &' for 1st argument
    basic_istream& operator>>(unsigned short& __n);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:236:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'int &' for 1st argument
    basic_istream& operator>>(int& __n);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:237:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'unsigned int &' for 1st argument
    basic_istream& operator>>(unsigned int& __n);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:238:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'long &' for 1st argument
    basic_istream& operator>>(long& __n);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:239:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'unsigned long &' for 1st argument
    basic_istream& operator>>(unsigned long& __n);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:240:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'long long &' for 1st argument
    basic_istream& operator>>(long long& __n);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:241:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'unsigned long long &' for 1st argument
    basic_istream& operator>>(unsigned long long& __n);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:242:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'float &' for 1st argument
    basic_istream& operator>>(float& __f);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:243:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'double &' for 1st argument
    basic_istream& operator>>(double& __f);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:244:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'long double &' for 1st argument
    basic_istream& operator>>(long double& __f);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:245:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'void *&' for 1st argument
    basic_istream& operator>>(void*& __p);
                   ^
Project1.cpp:36:9: error: invalid operands to binary expression ('std::__1::istream' (aka 'basic_istream<char>') and 'std::__1::string [16]')
    cin >> file_outName;
    ~~~ ^  ~~~~~~~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:626:1: note: candidate function template not viable: no known conversion from 'std::__1::string [16]' to 'unsigned char *' for 2nd argument
operator>>(basic_istream<char, _Traits>& __is, unsigned char* __s)
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:634:1: note: candidate function template not viable: no known conversion from 'std::__1::string [16]' to 'signed char *' for 2nd argument
operator>>(basic_istream<char, _Traits>& __is, signed char* __s)
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:678:1: note: candidate function template not viable: no known conversion from 'std::__1::string [16]' to 'unsigned char &' for 2nd argument
operator>>(basic_istream<char, _Traits>& __is, unsigned char& __c)
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:686:1: note: candidate function template not viable: no known conversion from 'std::__1::string [16]' to 'signed char &' for 2nd argument
operator>>(basic_istream<char, _Traits>& __is, signed char& __c)
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:615:1: note: candidate template ignored: deduced conflicting types for parameter '_CharT' ('char' vs. 'std::__1::basic_string<char>')
operator>>(basic_istream<_CharT, _Traits>& __is, _CharT* __s)
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:643:1: note: candidate template ignored: deduced conflicting types for parameter '_CharT' ('char' vs. 'std::__1::basic_string<char> [16]')
operator>>(basic_istream<_CharT, _Traits>& __is, _CharT& __c)
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:1451:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'std::__1::basic_string<char> [16]'
operator>>(basic_istream<_CharT, _Traits>& __is,
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:1591:1: note: candidate template ignored: could not match 'bitset<_Size>' against 'std::__1::basic_string<char> [16]'
operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x)
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/iomanip:302:1: note: candidate template ignored: could not match '__iom_t7<type-parameter-0-2>' against 'std::__1::basic_string<char> [16]'
operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x)
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/iomanip:421:1: note: candidate template ignored: could not match '__iom_t9<type-parameter-0-0>' against 'std::__1::basic_string<char> [16]'
operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x)
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/iomanip:602:33: note: candidate template ignored: could not match '__quoted_proxy<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'std::__1::basic_string<char> [16]'
basic_istream<_CharT, _Traits>& operator>>(
                                ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:220:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'std::__1::basic_istream<char> &(*)(std::__1::basic_istream<char> &)' for 1st argument
    basic_istream& operator>>(basic_istream& (*__pf)(basic_istream&))
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:224:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'basic_ios<std::__1::basic_istream<char>::char_type> &(*)(basic_ios<std::__1::basic_istream<char>::char_type> &)' (aka 'basic_ios<char> &(*)(basic_ios<char> &)') for 1st argument
    basic_istream& operator>>(basic_ios<char_type, traits_type>&
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:229:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'std::__1::ios_base &(*)(std::__1::ios_base &)' for 1st argument
    basic_istream& operator>>(ios_base& (*__pf)(ios_base&))
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:232:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'basic_streambuf<std::__1::basic_istream<char>::char_type> *' (aka 'basic_streambuf<char> *') for 1st argument
    basic_istream& operator>>(basic_streambuf<char_type, traits_type>* __sb);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:233:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'bool &' for 1st argument
    basic_istream& operator>>(bool& __n);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:234:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'short &' for 1st argument
    basic_istream& operator>>(short& __n);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:235:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'unsigned short &' for 1st argument
    basic_istream& operator>>(unsigned short& __n);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:236:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'int &' for 1st argument
    basic_istream& operator>>(int& __n);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:237:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'unsigned int &' for 1st argument
    basic_istream& operator>>(unsigned int& __n);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:238:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'long &' for 1st argument
    basic_istream& operator>>(long& __n);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:239:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'unsigned long &' for 1st argument
    basic_istream& operator>>(unsigned long& __n);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:240:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'long long &' for 1st argument
    basic_istream& operator>>(long long& __n);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:241:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'unsigned long long &' for 1st argument
    basic_istream& operator>>(unsigned long long& __n);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:242:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'float &' for 1st argument
    basic_istream& operator>>(float& __f);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:243:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'double &' for 1st argument
    basic_istream& operator>>(double& __f);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:244:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'long double &' for 1st argument
    basic_istream& operator>>(long double& __f);
                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:245:20: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'void *&' for 1st argument
    basic_istream& operator>>(void*& __p);
                   ^
Project1.cpp:41:9: error: no matching member function for call to 'open'
    fin.open(file_inName);
    ~~~~^~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/fstream:1173:10: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'const char *' for 1st argument
    void open(const char* __s, ios_base::openmode __mode = ios_base::in);
         ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/fstream:1177:10: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'const std::__1::string' (aka 'const basic_string<char>') for 1st argument
    void open(const string& __s, ios_base::openmode __mode = ios_base::in);
         ^
Project1.cpp:48:10: error: no matching member function for call to 'open'
    fout.open(file_outName);
    ~~~~~^~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/fstream:1387:10: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'const char *' for 1st argument
    void open(const char* __s, ios_base::openmode __mode = ios_base::out);
         ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/fstream:1391:10: note: candidate function not viable: no known conversion from 'std::__1::string [16]' to 'const std::__1::string' (aka 'const basic_string<char>') for 1st argument
    void open(const string& __s, ios_base::openmode __mode = ios_base::out);
         ^
4 errors generated.  
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<iomanip>

using namespace std;

void table(double c[], double p[])
{
    const string month[12]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    ifstream fin;
    ofstream fout;

    fout << setw(6) << "Month" << setw(14) << "CurrentRF"
         << setw(13) << "PreviousRF" << setw(11) << "Average" << endl;

    for(int i = 0; 0 < 12; i++)
    {
        fout << month[i] << setw(9) << c[i] << setw(13) << p[i] << setw(13) << (c[i] + p[i]) / 2 << endl;
    }


}
int main( )
{
  int answer;
  double currentAvg[11], previousAvg[11];
  char response;
  string file_inName[16], file_outName[16];
  do
  {
    cout << "Please enter the name of the file you'd like to open.\n";
    cin >> file_inName;
    cout << "\nPlease enter the name of the file where you'd like the data to go.\n";
    cin >> file_outName;

    ifstream fin;
    ofstream fout;

    fin.open(file_inName);
    if (fin.fail( ))
    {
        cout << "Input file opening failed.\n";
        exit(1);
    }

    fout.open(file_outName);
    if (fout.fail( ))
    {
        cout << "Output file opening failed.\n";
        exit(1);
    }
    for(int i = 0; 0 < 12; i++)
    {
      fin >> currentAvg[i];
      fin >> previousAvg[i];
    }

    cout << "Which format would you like the program to give?" << endl;
    cout << "(Press 1 for a table, 2 for bar graph)" << endl;
    cin >> answer;
    cout << endl;

    if ( answer == 1 )
    {
      table(currentAvg, previousAvg);
    }
    if ( answer == 2 )
    {
      //bar_graph( x, y );
    }
    cout << "Would you like to use this program again? (Y for yes, anything else for no) ";
    cin >> response;
  } while (response == 'y' || response == 'Y');

  return 0;
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • 1
    You have an array of 16 strings, is this what you wanted? – Yksisarvinen Jun 18 '21 at 09:05
  • You seem to be using an array of strings (`file_inName`, `file_outName`) as a single string. Did you mean to declare them as `string file_inName, file_outName;`? – Algirdas Preidžius Jun 18 '21 at 09:06
  • Lilely you just wanted to have a single string: `std::string fileName;`. If you indeed wanted an array, you'd have to use a specific index: `std::cin >> fileName[desiredIndex];`. – Aconcagua Jun 18 '21 at 09:07
  • Side note: [about `using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Aconcagua Jun 18 '21 at 09:08
  • 1
    Also, `for(int i = 0; 0 < 12; i++)` is infinite loop (in both cases). – Yksisarvinen Jun 18 '21 at 09:08
  • When I do that my program loops endlessly so I tried to give a limited amount of characters to use to see if there was an issue with the way I inputted my file name – David Lopez Jun 18 '21 at 09:08
  • Note @Yksisarvinen 's comment: You need to compare `i` against being smaller 12, not `0`... – Aconcagua Jun 18 '21 at 09:10
  • @DavidLopez "_so I tried to give a limited amount of characters to use to see if there was an issue with the way I inputted my file name_" That makes no sense, since your code does no such limiting. – Algirdas Preidžius Jun 18 '21 at 09:10
  • `std::string` does not provide means to limit input size. If you want to, have a look at [std::setw](https://en.cppreference.com/w/cpp/io/manip/setw). Especially, adding brackets to the variable declaration creates an array of multiple strings, so that's rather the opposite of what you actually intended, you now have 16 strings of *still* unlimited size in a row instead of a single one. – Aconcagua Jun 18 '21 at 09:12
  • @AlgirdasPreidžius Wow I feel so stupid! Thanks! – David Lopez Jun 18 '21 at 09:14
  • Do you guys know why my function table won't fout things in the desired file? – David Lopez Jun 18 '21 at 09:17
  • You never open any file in `table`. `fout` in that function has nothing in common with `fout` in `main()`. Beware that you cannot open the same file twice, so if you have file open in main, you cannot create second stream to the same file in other function. – Yksisarvinen Jun 18 '21 at 09:25
  • @Yksisarvinen so I should just open the files in the function? – David Lopez Jun 18 '21 at 09:34

0 Answers0