//final.cpp
//Creates an array with 2 players' batting avg
//Read in hits and atbats for each player
//1)	one player get data in main           
//2)	other get data in battingavg function
//battingavg function figures out battingavg for both players
// and puts them in array (not hits & abats), print out array in function
#include <iostream>           //  Rose 309 611           Jack 250 689

//COMPILE ERROR???? G:\C++\HW\final.cpp In function `int main()': 

#include <fstream.h>
#include <stdlib.h>
using namespace std;

void BattingAverage(int hits, int atbats, int count);     //&????
float batavg[2];
int hits, atbats, count;
char name;

int main() 
{
   ifstream inbat;
   ofstream outbat;

   inbat.open("inbat.txt");
   outbat.open("outbat.txt");

   if ( inbat.fail() )
   {
      cout<<"Output file doesn't exist!";
   }
   for ( int count=0; count<2; count++ )
   {
      inbat.getline(name,5);        //ERROR: 35 G:\C++\HW\final.cpp invalid conversion from `char' to `char*' 
      //32 G:\C++\HW\final.cpp   initializing argument 1 of
      // `std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]' 
      inbat>>hits>>atbats;

      BattingAverage(hits, atbats, count); //calling function to do calc	

   }                   
   system("PAUSE");
   inbat.close();
   return 0;
}
void BattingAverage(int hits, int atbats, int count) 
{
   batavg[count] = (float)hits/(float)atbats;      

   cout<<name<<"'s batting average is:"<<batavg[count]<<endl;      //ios
}

<< moderator edit: added [code][/code] tags and fixed indentation >>


Thank you so much for your help

Recommended Answers

All 8 Replies

Are you only using a single letter for the name of the player?

If not, then you should change

char name;
to
char name;


(where size is the number of letters required to store the name + 1 for the null terminator)

>char name;
Good suggestion, bad code. You seem to be mixing and matching features of C++ and Java to create something completely incorrect for both. Try this instead:

char name[size];

Where size is a suitably defined constant value, in this case 5.

You're right - I should stick to doing one language well instead of doing more than one language not so well...

Still doesn't work -thanks for help. By changing to char name[5] - Not reading in 2nd name and not doing calculation correctly for either. Thanks again

*sigh* If I had a nickel for every time someone left a newline in the stream with cin>> or scanf, I would be rich by now.

Sorry Narue but as I'm sure you can tell, I am a beginner and have no idea what you are saying. Can you tell me in very basic C++ words. Thanks, Kittie

>Can you tell me in very basic C++ words.
Enter get stuck in cin, break rest of program.

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.