-2

i tried strlen for char to get howmany characters in cstring to join a loop which looks for charcaters in the code but strlen just returned 0 the email[] i made that empty to get any value in it

using namespace std;
#include <iostream>
#include <cstring>
int main(int argc, char **argv)


{
char email[] = "";

cout << "Enter the email in question"<< endl;
cin >> email;

int size = strlen(email);

for (int i = 0; i < size; i++)
{
    cout << email;
    if (email[i] == '@' )
    {
        cout << "valid email";
}
    
}
  • 3
    `char email[] = "";` defines an array of exactly the size to store a null terminator. You can't read anything useful into this string. – user4581301 Dec 21 '21 at 19:42
  • 2
    why not use `std::string` ? – 463035818_is_not_an_ai Dec 21 '21 at 19:45
  • 2
    You are treating `char email []` as if it's a `std::string`. If you did change that to `std::string`, then all of that other code would work (except for the `strlen` -- you would use `size()` for that). – PaulMcKenzie Dec 21 '21 at 19:45
  • Your program has _undefined behavior_, it's not commonly predictable what and why specifically happens. – πάντα ῥεῖ Dec 21 '21 at 19:46
  • Assuming the assignment must be completed without `std::string`, `char email[20]` would allow you to read 19 characters into this string (never forget the null terminator!), but `cin >> email;` doesn't know how big array is (See [array decay](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay)) and will happily read off the end of the array and put you in a similar boat. You want to use something that allows you to specify the length of the array like [`cin.getline(email, sizeof(email));`](https://en.cppreference.com/w/cpp/io/basic_istream/getline) – user4581301 Dec 21 '21 at 19:47
  • @πάνταῥεῖ until c++20, there's now an overload to `>>` which knows the array size https://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt2 – Alan Birtles Dec 21 '21 at 19:50
  • It was only a matter of time before a `>>` template that correctly ate arrays got added. Have to wonder why it took until C++20. – user4581301 Dec 21 '21 at 20:00
  • In this day and age, why is anyone being taught unsafe code in C++? OK, so let's say the array is increased to 20. All someone has to do is write an input script that inputs more than 20 characters, and that's a buffer overrun. Buffer overruns have been used to exploit systems. – PaulMcKenzie Dec 21 '21 at 20:06

1 Answers1

1
char email[] = "";

This is an empty string. The length of the string is 0, and the size of the array is 1. The only element is the null terminator.

but strlen just returned 0

This is hardly surprising given that the only string that can fit in the array is a string of length 0.

cin >> email;

Prior to C++ 20, this statement used to be highly unsafe. If you were to provide longer input than the size of the array, then you overflow the array. Since the size of the array is 1, the only string that fits is the empty string and thus any non-empty input would overflow.

Overflowing the array will cause undefined behaviour. Hence the behaviour that you observed would not be guaranteed. This is bad; don't do this.

Since C++20, the operator will safely read only as many characters as fit in the array, taking null terminator into consideration - which is 0 characters in this case. This is safe, but probably not what you inteded.

A good solution is to use std::string:

std::string email;
std::cin >> email;
int size = email.size();
eerorika
  • 232,697
  • 12
  • 197
  • 326