Here is a working piece of code for your assignment. Fully done, i was bored so don't expect it to be done often....
Code:
#include <iostream>
using namespace std;
int main()
{
// Variables s1 = Sentence 1, s2 = Sentence 2, ch1 = Character 1, etc.....
char s1ch1 = '\0';
char s1ch2 = '\0';
char s1ch3 = '\0';
char s2ch1 = '\0';
char s2ch2 = '\0';
char s2ch3 = '\0';
// Get the first sentence
cout << "Please enter your first sentence: ";
s1ch1 = cin.get();
s1ch2 = cin.get();
s1ch3 = cin.get();
while(cin.peek() != '\n')
{
s1ch1 = s1ch2;
s1ch2 = s1ch3;
s1ch3 = cin.get();
}
cin.get(); // Gets the last new line from the buffer
cout << "Please enter your second sentence: ";
s2ch1 = cin.get(); // Gets first letter in sentence
s2ch2 = cin.get(); // Gets second letter in sentence
s2ch3 = cin.get(); // Gets third letter in sentence
while(cin.peek() != '\n')
{
s2ch1 = s2ch2;
s2ch2 = s2ch3;
s2ch3 = cin.get();
}
cin.get(); // Gets the last new line from the buffer
// Checks if the three characters from each sentence are the same
if((s1ch1 == s2ch1) && (s1ch2 == s2ch2) && (s1ch3 == s2ch3))
cout << "Last Three latters where the same." << endl;
else
cout << "Last Three letters where not the same." << endl;
return 0;
}
if you don't know how cin.peek() works you might wanna consider replacing:
Code:
while(cin.peek() != '\n')
{
s1ch1 = s1ch2;
s1ch2 = s1ch3;
s1ch3 = cin.get();
}
cin.get(); // Gets the last new line from the buffer
and the equivilant block for the second sentence with something like:
Code:
char temp = cin.get();
while(temp != '\n')
{
s1ch1 = s1ch2;
s1ch2 = s1ch3;
s1ch3 = temp;
temp = cin.get();
}
in the basis what this program does is it gets the first three letter from the sentence. Then until it finds a '\n' it will set the first letter equal to the first, the second equal to the third and the third equal to one newly gotten from the buffer.