Hi. I am having a problem with an assignment I am working on. I am suppose to read in two sentences from a user, read in each character one at a time, and check to see if the last three letters of the last word in each sentence are the same. Each sentence ends with a newline character. I currently just have the user entering one sentence, until I can get the first one working! I am not suppose to use strings or arrays, just single characters.
Attached is my source code. I have a loop to read in the characters one at a time, but am not sure how to 'extract' the last three letters out of each sentence (because I don't have a variable assigned to each individual letter, not knowing how long of a sentence the user is entering). Also, my program feels big and bulky with lots of variables. Is there a easier way?
Thanks in advance!
[CODE]Code:#include <iostream.h> using namespace std; // assign variables int main () { char ch; char ch1; char ch2; char ch3; char ch4; char ch5; char ch6; char ch7; char ch8; char ch9; char ch10; char ch11; char ch12; char ch13; char ch14; char ch15; int i = 0; //get line 1 of poem from user cout << "Please enter a sentence followed by return" <<endl; cin.get(ch); // loop that will loop through and read in // each character one at a time //echoing back just for testing. while (ch != '\n'){ if (i == 15) { ch = ch1; i = 0; } if (ch == '\n') continue; continue; ch1 = ch; cout << ch1 << endl; i++; cin.get(ch); if (ch == '\n') continue; ch2 = ch; cout << ch2 <<endl; i++; cin.get(ch); if (ch == '\n') continue; ch3 = ch; cout << ch3 <<endl; i++; cin.get(ch); if (ch == '\n') continue; ch4 = ch; cout << ch4 <<endl; i++; cin.get(ch); if (ch == '\n') continue; ch5 = ch; cout << ch5 <<endl; i++; cin.get(ch); if (ch == '\n') continue; ch6 = ch; cout << ch6 <<endl; i++; cin.get(ch); if (ch == '\n') continue; ch7 = ch; cout << ch7 <<endl; i++; cin.get(ch); if (ch == '\n') continue; ch8 = ch; cout << ch8<<endl; i++; cin.get(ch); if (ch == '\n') continue; ch9 = ch; cout << ch9<<endl; i++; cin.get(ch); if (ch == '\n') continue; ch10 = ch; cout <<ch10<<endl; i++; cin.get(ch); if (ch == '\n') continue; ch11 = ch; cout <<ch11<<endl; i++; cin.get(ch); if (ch == '\n') continue; ch12 = ch; cout <<ch12<<endl; i++; cin.get(ch); if (ch == '\n') continue; ch13 = ch; cout << ch13<<endl; i++; cin.get(ch); if (ch == '\n') continue; ch14 = ch; cout <<ch14<<endl; i++; cin.get(ch); if (ch == '\n') continue; ch15 = ch; cout <<ch15<<endl; i++; } if (ch == '\n') //number of loops it went through cout<< i; return 0; }



LinkBack URL
About LinkBacks


