I am writing a get function within a class of an object oriented program, but the i++ in my code is not executing for some reason.
This is what is used in my .cpp file:
char MyString::Get(int i)
{
if( i = '\0')
{
exit(1);
}
else
{
return String[i];
}
}
This is what is called in the main.cpp file:
for(int i=0; i < String1.Length()+1; i++)
{
cout<< String1.Get(i)<<" ";
}
cout << endl;
This is the length method in the .cpp file for reference purposes:
int MyString::Length()
{
int counter(0);
while(String[counter] != '\0')
{
counter ++;
}
return (counter);
}
Also: String1 = Jello World
The output:
J J J J J J J J J J J J J
Well – probably cause of this. You assigned instead of compared. You assigned zero to i. That failed the ‘if’ test (it’s zero), so exit did not get called and the rest of your routine ran with i == to zero.