C++ Programmers unite!

Nov 08, 2004 01:05

My friend is taking C++ This semester and i'm having issues helping him because I can't remember C++ to save my ever loving life. This program does not exit correctly. It's supposed to exit on it's own after it's analyzed the line of code but it doesn't until you'v ehit enter 4 times for a short text entry and 6 times for a long text entry.

If you could help us fix his program i'd appreciate it :) Program behind the cut.


/* This program decodes secret messages in text and
displays them as resulting text.

INPUT: User inputs the text to decode

OUTPUT: Outputs the message
*/

#include
using namespace std;

int main()
{
char spacetest1; //tests the space for doubles
char spacetest2; //second variable to test the space
char ch; //character variable used to read individual chars
char one; //first letter variable
char two; //second letter variable
char three; //third letter variable

char newline; //tests the new line variable
newline = 'n';
char newlinetest ( char & newline, char & ch );

cout << "input a line of text: ";
//puts the line in the input buffer

//reads in the data
cin.get(ch);
one = ch;
cin.get(ch);
two = ch;
cin.get(ch);
three = ch;
//do while loop to test the data and output it.
do {
//tests the line ex: aa' ' will output a
if (( one == two ) && (three == ' '))
cout << one;
//outputs the first letter if it isn't a double letter
if ( one != two )
cout << one;
else cout << three;
do { cin.get(ch);
} while (( ch != ' ')&& ( ch != '\n'));

// ch = a space or a new line
newlinetest (newline, ch);
//tests for new line

spacetest1 = ch; //stores space in variable
cin.get(ch);
spacetest2 = ch; //puts a potential letter in two
if ( spacetest1 == spacetest2 )
cout << ' ';
else { one = spacetest2; }
if (spacetest2 != one )
{
cin.get(ch);
one = ch;
cin.get(ch);
two = ch;
cin.get(ch);
three = ch;
}
else {
cin.get(ch);
two = ch;
cin.get(ch);
three = ch;
}
//if no space is there, two is the first
//letter in the next word, and is then
//is stored as such, else it reads in
//new data

}

while (newline == 'n');
cout << "weeeeeeeeeeeee go away now";

return 0;
}

char newlinetest ( char & newline, char & ch )
{
if ( ch == '\n' )
newline = 'y';
else newline = 'n';
}
Previous post Next post
Up