I am trying to make a loop that reads a string as long as it is not empty, if it does not contain any character then the loop is exited; but I have not been able to get it, it stays in a loop and never exits, here is 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] !='\0'){
cout<<nombre<<endl;
cout<<"ingrese una palabra "; cin>>nombre;
}
}
My problem is in the while condition (nombre[0] !='\0')
that apparently doesn't read it for some reason.
Why does it not meet this condition?
Thanks.
To find out 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:size
: This method is equivalent tolength
. The existence ofsize
is justified so thatstd::string
it can be used with container functions (std::vector
,std::set
, etc...)empty
: This method returns a boolean indicating whether the object is empty or not... plain and simple it does exactly what you ask.Iterators: Okay, it's not the most orthodox way to do it, but for that matter, it's a different way of verifying that the string is empty:
The easiest way to do it is
Elnombredetustring.empty()
by putting itif
in some example:And with your question why the way you try to do it doesn't work is because for it to work as you show:
It means that the name has a value of 0, not that it is null, that is, you would have to match the string to the char of 0, but that does not mean that it is empty.
You can try with TextIsEmpty,
or put it with a defined value that would be zero
You're in trouble if you rely on that condition.
If the
std::string
is empty it will have no elements, no elements. So by accessing the first element of astd::string
void you are causing undefined behavior . So it could get stuck in a loop and never exit , or it could throw a runtime error, or daemons could be spawned out of your nostrils .The value is read, but we described in the previous paragraph it will be an indeterminate value that is possibly almost never
'\0'
and therefore almost never exits the loop even though itstd::string
is effectively empty.Having described the problem you are facing, let's move on to the solutions. If we consult the documentation
std::string
we see that it has several functions related to its capacity:empty
: Check if the string is empty.size
,length
: Returns the number of characters stored.max_size
: Returns the maximum number of characters the string can store without exceeding library or implementation limits.reserve
: Reserve memory to store more characters.capacity
: Returns the memory reserved for this string, whether it contains data or not.shrink_to_fit
: Reduces the memory reserved by the chain so that it is equal to the memory that contains data.size
In your case only the ,length
and functions are relevantempty
, which you could use as follows:size
Integer values are implicitly convertible to boolean condition being a value
0
interpreted asfalse
and any other value interpreted astrue
; so: if itsize
returns a value other than0
, it will stay in the loop, exactly the same thing happens with:length
Finally, we can tell if it's empty with the function as well
empty
, but it requires us to reverse the check (since we want the loop to continue as long as it's not empty):empty
You can try putting
nombre.IsEmpty()
in the if which returns true or false depending on whether the string is empty or not. You can also try usingstrlen(nombre) == 0
, what strlen does is count the number of indices of the string (or the length of the string). I hope it helps you.