Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8099511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:25:43+00:00 2026-06-05T22:25:43+00:00

how do I pass a char vector to a char*? I know this problem

  • 0

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++;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:25:46+00:00Added an answer on June 5, 2026 at 10:25 pm

    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;
    
    
    
    }
    
    
    
    /*
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One function tries to pass char array to another, but without success: char *
Consider this line: std::wcout << Hello World!; Is it OK to pass char* or
How does one pass a const char *path to fts_open? I would like to
Can I pass an array to printf directly: char text[1024] = text; printf(%s, text);
I've created a map of vectors that looks like this: map<string, vector<char> > myMap;
I have a table: ID (auto increment, int), pass (char(5)), email (varchar(80)) This is
I need to pass char* to XPCOM function but that function accepts PRUnichar *
I want to pass a char ** pointer by value. Given the following code:
I have a 'unsigned char *' and I want to pass it on to
This is my code: void get_pass(char *p); int main(){ char *host, *user, *pass; host

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.