//Demonstrates using a string object as ig it were an array.
#include<iostream>
#include<string>
usingnamespace std;
int main()
{
string first_name, last_name;
cout<<"Enter your first and last name:\n";
cin >> first_name >> last_name;
cout<< "Your last name is spelled:\n";
int i;
for(i = 0; i < last_name.length(); i++)
{
cout<< last_name[i] << " ";
last_name[i] = '_';
}
cout<< endl;
for(i = 0; i < last_name.length(); i++)
cout<< last_name[i] << " "; //Places a
// "_" under each letter.
cout << endl;
cout << "Good day " << first_name << endl;
return 0;
}
> int i;
> for(i = 0; i < last_name.length(); i++)
Get a better text book.
It's been 20 years since this way of declaring a for loop was necessary in C++.
Try for ( size_t i = 0; i < last_name.length(); i++)
Or for ( string::size_type i = 0; i < last_name.length(); i++)
Mismatching the data type can be a warning, Visual Studio doesn't warn at default settings, unless you are compiling with the option warnings are treated as errors.
Your book is likely outdated, not using the latest features of the C++ standard. Creating the loop variable outside the for loops is not C++ style. It is something C would do.
What is your compiler? What is the publication date of your textbook?
salem c wrote:
Get a better text book.
That might not be an option, the textbook is probably required by the course/instructor. However outdated it looks to be.