c++ problem with character manipulation

This is a discussion on c++ problem with character manipulation within the C++ Programming forums, part of the General Programming Boards category; Hi. I am having a problem with an assignment I am working on. I am suppose to read in two ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    2

    c++ problem with character manipulation

    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;
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Code:
    bool validate(void)
    {
     int count = 1;
     char ch, last_char = 0;
     
       while(1)
      {
       cin.get(ch);
    
          if(ch == '\n')
         {
             if(count == 3)
              return true;
             else
              return false;
         }
          else if(ch == last_char)
           ++count;
          else
           count = 1;
    
       last_char = ch;
      }
     return false;
    }
    
    int main()
    {
     bool condition1 = validate();
     bool condition2 = validate();
    
     cout << "The input was ";
    
     if(condition1 && condition2)
      cout << "valid";
     else cout << "invalid";
    
     cin.get();
    }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    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.

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    2
    Thanks, that helped out a lot. I am new to C++ and because my classes are so large, there isn't much opportunity for any one on one interaction with the instructor. I wouldn't have thought of shifting the variables, but that makes the whole program much simplier.

    Thanks for you help and also for explaining why you did what you did!

    scrapper777

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character problem
    By Guru_ in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2009, 06:12 PM
  2. Blank Character Constant Problem
    By rrc55 in forum C Programming
    Replies: 9
    Last Post: 02-10-2009, 02:50 PM
  3. problem comparing character
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 03-19-2004, 11:15 AM
  4. Problem with character arrays in classes
    By spoketoosoon in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2004, 02:57 AM
  5. Comparing Character Arrays problem...
    By newy100 in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2003, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21