How is an empty string valid?

3

I am trying to make a cycle that reads a string while it is not empty if it fulfills that it does not contain any character because it goes out of the cycle; but I have not been able to get a loop and it never comes out, here's my code:

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;

int main(){
    string nombre;
    cout<<"ingrese una palabra "; cin>>nombre;
    while (nombre[0] !='
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;

int main(){
    string nombre;
    cout<<"ingrese una palabra "; cin>>nombre;
    while (nombre[0] !='%pre%'){
        cout<<nombre<<endl;
        cout<<"ingrese una palabra "; cin>>nombre;
    }
}
'){ cout<<nombre<<endl; cout<<"ingrese una palabra "; cin>>nombre; } }

My problem is in the condition of the while (nombre[0] !='%code%') that apparently does not read it for some reason.

Why do not you meet this condition?

Thank you.

    
asked by DDR 21.06.2018 в 23:43
source

5 answers

1
  

My problem is in the while (nombre[0] !='std::string') condition that apparently does not read it for some reason.

You have problems if you trust that condition.

If the std::string is empty it will have no elements, no element. So accessing the first element of a% empty% co is causing a undefined behavior . For this reason, it could be that remains in a loop and never comes out , or it could launch a runtime error, or it could be invoked demons that would be fired from your nostrils .

The value is read, but we have described in the previous paragraph will be an indeterminate value that is almost never 'std::string' and consequently almost never leaves the loop even if the std::string is effectively empty.

After describing the problem you are with, let's move on to the solutions. If we consult the documentation of empty we see that it has several functions related to its capacity:

  • size : Check if the string is empty.
  • length , max_size : Returns the number of characters stored.
  • reserve : Returns the maximum number of characters the chain can store without exceeding the limits of the library or the implementation.
  • capacity : Save memory to store more characters.
  • shrink_to_fit : Returns the memory reserved for this string, whether it contains data or not. contains.
  • size : Reduces the memory reserved by the string to equal the memory it contains data.

In your case only the functions length , empty and size are relevant, which you could use in the following way:

0
while (nombre.size()){
    cout<<nombre<<endl;
    cout<<"ingrese una palabra "; cin>>nombre;
}

Integer values are implicitly convertible to a Boolean condition with a value false interpreted as true and any other value interpreted as size ; so: if 0 returns a value different from length , it will remain in the loop, the same thing happens with:

empty
while (nombre.length()){
    cout<<nombre<<endl;
    cout<<"ingrese una palabra "; cin>>nombre;
}

Finally, we can know if it is empty also with the function empty , but it requires us to invert the check (since we want the loop to continue while it is not empty):

%code%
while (!nombre.empty()){
    cout<<nombre<<endl;
    cout<<"ingrese una palabra "; cin>>nombre;
}
    
answered by 22.06.2018 / 10:12
source
5

To know if an object of type std::string is empty you have several options at your disposal:

  • length : This method returns the number of characters. If the object is empty, the length will be zero:

    std::string test = /* ... */
    if( test.length() == 0 )
      std::cout << "Cadena vacia";
    
  • size : This method is equivalent to length . The existence of size is justified so that std::string can be used with functions of containers ( std::vector , std::set , etc ...)

  • empty : This method returns a boolean that indicates whether the object is empty or not ... simply and simply does exactly what you ask for.

    if( test.empty() )
      std::cout << "Cadena vacia";
    
  • Iterators: Okay, it's not the most orthodox way to do it but now it's another way of verifying that the string is empty:

    if( test.begin() == test.end() )
      std::cout << "Cadena vacia";
    
answered by 22.06.2018 в 08:03
3

The easiest way to do it is Elnombredetustring.empty() putting it is some if for example:

if(Elnombredetustring.empty())
{
  cout<<"Esta vacio"
}

And with your question why the way you try to do it does not work, it's because it works so you show it:

while (nombre[0] !='
if(Elnombredetustring.empty())
{
  cout<<"Esta vacio"
}
'){ cout<<nombre<<endl; cout<<"ingrese una palabra "; cin>>nombre; }

It means that the name has a value of 0 that is not null, that is, you would have to match the string to the char of 0, but that does not mean that it is empty.

    
answered by 22.06.2018 в 03:49
2

You can try TextIsEmpty,

int main(){
    string nombre;
    cout<<"ingrese una palabra "; cin>>nombre;
    while (nombre.TextIsEmpty()){
        cout<<nombre<<endl;
        cout<<"ingrese una palabra "; cin>>nombre;
    }
}

or put it with a defined value that would be zero

   int main(){
        string nombre;
        cout<<"ingrese una palabra "; cin>>nombre;
        while (strlen(nombre) == 0){
            cout<<nombre<<endl;
            cout<<"ingrese una palabra "; cin>>nombre;
        }
    }
    
answered by 22.06.2018 в 00:19
-1

You try to try putting nombre.IsEmpty() in the if which returns a true or false depending on whether the string is empty or not. You can also try using strlen(nombre) == 0 , what strlen does is count the number of indexes of the string (or the length of the string). I hope it serves you.

    
answered by 22.06.2018 в 00:06