Hello, I need help with me homework. It's probably something simple. My program is supposed:
1) read lines from a file (rain.txt)
2) find the shortest and longest lines (I used strlen())
3) return the line number of shortest and longest and well as their length
4) show the avg number or characters per line

So far I'm just trying to read the lines from the file. When I go to compile this is the error I receive:

error C2664: 'strlen' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.

do i need a type conversion in here? what am I doing wrong? oh yeah, here's my code: :o

#include <iostream>		// cin, cout
#include <fstream>		// ifstream, ofstream
#include <string>		// string, getline()
#include <cassert>		// assert()
#include <cfloat>		// DBL_MIN and DBL_MAX
using namespace std;

int main()
{
   cout << "This program computes the number, maximum, minimum, and\n"
           "average of an input list of stings and characters in one file\n";


                                              
   ifstream inStream;                        
   inStream.open("rain.txt"); 
   if(!inStream.is_open())
		{ cout << "Error opening file"; exit (1); }

   
   int members, count = 1;                         
                              
   double maximum = DBL_MIN,                 
          minimum = DBL_MAX;                 
           
   string lyrics;

     while (!inStream.eof() )                                  
   {
	  getline(inStream, lyrics);  
	  cout<<count<< ") " <<lyrics <<"\n";
	  string bob = lyrics;
	  members = strlen(bob);
	  count++;
      if ( inStream.eof() ) break;           
                           
      
     if (members < minimum)    
         minimum = members;                  
     if (members > maximum)
         maximum = members;
   }                                         

   inStream.close();                         

   cout << count;



   return 0;
}

Hello,

First, a comment on style. I see that you do some declarations of variables inside of functions between instruction segments. That made the code hard to read. You may wish to consider placing all of your definitions at the top of the function, so that eyes do not have to hunt down where things are declared,

Second, I don't think that you can do a

string bob = lyrics;

I think you need to do a strcpy(); on the two arrays (strings). Better yet, just do a

members = strlen(lyrics);

Why bother with Bob?

A purist might complain that you wasted too much memory and processor cycles :)

Christian

members = strlen(bob.c_str());

Perhaps?

You're right... I'm sorry about that big mess.

Hello,

First, a comment on style. I see that you do some declarations of variables inside of functions between instruction segments. That made the code hard to read. You may wish to consider placing all of your definitions at the top of the function, so that eyes do not have to hunt down where things are declared,

Second, I don't think that you can do a

string bob = lyrics;

I think you need to do a strcpy(); on the two arrays (strings). Better yet, just do a

members = strlen(lyrics);

Why bother with Bob?

A purist might complain that you wasted too much memory and processor cycles :)

Christian

Thanks Guys!! It worked!!! :mrgreen:

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.