#include<iostream>
#include<cmath>
#include<iomanip>
#include<string>
using namespace std;
int main()
{
string word;
int j = 0;
cin >> word;
while(word[j]){
cout << "idk";
j++;
}
cout << "nope";
system("pause");
return 0;
}
This is just a little trial program to test this loop out. The program I am working on is about vowels and printing vowels out from a sequence determined by the user. The string isn't defined until the user types in. Thank you for your guys help in advance.
The size of an
std::string
is not unknown - you can get it using thestd::string::size()
member function. Also note that unlike C-strings, thestd::string
class does not have to be null-terminated, so you can't rely on a null-character to terminate a loop.In fact, it's much nicer to work with
std::string
because you always know the size. Like all C++ containers,std::string
also comes with built-in iterators, which allow you to safely loop over each character in the string. Thestd::string::begin()
member function gives you an iterator pointing to the beginning of the string, and thestd::string::end()
function gives you an iterator pointing to one past the last character.I'd recommend becoming comfortable with C++ iterators. A typical loop using iterators to process the string might look like:
Try this for your loop: