user1066524
user1066524

Reputation: 373

passing vector<char> to a pointer char*

how do I pass a char vector to a char*? I know this problem could easily be solved with a predefined char[] array with a SIZE const, but I want the flexibility of a vector because there will be no predefined size.

using namespace std;

//prototype 
void getnumberofwords(char*);

int main() {
    //declare the input vector
    vector<char> input;

    /*here I collect the input from user into the vector, but I am omitting the code here for sake of brevity...*/

    getnumberofwords(input); 
    //here is where an ERROR shows up: there is no suitable conversion from std::vector to char*                     
    return 0;
}

void getnumberofwords(char *str){
    int numwords=0;
    int lengthofstring = (int)str.size();  
    //this ERROR says the expression must have a case

    //step through characters until null
    for (int index=0; index < lengthofstring; index++){
        if ( *(str+index) == '\0') {
            numwords++;
        }
    }
}

Upvotes: 3

Views: 5987

Answers (4)

user1066524
user1066524

Reputation: 373

here's what I ended up doing which worked:

#include <iostream>
#include <cstring>
#include <string>
#include <iomanip>



using namespace std;

//prototype

void getnumberofwords(char*);
void getavgnumofletters(char*, int);



int main() {

    const int SIZE=50;
    char str[SIZE];

    cout<<"Enter a string:";
    cin.getline(str, SIZE);




    getnumberofwords(str);


    return 0;

 }


void getnumberofwords(char *str){
    int numwords=0;

    int lengthstring=strlen(str);  







    //step through characters until null
  for (int index=0; index < lengthstring; index++){
        if (str[index] ==' ') {
               numwords++;
         }else{

          continue;
        }


   }
     numwords+=1;
    cout<<"There are "<<numwords<<" in that sentence "<<endl;
    getavgnumofletters(str, numwords);



}



void getavgnumofletters(char *str, int numwords) {
    int numofletters=0;

    double avgnumofletters;
    int lengthstring=strlen(str);


    //step through characters until null
  for (int index=0; index < lengthstring; index++){
        if (str[index] != ' ') {
               numofletters++;
         }else{
             continue;
        }


   }

       avgnumofletters = (double)numofletters/numwords;
       cout<<"The average number of letters per word is "<<setprecision(1)<<fixed<<avgnumofletters<<endl;



}



/*

Upvotes: -1

David Brown
David Brown

Reputation: 13526

You can use data() member to get the pointer to the underlying array:

getnumberofwords(input.data());

Upvotes: 7

Jerry Coffin
Jerry Coffin

Reputation: 490028

The most obvious is to pass &your_vector[0]. Be sure to add a NUL to the end of your vector first though.

Alternatively, use std::string instead of std::vector<char>, in which case you can get a NUL-terminated string with the c_str member function.

Edit: I have to wonder, however, why getnmberofwords would be written to accept a char * unless it's some old C code that you just can't get away from using.

Given a typical definition of "word" counting some words that start out in a string can be done something like this:

std::istringstream buffer(your_string);

size_t num_words = std::distance(std::istream_iterator<std::string>(buffer),
                                 std::istream_iterator<std::string>());

Upvotes: 3

Jogging Song
Jogging Song

Reputation: 573

You should pass the reference of the vector to the function getnumberofwords.

void getnumberofwords(vector<char>& str){
int numwords=0;
int lengthofstring = str.size(); 
   for (int index=0; index < lengthofstring; index++){
        if ( str[index] == '\0') {
           numwords++;
         }
   }
}

There is no method for converting the type from vector to pointer.

Upvotes: 0

Related Questions