PassMak3r v2

This is a discussion on PassMak3r v2 within the C++ Programming forums, part of the General Programming Boards category; In my second version of my program that will help create a secure password for the user I am attempting ...

  1. #1
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280

    PassMak3r v2

    In my second version of my program that will help create a secure password for the user I am attempting to have it create an acronym from a phrase that the user types in. One problem that I am having is whenever I display the acronym it shows the acronym but then it shows some unprintable characters and then the phrase. What did I do wrong? Here is the quick code I wrote to do it so I haven't really worried about neatness for now.
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int c=1;
    	char again, phrase[40], acronym[40];
    	do
    	{
    		cout << "Enter phrase (40 character limit): ";
    		cin.getline(phrase, 40, '\n');
    		for (int i=0; i < 40; i++)
    		{
    			if (i == 0)      //if it is the first letter of the phrase
    				acronym[0] = phrase[0];
    			else if (phrase[i] == ' ') //if it is inbetween words use the first letter in the following word
    			{
    				acronym[c] = phrase[i+1];
    				c++;
    			}
    		}
    		cout << "Acronym is " << acronym << "\nAgain?: ";
    		cin >> again;
    		cin.ignore(80, '\n');   //just incase they typed in 'yes', 'Yes', 'YES'...
    	} while (again == 'y' || again == 'Y');
    	return 0;
    }

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Here's a hint:

    How does cout know when it reaches the end of a string?

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    After your for loop, put
    Code:
    acronym[ c ] = '\0';
    cout always outputs until it hits a '\0' character.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Thanks alot, that fixed that problem. Next one is that if the character enters 40 characters the program crashes, I added a cin.ignore after I call getline to make sure no extra characters carry over. But it still crashes. If the user types in any less than 40 characters they need to hit enter twice before they can see the acronym. Why are these happening?
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int c=1;
    	char again, phrase[40], acronym[40];
    	do
    	{
    		cout << "Enter phrase (40 character limit): ";
    		cin.getline(phrase, 40, '\n');
    		cin.ignore(80, '\n');
    		for (int i=0; i < 40; i++)
    		{
    			if (i == 0)
    				acronym[0] = phrase[0];
    			else if (phrase[i] == ' ')
    			{
    				acronym[c] = phrase[i+1];
    				c++;
    			}
    		}
    		acronym[c] = '\0';
    		cout << "Acronym is " << acronym << "\nAgain?: ";
    		cin >> again;
    		cin.ignore(80, '\n');
    	} while (again == 'y' || again == 'Y');
    	return 0;
    }

  5. #5
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    You could try changing the cout near the end to
    Code:
    cout << "Acronym is " << acronym<<"\n";
    cout << "Again?:";
    It might work - I'm not sure, just try it.
    Away.

  6. #6
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by Munkey01
    [B]Thanks alot, that fixed that problem. Next one is that if the character enters 40 characters the program crashes, I added a cin.ignore after I call getline to make sure no extra characters carry over. But it still crashes. If the user types in any less than 40 characters they need to hit enter twice before they can see the acronym. Why are these happening?

    1) Some crashing is (nearly) unrelated to having more than 40 characters. Here's a hint: It never crashes on the first time through the loop. Here's a second hint, some actual output from your program:

    Code:
    Enter phrase (40 character limit): 1 2 3
    
    Acronym is 123
    Again?: y
    Enter phrase (40 character limit): 4 5 6
    
    Acronym is 42356
    Again?: y
    Enter phrase (40 character limit): 7 8 9
    
    Acronym is 7235689
    Again?:
    That should be all the clues you'd need to find the real cause of the problem.

    2) There IS a problem with reading more than 40 characters, and you can tell if it happened. If readline() can't hit the newline character before it fills the buffer, you get the first 39 characters back (it null terminates the 40-char array), but it also puts cin in the error state -- you have to clear the error flags, purge the input (which you know has a \n terminator, so use ignore with a really big number and that delim). You only call ignore() if there are characters that need to be ignored.

    This snippet solves the cin error problem:

    Code:
    if (!cin){
       cin.clear();
       cin.ignore(0x7FFFFFFFL,'\n');
    }
    If there was an error, it resets the error and wipes the maximum possible number of characters from the input stream (0x7FFFFFFF = largest positive integer).
    Last edited by Cat; 05-24-2003 at 04:47 PM.

  7. #7
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Thank you so much for your help. I added some code to make every array element null before getting the phrase and producing the acronym, so that fixed the acronym problem you pointed out. And the cin error check of your's I added and it is working fine. I still have a problem with it though, but it isn't that bad of a problem, I will analyze it when I get home from the movies. The problem doesn't cause an infinate loop or anything like that, it just causes the acronym to be only the first letter of the phrase, it has to have something with entering more than 40 characters since it only happened when I entered more than 40, but I have to go. If you see the error point it out to me so I can fix it, thanks again for your help.

    I will probley add a message in the error check to warn the user that an error has occured and to retry it, and maybe something about making the acronym shorter. Is there a way to display what error has occured?
    Last edited by Munkey01; 05-24-2003 at 06:55 PM.

  8. #8
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    The BIG problem that you had before was the value of 'c' the second (and later) times through the loop. 'c' keeps getting bigger and bigger because it is never reset on future loops. It should be 1 at the beginning of EVERY loop, not just the first.

    That IS why it's only printing the first letter on every try except the first (it shouldn't matter how long the phrase is).

  9. #9
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    I see what you are saying. Yep, that was a big problem I didn't see. Thanks. I am just not good at error checking and debuging.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector vs. array V2.
    By matsp in forum C++ Programming
    Replies: 38
    Last Post: 11-26-2008, 01:05 AM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 04:33 PM
  3. Problems with growth program.
    By teck in forum Windows Programming
    Replies: 4
    Last Post: 11-14-2007, 04:28 PM
  4. operator problems
    By hdragon in forum C++ Programming
    Replies: 10
    Last Post: 12-30-2005, 11:47 PM

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