Loop output is not quite what I want

This is a discussion on Loop output is not quite what I want within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to figure out how to make a program read 1 character at a time but have ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    32

    Unhappy Loop output is not quite what I want

    Hi,

    I am trying to figure out how to make a program read 1 character at a time but have run into a snag. When I enter my data, my first character entered does not print. Is there a way to fix this in my loop?

    I know using normal strings and arrays would be much easier but I am trying to learn more about character manipulation here.

    An example of the input out put is this:

    [input]: Hello world
    [output]: ello world
    [input]: Hello world
    [output]: Hello world.
    <EOF>

    You can see that the first output 'H' is missing and it is because of the way my loop is incremented but I do not know how to fix this.

    The code I have is:

    Code:
    #include<iostream>
    #include<cctype>
     
    
    using namespace std;
     
    
    int main()
    {
        char text_main; 
        int count;
     
        text_main = 0; 
        count = 0;
     
        cin.get(text_main);
     
        while(text_main != '.')
        {
            count++;
            cin.get(text_main);
            cout << text_main;
        }
     
        cout << endl; 
     
     
     return 0;
    }
    Also, Is there a way to make sure that first (and only the first) letter is always converted to a Capital with all of the rest lower?

    Any ideals?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    cin.get(text_main);
     
    while(text_main != '.')
    {
            count++;
            cin.get(text_main);
            cout << text_main;
    }
    Look at the flow of your code. You read one character, check it's value, read another character, then write it out. So when did you write out the first character? (You didn't, obviously). How about trying something like this:
    Code:
        while ((text_main = cin.get()) != '.')
        {
            count++;
            cout << text_main;
        }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    Well I tried the changes and the same thing happend but what you wrote down did the job for me by pointing out my mistake.

    Look at the flow of your code. You read one character, check it's value, read another character, then write it out. So when did you write out the first character? (You didn't, obviously).
    That was what I needed more than anything. I forgot to read my own code step by step.

    I changed it to

    Code:
    #include<iostream>
    #include<cctype>
    
    
    using namespace std;
    
    
    int main()
    {
         char first_letter;		
         char text_main;	
         int count;
    	
    
         first_letter = 0;
         text_main = 0;	
         count = 0;
    
         cin >> first_letter;
         cout << first_letter;
    	
    
         while ((text_main = cin.get()) != '.')
        {
              count++;
              cout << text_main;
        }
    
    	
         cout << endl;	
    	
         return 0;
    }
    Thank you Hammer.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    cin >> first_letter;
    cout << first_letter;
    Whats the point of those 2 lines? You shouldn't need them.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    maybe i am wrong in asking this, but why are you initializing character variables to 0? Why initialize them like that at all? Its my understanding that

    char variable[];

    is al thats needed before its used, if i am incorrect please correct me.

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    Hammer wrote,

    Whats the point of those 2 lines? You shouldn't need them.
    I just took them out, you're right...they are not needed

    Code:
    #include<iostream>
    #include<cctype>
    
    using namespace std;
    
    int main()
    {
         char text_main;	
         int count;
    
         count = 0;
    	
         while ((text_main = cin.get()) != '.')
         {
            count++;
            cout << text_main;
        }
    
         cout << endl;	
         
         return 0;
    }

    Ride -or- Die wrote,

    maybe i am wrong in asking this, but why are you initializing character variables to 0? Why initialize them like that at all? Its my understanding that

    char variable[];

    is al thats needed before its used, if i am incorrect please correct me.
    Not at all Ride -or- Die, please correct me, I'm the one learning here

    The book that I bought to learn C++ has its examples like that, so I have been doing the same. I take it it is not needed for a char. I have removed them from my code.

    Also, for the heck of it I hit your Read Me link and I am glad that I did. One of the FAQs answered my other question:
    Is there a way to make sure that the first (and only the first) letter is always converted to a Capital with all of the rest lower?
    I'm glad I read it.


    Once again thanks for the help people. I'm sure I'll have more questions soon enough.
    Last edited by Zalbik; 10-27-2002 at 08:27 PM.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Hmmm...
    first off, don't include headers you aren't useing (cctype). Unless, of course, you were planning on using that later.

    secondly, whats the counter even there for, if I may ask? Are you going to use it later?

    And I have a question.. I thought that char variables have to be given a size, e.g.

    char some_text[256];

    Why is his code valid in that respect? Thanks

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    The <cctype> is being used in another part of this program. I only posted the code for the part that I was having a problem with.

    As for the counter "count" in the loop it will be used for something that I have not done yet. Also I am not going to use strings in this program or the tools in the <cstring> header. This is more of a learning experience for me on character manipulation than a program with a practical use.

    Now that I have the program reading the text that is input 1 character at a time I want to see if I can get the program to remove all reapeating characters...so if the input is Seattle, the output would be Seatle. It may be simple stuff but for me it is a good learning experience. If there is a better way of doing it I would love to know, but as I have said I want all input text read a character at a time.

    Also, here is a question...if I instead used a char with a defined size such as:

    char some_text[256];

    What happens if the input from the user is greater than the size defined? Wouldn't the data be lost or is the size of the defined char automatically increased?
    Last edited by Zalbik; 10-30-2002 at 08:50 PM.

  9. #9
    J_2023
    Guest
    you can use the function prototype

    int toupper(char)

    this will return the uppercase equivalent if the character is lowercase, otherwise leavs it unchanged

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Captain Penguin
    And I have a question.. I thought that char variables have to be given a size, e.g.

    char some_text[256];

    Why is his code valid in that respect? Thanks
    Thats a char array, which is different from a char.

    Zalbik: You can use a single char to catch your input, but if you need to compare the previously read character with the most recent one, you'll need two char variables. You can do this like this:
    >char Current;
    >char Previous;
    (obviously the names are your choice).
    Once you've read a character, and compared it to the previous one, just assign the value of Current to Previous, and go round the loop again. Don't forget to initialise Previous to something before you go through the loop the first time though
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Nice to know the sig links do work : )

    I always declared

    char variable[];

    rather than

    char variable[256];

    because i thought if its [] it will continue to grow as needed.

  12. #12
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    You can use a single char to catch your input, but if you need to compare the previously read character with the most recent one, you'll need two char variables. You can do this like this:
    >char Current;
    >char Previous;
    (obviously the names are your choice).
    Once you've read a character, and compared it to the previous one, just assign the value of Current to Previous, and go round the loop again. Don't forget to initialise Previous to something before you go through the loop the first time though
    Thats sound like what I should do. I will need to compare the current to the previous character for some(most) of the thing that I will be doing. I'll try it out tonight when I get off work.

    Again, my thanks.

    [added on lunch break]
    Well I couldn't wait till I go home so I broke open my laptop and tried it out. So I made some simple changes:
    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
        char current;
        char previous;     
        int count;
     
        count = 0;
        previous = 0;  
      
        while ((current = cin.get()) != '.')
        {
              count++;
    
              if(count == 1)
              {
                    current = toupper(current);
                    cout << current;
              }
    		
              while ((previous = cin.get()) != '.')
              {
                    count++;
                    cout << previous;
              }
         }
    
          cout << endl ;	
          return 0;
    }
    If I enter hello world I get back Hello world which is what I want.

    My question now is this: does the way I have this set up now allow me to compare the previously read character with the most recent one? It seem to be at least reading in the input in correctly but am I going down the right path or did I take a step back with out knowing it? I suppose I will find out soon enough as tonight I want to try to read and compare 2 characters and see what I can do with them.

    I must say number manipulation is a whole lot easier than characters but at least I am enjoying the challenge.
    Last edited by Zalbik; 10-31-2002 at 05:08 PM.

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You don't need the second while loop, you're making it over complicated. Here's some semi-psuedo code to try and help:
    Code:
        while ((current = cin.get()) != '.')
        {
            if current != previous
                cout << current;
            previous = current;
        }
    OK, so ignored the uppercase part, but I'm sure you can combine the two sections of code youself.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    THe answer to your question is yes. And, like the last post said, you are making it over complicated.

    To simplify your code, you should ONLY capitalize current in the first IF statement. After that, you can do the normal current/previous comparisons, etc, and cout the result.
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  15. #15
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    Ok, I am back to the the quest.

    Well I have corrected the code thanks to all your help. It now works correctly reading one character at a time. I have also made it so that there is not more than 1 whitespace between words (I figured out how to do that by searching the forums ).

    Next I plan to deal with the capitalization I wanted to do. I have deleted what I was doing before because it made little sense. I have instead decided to try to capitalize the first letter of every word. This will require me to compare the previous to the last character which is what I seem to need more practice on.

    Currently my code is:
    Code:
    #include<iostream>
    #include <cctype>
    
    using namespace std;
    
    int main()
    {
    	char current;
            char previous;  
    		
            //Repeat on return input
    	
    	do	
    	{
    		//Read input data 1 character at at time 
    
    		count = 0;
    	      
    		while ((current = cin.get()) != '.')
    		{
    			
    			if(current != previous)
    			{
    				cout << current;
    				previous = current;
    			}
    		
    			//Multiple spaces should be separated by only a single blank
    				
    			while((current = cin.get()) != '\n')
    			{
    				if(!isspace(current) && isspace(previous))
    				{
    					putchar(' ');
    				}
    
    				if(!isspace(current)) 
    				{
    					putchar(current);
    				}
    		
    				previous = current;
    			}
    				
    			cout << endl << endl;
    		}
    		
    	}	while(current == '\n');
    	
    	return 0;
    }
    I'm sure I'll have more questions at some point soon but for now thanks a bunch.
    Last edited by Zalbik; 10-30-2002 at 09:56 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 11:21 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  4. OUTPUT TO PRINTER using c++ ???
    By howhy in forum C++ Programming
    Replies: 12
    Last Post: 09-01-2005, 10:36 PM
  5. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 05:13 AM

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