0

Does anyone know how to write a function that counts length of an array given like in example ?

cout << length(argv[1]) << endl;

I've tried somethig like this but it says that there can't be function lentgh then.

for(int i = 0; myStrChar[i] != '\0'; i++)
{
      count++;
}

EDIT1: Here is the rest of code :

#include <iostream>
#include<cstring>
using namespace std;
int i;
int length()
{


for(int i = 0; myStrChar[i] != '\0'; i++)
{
      count++;
}
}
int main()
{

    cout << "Hello world!" << endl;
    cout << "length of first text: ";
cout << length(argv[1]) << endl;
char tab[255] = {"Ana has a cat"};

EDIT2: Now I did something like this but it puts out 108 as length of text

char argv[]="ana has a cat";
int length(char l)
{
int cou= 0;

for(int i = 0; i!=0; i++)
{

     cou++;
     return cou;
}
}
  • What goes wrong when you do this? – cigien Apr 24 '20 at 20:25
  • What is the prototype for `length`? – 001 Apr 24 '20 at 20:27
  • To be precise it says for too many arguments in function int length() – Mikołaj Jarząbkiewicz Apr 24 '20 at 20:30
  • I understand it's an exercise, but someone has to watch you `std::strlen`: https://en.cppreference.com/w/cpp/string/byte/strlen – Sandburg Apr 24 '20 at 20:36
  • In your code `int length()` does not take any parameter. `i` should not be a global variable. `myStrChar` and `count` are not declared. `using namespace std;` is a bad practice that should be avoided. It could cause you some problems. – drescherjm Apr 24 '20 at 20:43
  • `argv[1]` is only valid if `argc` is greater than 1 and you did not use the correct signature for main to use argv or argc. [https://stackoverflow.com/questions/2108192/what-are-the-valid-signatures-for-cs-main-function](https://stackoverflow.com/questions/2108192/what-are-the-valid-signatures-for-cs-main-function) – drescherjm Apr 24 '20 at 20:46
  • But for an example if declare that argv= "Mynameis John" will it work ? – Mikołaj Jarząbkiewicz Apr 24 '20 at 20:53
  • You've posted a whole code. So here is several problems: you don't return anything from `length`. The compiler should probably warn you about it. Moreover: there's no definition and declaration of `argv` - this array doesn't exist for compiler! – Georgy Firsov Apr 24 '20 at 21:01

1 Answers1

1

Hmm, what's wrong? It is a valid code - just wrap it into a function:

size_t length(const char* myStrChar)
{
    size_t count = 0;
    for(int i = 0; myStrChar[i] != '\0'; i++) { 
        count++;
    }
    return count;
}

You can do it a bit shorter:

size_t length(const char* myStrChar)
{
    size_t count = 0;
    for (; myStrChar[count] != 0; count++)
        /* Do nothing */;
    return count;
}

UPD: Here is a code from the question and my comments to author:

// You're passing just a single character to this function - not array
// of char's - not a string.
// Pass char* or better const char*.
int length(char l)
{
int cou= 0;

// Ok, this loop does nothing - it's going to stop at the
// first condition check, because you assign 0 to i
// and immediately check if it is not zero - this check will
// return false and the loop will never start.
// Check the current symbol in string if it is not zero
for(int i = 0; i!=0; i++)
{

     cou++;

     // Even if you'll fix mistakes above, your loop will stop
     // at the first iteration, because of this return.
     // You must put it right after the loop.
     return cou;
}

// Your function actually returns nothing, because no iterations
// of the loop above is performed - so no return statement reached
// at all - it is undefined behaviour :(
// Put return here out of the loop;
}
Georgy Firsov
  • 286
  • 1
  • 13
  • Now I have a message: invalid types char[int] for array subscript – Mikołaj Jarząbkiewicz Apr 24 '20 at 20:36
  • @Mikołaj Jarząbkiewicz, well, there's something wrong with a string you pass to this function. I case of `char* argv[]` in main it'll work fine (with each element of the array). – Georgy Firsov Apr 24 '20 at 20:57
  • 1
    "*You can do it a bit shorter*" - yeah, by getting rid of this `length()` function altogether and use [`std::strlen()`](https://en.cppreference.com/w/cpp/string/byte/strlen) instead, which accomplishes the exact same thing this `length()` function is doing manually. – Remy Lebeau Apr 24 '20 at 21:38
  • 1
    @RemyLebeau, of cource using `std::strlen` is the obvious solution (moreover, you'd better prefer `std::string` for strings in most cases), but I think the author of the question just learns C++. It is useful to write such simple function by youself at the beginning :) – Georgy Firsov Apr 24 '20 at 21:46