0

So I have been trying for 1.30 hour to get this to work. I am new indeed, but I have searched all over the place and couldn't find an exact answer. I do not wish to do this another way, as it would take away the entire purpose of learning to code. I have to find why this thing isn't working. I tried dozens if not hunderds of syntaxes, but nothing works.

I want to read in a const char* name, than count the number of elements in it, so I thought had to be strlen(), and than output the name and the number of elements. If that works I can write the rest of the code.

#include <iostream>

using namespace std;

int main()
{
    //writing your name, and counting the characters including \0
    int a;
    const char* name;

    a = int strlen(name);

    cin.getline(name);

    cout << name;
    cout >> a;

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Stephane
  • 29
  • 1
  • 6
  • I don't think a useful or effective way to acquire knowledge is to randomly permute syntax without effort of understanding the ingredients. Try first to understand what a pointer is and how pointers are used, and you should see your problem. – Kerrek SB Feb 14 '17 at 23:38
  • thx remy, i will look for good syntax next time i post. but effort you have no idea. i have been going through my book, i just don't see it. I understand it is a pointer but everywhere i look, they say use strlen(name) to get the size of the const char* variable – Stephane Feb 14 '17 at 23:39
  • Maybe you have been using a bad book, which is likely since you are using `const char *` to assign to it later and `strlen` tagging it C++. Maybe you should look [here](http://stackoverflow.com/a/388282/3484570). – nwp Feb 14 '17 at 23:43
  • this is what i found everywhere: char *ptr = "stackoverflow" size_t len = strlen(ptr); if i copy that exactly it still doesnt work, i get strlen was not declared in this scope – Stephane Feb 14 '17 at 23:45
  • `char *ptr = "stackoverflow"; size_t len = strlen(ptr);` is valid code, because `ptr` points at valid memory that `strlen()` can loop through. The code you wrote is not valid code, it has several errors in it. – Remy Lebeau Feb 15 '17 at 00:23

3 Answers3

1

There are a lot of problems with your code.

You are not allocating any memory for cin.getline() to read into. const char* name; is declaring an uninitialized pointer to nothing. You have to allocate memory for name before you can then read any data into it.

cin.getline() expects two input parameters (a pointer to an allocated buffer, and the max number of characters the buffer can hold), but you are only passing in one value.

You are calling strlen() before you have read anything into name (and there is a syntax error on your strlen() statement anyway).

You are passing a to std::cout using >>, but std::ostream does not implement the >> operator. You have to use << instead.

And lastly, don't use using namespace std;.

Try this instead:

#include <iostream>
#include <cstring>

int main()
{
    //writing your name, and counting the characters including \0
    int a;
    char name[32];
    
    std::cin.getline(name, 32);
    a = std::strlen(name);

    std::cout << "You entered: " << name << std::endl;
    std::cout << "It is << a << " chars in length" << std::endl;

    return 0;
}

Or, if you really don't like using std:: everywhere, at least use using <identifier>; instead of using namespace std;:

#include <iostream>
#include <cstring>

using std::cin;
using std::strlen;
using std::cout;
using std::endl;

int main()
{
    //writing your name, and counting the characters including \0
    int a;
    char name[32];
    
    cin.getline(name, 32);
    a = strlen(name);

    cout << "You entered: " << name << endl;
    cout << "It is " << a << " chars in length" << endl;

    return 0;
}

Now, that being said, the preferred solution is to use std::getline() instead of cin.getline():

#include <iostream>
#include <string>
    
int main()
{
    int a;
    std::string name;
    
    std::getline(std::cin, name);
    a = name.length();

    std::cout << "You entered: " << name << std::endl;
    std::cout << "It is " << a << " chars in length" << std::endl;

    return 0;
}
Tim
  • 21
  • 1
  • 7
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • That works perfectly, but how about if i don't want to use any string commands, which is the point, i am stuck on something and i am not willing to find another, or more easy way. I want to solve it using a const char* and a strlen command on that, so i learn how i need to use those things. – Stephane Feb 14 '17 at 23:52
  • @Stephane: the very first example I gave you fixes the main problems of your code - you were not allocating any memory for `cin.getline()` to fill in, and you were not calling `cin.getline()` correctly. Fix those two mistakes, and then you can use `strlen()` properly. – Remy Lebeau Feb 15 '17 at 00:28
  • ok, i understand you perfectly, but this requires to give the array a fixed size. Which was my whole point to avoid. But i guess i am just ridiculous in what i try. I start to see that myself :) – Stephane Feb 15 '17 at 00:35
  • `cin.getline()` requires a fixed-length buffer as input, and it won't read more than the buffer can hold. Whether you allocate that buffer statically at compile-time or dynamically at run-time does not matter. If you want to handle variable-length input, you have to use `std::string` instead, which means using `std::getline()` instead of `cin.getline()`. – Remy Lebeau Feb 15 '17 at 00:38
1

I found a working solution, although I don't see where I had gone wrong. But this does exactly what I want using const char* and strlen() without using std::string.

Thanks for all your help, you have all pointed me to the correct direction.

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int main ()
{
   const char *name;
   int len;

   name = "stephane";

   len = strlen(name);
   cout << name;
   cout << len;

   return(0);
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Stephane
  • 29
  • 1
  • 6
  • This code does not satisfy your original requirements because it is not actually reading any input from the user. Just because this "works" does not make it "right". – Remy Lebeau Feb 15 '17 at 00:26
  • hi remy, i am working now on the input, havent figured that one out yet, but as you see, i am trying and i am not giving up, till i find the syntax, this way i will never forget. Although some help on how the input is done would be helpfull. I have learned now that as it is a pointer the name needs to be completely defined before i attempt any other work on it. ( reason of jumping through hoops is that i don't want to define a array of char like name[120]. I want it to be variable upon input and no bigger that need be. ( no buffer overflow ) . If i can get the input statement – Stephane Feb 15 '17 at 00:29
  • The answer I posted earlier shows you *exactly* how to read input the right way. What are you still trying to "figure out"? – Remy Lebeau Feb 15 '17 at 00:31
  • yours is better, without a doubt. but i can not use any strings. sorry that's the point of the exercise for me. – Stephane Feb 15 '17 at 00:32
  • I gave you an example that doesn't involve strings. You still have to allocate a character buffer, and that seems to be the piece you are not understanding. – Remy Lebeau Feb 15 '17 at 00:34
  • yes indeed, i am using that now, it seems without buffer this can not be solved. So thanks for your help. The next question is coming up. Read the integers in a char array seperated by a white space. (Again over my head i think ) :))) – Stephane Feb 15 '17 at 00:39
  • Make sure you post that as a new question. – Remy Lebeau Feb 15 '17 at 00:40
0

As another user has pointed out, I think it's a good idea for you to take a few steps back and read the basics until you understand how pointers work.

A const char* is that: const. It could be used usually while doing things like this:

const char* cpName = "Stephane"; //expected not to change through the program's lifetime
char* pName = "Stephane"; //can be changed to point to something else
char *pOther = "Vada";
pName = pOther; //pName now points to the string "Vada"
cpName = pOther; //this won't compile as cpName is const
Vada Poché
  • 763
  • 5
  • 16
  • 1
    More accurately, a `const char*` is a **non-const** pointer to **const** memory. The memory being pointed at cannot be written to, but the pointer itself can be changed to point at different memory. Also, pointing a non-const `char*` to a string literal is deprecated in C++11, you have to use `const char *` instead. – Remy Lebeau Feb 15 '17 at 00:37
  • Thanks for the comments, @RemyLebeau – Vada Poché Feb 15 '17 at 01:00