Thread: Passing the Spammer

  1. #1
    Registered User Program_Me's Avatar
    Join Date
    May 2012
    Posts
    9

    Passing the Spammer

    hey guys, i have been trying to get this to work for a while now but i cant seem to puzzle it together. Could you maybe give suggestions on what to do.


    Basically what i am trying to do is to write a small program that prints out a bit of text say around 50 times and then asks you for a password, if you get it wrong then it prints out that same bit of text another 50 times. But if you get it right then it will print out "correct", and then ends.


    all help, tips. and suggestions are greatly appreciated

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        int pass = 567;
        int guess;
    
    
        printf("Please enter the password: ");
        scanf("%d", &guess);
    
        while(guess != pass)
        {
             printf("Incorrect")  // bit of text that get displayed 50 times
        }
    
    
        if(guess != pass)
        {
            printf("incorrect");
        }
        else
        {
            printf("correct");
        }
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like you should start by printing out the text. The password challenge comes after that, according to what you stated.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Program_Me View Post
    Code:
    printf("Please enter the password: ");
    scanf("%d", &guess);
    
    while(guess != pass)
    {
        printf("Incorrect")  // bit of text that get displayed 50 times
    }
    Because you never change neither guess nor pass inside the loop, you are stuck in an infinite loop when the user doesn't enter the correct password the first time.
    For doing something excatly 50 times, a for-loop is your friend.

    Bye, Andreas

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    1
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	int numTimes = 50;
    	int count;
    	char *password = "password";
    	char guess[80];
    	
    	do
    	{
    		for (count = 0; count < numTimes; count++)
    			puts("Incorrect");
    		puts("Enter password: ");
    		gets(guess);
    	}
    	while (strcmp(guess, password) != 0);
    	
    	puts("Correct.");
    	return 0;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Because using gets() is always a big step towards "security"
    SourceForge.net: Gets - cpwiki
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User Program_Me's Avatar
    Join Date
    May 2012
    Posts
    9
    Thanks for that AndiPersti, i will try that. i have only been doing C for a little while, so if the methods that i use at the moment are to basic to perform the set tasks, it is because i havent learnt of the more difficult methods yet. i will update on weather the for loop worked or not. thanks guys

  7. #7
    Registered User Program_Me's Avatar
    Join Date
    May 2012
    Posts
    9
    Thank you for your bit of code MasterofPuppets, your code is exactly what im looking for, but the bit that i dont understand is the " strcmp " part, what does that bit of text mean. thanks

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another jailed spammer
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-19-2005, 02:46 AM