9 Replies - 2056 Views - Last Post: 12 February 2014 - 06:21 PM Rate Topic: -----

#1 Frost_Hammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-February 14

Password entry

Posted 11 February 2014 - 03:23 PM

Hey guys so what I am trying to do is have program that has the user enter in 3 codes
1. User identification. FORMAT: xxx-yyyy where xxx can be any letter combination in
lower case and yyyy is any number, 0-9999.
2. User password. FORMAT: 4 to 12 digits long, must contain a mixture of numbers and
letters but no spaces
3. Access code. FORMAT: aaa-bbb-ccc where aaa, bbb, and ccc can be any alphanumeric
characters defined in the program

I have part or function 1 working I just need to loop it so if the user enters in an incorrect input they have to resubmit/terminate.

TL:DR the issue I am having now is whenever I execute my code it gets to the second user access code input and crashes when I put in a valid string length. If it is not of size I get the error code for wrong string size but it loops for infinity. Any and all help/ input is greatly appreciated.

/*
    Sam Sloane C0353667


A program that will prompt the user to enter three items of data (text strings) in the specific
formats outlined below:

1. User identification. FORMAT: xxx-yyyy where xxx can be any letter combination in
lower case and yyyy is any number, 0-9999.
2. User password. FORMAT: 4 to 12 digits long, must contain a mixture of numbers and
letters but no spaces
3. Access code. FORMAT: aaa-bbb-ccc where aaa, bbb, and ccc can be any alphanumeric
characters defined in the program

Have tons of printf statements so the user does not become lost in the command prompt
use strings of characters to hold the values

Sudo Code
describe program
have user enter in Id
check for error
if error re-enter value
have user enter password
check for error
if error re-enter value
have user enter access code
check for error
if error re-enter value
if all is good printf statement
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

void parse_string(char *);
void password_string(char *);

// main program starts here
int
main(void)
{

   char str[20];
   char str2[20];
   char str3[20];

	//clrscr();	// clear the screen
	system("CLS");

	// main program loop
	do
	{
		//clrscr();	// clear the screen
		system("CLS");

		// tell user what the program does and/or prompt for input
        printf("-----------------------------------------------------------------------\n");
        printf("This program has the user enter in data in a predetermined format\n");
        printf("The program then makes sure the data is implemented in the correct format\n");
        printf("-----------------------------------------------------------------------\n\n\n\n");// declare variables

		printf("Enter a string in the following format\n\n   xxx-yyyy\n\n");
		printf("Where: xxx can be a letters followed by a dash '-'\n");
		printf("Where: yyyy can be any digits 0-9\n\n");

		printf("OR...Enter 'q' to exit program\n\n");

        printf("ENTER STRING:    ");

		// scan the string
		scanf("%s", str);

        // check exit condition
		if (str[0] == 'q')
      	   break;

        // check the format of the string
		parse_string(str);

		// continue or exit?
		printf("\n\nPress any key\n\n\n");
		getch();
            printf("-----------------------------------------------------------------------\n");
            printf("Enter a 12 character password\n\n");
            printf("Can be any alphanumeric combination \n");


            printf("OR...Enter 'q' to exit program\n\n");

            printf("ENTER STRING:    ");
                // scan the string
            scanf("%s", str2);

            // check exit condition
            if (str2[0] == 'q')
               break;

            // check the format of the string
            password_string(str2);

		// continue or exit?
		printf("\n\nPress any key\n");
		getch();

	} while (1);

	//clrscr();
	system("CLS");
	printf("\nProgram Terminated\n");
}
// end of main program


// string parsing functions
void
parse_string(char *str)
{
	// assume no error at start
	int str_error = 0;
	int n;
    int len;

	// check length
	if ((len = strlen(str)) != 8)
	{
		printf("Error: String wrong length %d\n", len);
		str_error = 1;    // flag error
	}

    for (n = 0; n < 3; n++)
	{

		if (str[n] < 'a' || str[n] > 'z')
		{
            printf("Error: Bad letter at position %d\n", n+1);
            str_error = 1;   // flag error

		}

    }


	// check delimiter
	if (str[3] != '-')
	{
		printf("Error: Invalid delimiter\n");
		str_error = 1;   // flag error
	}

	// scan last 3 characters
	for (n = 4; n < 8; n++)
	{
		if (str[n] < '0' || str[n] > '9')
		{
			printf("Error: Bad number at position %d\n", n+1);
			str_error = 1;   // flag error
		}
	}
	if (!str_error)
		printf("String is valid format\n");

	}


	/////////////////////////////////////////////////////////
    void password_string(char *str2)
    {


	// scan the string

		// main program loop
        do
        {

            // assume no error at start
            int str_error = 0;
            int n;
            int len;

            // check length
            if ((len = strlen(str2)) > 12 || < 4)
            {
                printf("Error: String  is the wrong length %d\n", len);
                str_error = 1;    // flag error
            }
                // check exit condition
                if (str2[0] == 'q')
                   break;

                // check the format of the string
                password_string(str2);

                // continue or exit?
                printf("\n\nPress any key\n");
                getch();


        }




        while (1);
        {


        //clrscr();
        system("CLS");
        printf("\nProgram Terminated\n");
        }
    // end of main program

    }



Is This A Good Question/Topic? 0
  • +

Replies To: Password entry

#2 Black_Epiphany  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 64
  • Joined: 02-February 14

Re: Password entry

Posted 11 February 2014 - 04:16 PM

if ((len = strlen(str)) != 8)
    {
        printf("Error: String wrong length %d\n", len);
        str_error = 1;    // flag error
    }
 



I think this needs to be:

if ((strlen(str)) != 8)
    {
        printf("Error: String wrong length %d\n", len);
        str_error = 1;    // flag error
    }
 


Was This Post Helpful? 1
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4099
  • View blog
  • Posts: 12,695
  • Joined: 25-December 09

Re: Password entry

Posted 11 February 2014 - 05:14 PM

Quote

the issue I am having now is whenever I execute my code it gets to the second user access code input and crashes when I put in a valid string length.

I don't see how the program "gets anywhere" since it doesn't compile for me. Look at the following error report from my compiler.

Quote

||=== c_homework, Debug ===|
main.c||In function main:|
main.c|46|warning: unused variable str3 [-Wunused-variable]|
main.c||In function password_string:|
main.c|183|error: expected expression before < token|
main.c|179|warning: unused variable n [-Wunused-variable]|
main.c|178|warning: variable str_error set but not used [-Wunused-but-set-variable]|
||=== Build finished: 1 errors, 3 warnings (0 minutes, 0 seconds) ===|


Look at the following snippet:
           if ((len = strlen(str2)) > 12 || < 4)


In C/C++ you must have both sides of the equation in if statements.
len = strlen(str2);
if( len > 12 || len < 4)


Jim
Was This Post Helpful? 1
  • +
  • -

#4 Frost_Hammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-February 14

Re: Password entry

Posted 11 February 2014 - 08:34 PM

View Postjimblumberg, on 11 February 2014 - 05:14 PM, said:

Quote

the issue I am having now is whenever I execute my code it gets to the second user access code input and crashes when I put in a valid string length.

I don't see how the program "gets anywhere" since it doesn't compile for me. Look at the following error report from my compiler.

Quote

||=== c_homework, Debug ===|
main.c||In function main:|
main.c|46|warning: unused variable str3 [-Wunused-variable]|
main.c||In function password_string:|
main.c|183|error: expected expression before < token|
main.c|179|warning: unused variable n [-Wunused-variable]|
main.c|178|warning: variable str_error set but not used [-Wunused-but-set-variable]|
||=== Build finished: 1 errors, 3 warnings (0 minutes, 0 seconds) ===|


Look at the following snippet:
           if ((len = strlen(str2)) > 12 || < 4)


In C/C++ you must have both sides of the equation in if statements.
len = strlen(str2);
if( len > 12 || len < 4)


Jim



Oh yeaah I fiddle around with code to figure that one out. Thanks for the hand. Will post updated code when I get bootcamp up Macs suck lol.

View PostFrost_Hammer, on 11 February 2014 - 03:23 PM, said:

Hey guys so what I am trying to do is have program that has the user enter in 3 codes
1. User identification. FORMAT: xxx-yyyy where xxx can be any letter combination in
lower case and yyyy is any number, 0-9999.
2. User password. FORMAT: 4 to 12 digits long, must contain a mixture of numbers and
letters but no spaces
3. Access code. FORMAT: aaa-bbb-ccc where aaa, bbb, and ccc can be any alphanumeric
characters defined in the program

I have part or function 1 working I just need to loop it so if the user enters in an incorrect input they have to resubmit/terminate.

TL:DR the issue I am having now is whenever I execute my code it gets to the second user access code input and crashes when I put in a valid string length. If it is not of size I get the error code for wrong string size but it loops for infinity. Any and all help/ input is greatly appreciated.

/*
    Sam Sloane C0353667


A program that will prompt the user to enter three items of data (text strings) in the specific
formats outlined below:

1. User identification. FORMAT: xxx-yyyy where xxx can be any letter combination in
lower case and yyyy is any number, 0-9999.
2. User password. FORMAT: 4 to 12 digits long, must contain a mixture of numbers and
letters but no spaces
3. Access code. FORMAT: aaa-bbb-ccc where aaa, bbb, and ccc can be any alphanumeric
characters defined in the program

Have tons of printf statements so the user does not become lost in the command prompt
use strings of characters to hold the values

Sudo Code
describe program
have user enter in Id
check for error
if error re-enter value
have user enter password
check for error
if error re-enter value
have user enter access code
check for error
if error re-enter value
if all is good printf statement
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

void parse_string(char *);
void password_string(char *);

// main program starts here
int
main(void)
{

   char str[20];
   char str2[20];
   char str3[20];

	//clrscr();	// clear the screen
	system("CLS");

	// main program loop
	do
	{
		//clrscr();	// clear the screen
		system("CLS");

		// tell user what the program does and/or prompt for input
        printf("-----------------------------------------------------------------------\n");
        printf("This program has the user enter in data in a predetermined format\n");
        printf("The program then makes sure the data is implemented in the correct format\n");
        printf("-----------------------------------------------------------------------\n\n\n\n");// declare variables

		printf("Enter a string in the following format\n\n   xxx-yyyy\n\n");
		printf("Where: xxx can be a letters followed by a dash '-'\n");
		printf("Where: yyyy can be any digits 0-9\n\n");

		printf("OR...Enter 'q' to exit program\n\n");

        printf("ENTER STRING:    ");

		// scan the string
		scanf("%s", str);

        // check exit condition
		if (str[0] == 'q')
      	   break;

        // check the format of the string
		parse_string(str);

		// continue or exit?
		printf("\n\nPress any key\n\n\n");
		getch();
            printf("-----------------------------------------------------------------------\n");
            printf("Enter a 12 character password\n\n");
            printf("Can be any alphanumeric combination \n");


            printf("OR...Enter 'q' to exit program\n\n");

            printf("ENTER STRING:    ");
                // scan the string
            scanf("%s", str2);

            // check exit condition
            if (str2[0] == 'q')
               break;

            // check the format of the string
            password_string(str2);

		// continue or exit?
		printf("\n\nPress any key\n");
		getch();

	} while (1);

	//clrscr();
	system("CLS");
	printf("\nProgram Terminated\n");
}
// end of main program


// string parsing functions
void
parse_string(char *str)
{
	// assume no error at start
	int str_error = 0;
	int n;
    int len;

	// check length
	if ((len = strlen(str)) != 8)
	{
		printf("Error: String wrong length %d\n", len);
		str_error = 1;    // flag error
	}

    for (n = 0; n < 3; n++)
	{

		if (str[n] < 'a' || str[n] > 'z')
		{
            printf("Error: Bad letter at position %d\n", n+1);
            str_error = 1;   // flag error

		}

    }


	// check delimiter
	if (str[3] != '-')
	{
		printf("Error: Invalid delimiter\n");
		str_error = 1;   // flag error
	}

	// scan last 3 characters
	for (n = 4; n < 8; n++)
	{
		if (str[n] < '0' || str[n] > '9')
		{
			printf("Error: Bad number at position %d\n", n+1);
			str_error = 1;   // flag error
		}
	}
	if (!str_error)
		printf("String is valid format\n");

	}


	/////////////////////////////////////////////////////////
    void password_string(char *str2)
    {


	// scan the string

		// main program loop
        do
        {

            // assume no error at start
            int str_error = 0;
            int n;
            int len;

            // check length
            if ((len = strlen(str2)) > 12 || < 4)
            {
                printf("Error: String  is the wrong length %d\n", len);
                str_error = 1;    // flag error
            }
                // check exit condition
                if (str2[0] == 'q')
                   break;

                // check the format of the string
                password_string(str2);

                // continue or exit?
                printf("\n\nPress any key\n");
                getch();


        }




        while (1);
        {


        //clrscr();
        system("CLS");
        printf("\nProgram Terminated\n");
        }
    // end of main program

    }




How are those two segments any different?

For the second post I don't notice a difference in code
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4099
  • View blog
  • Posts: 12,695
  • Joined: 25-December 09

Re: Password entry

Posted 11 February 2014 - 09:17 PM

Quote

How are those two segments any different?

For the second post I don't notice a difference in code

If you wrote the code you should know what the differences are. Did you write this code?


Jim
Was This Post Helpful? 0
  • +
  • -

#6 Frost_Hammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-February 14

Re: Password entry

Posted 12 February 2014 - 12:26 PM

View Postjimblumberg, on 11 February 2014 - 09:17 PM, said:

Quote

How are those two segments any different?

For the second post I don't notice a difference in code

If you wrote the code you should know what the differences are. Did you write this code?


Jim


Yes I did, my name is student number is in the comment section of the code I am Sam Sloane. Anyway I meant to quote the second guy but accidentally hit my comment instead. Sorry for the confusion.
Was This Post Helpful? 0
  • +
  • -

#7 Frost_Hammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-February 14

Re: Password entry

Posted 12 February 2014 - 12:34 PM

So right now my code compiles and runs but crashes for the second input if the parameters are met. Yet if the condition is false that statement or condition works. Not sure how to fix this. I do get a error in CMD which is Process Returned -1073741571 <0xC00000FD> execution time 13.292s. Not sure if that helps at all but that is what I am getting. Also with checking if the input characters are lower case is there a way to do this so it can be upper or lower
 if (str[n] < 'a' || str[n] > 'z')
I have tried, with no luck.
 if ((str[n] < 'a' || str[n] > 'z') || if (str[n] < 'A' || str[n] > 'Z'))
Anyway my updated code is as follows
 /*
    Sam Sloane C0353667


A program that will prompt the user to enter three items of data (text strings) in the specific
formats outlined below:

1. User identification. FORMAT: xxx-yyyy where xxx can be any letter combination in
lower case and yyyy is any number, 0-9999.
2. User password. FORMAT: 4 to 12 digits long, must contain a mixture of numbers and
letters but no spaces
3. Access code. FORMAT: aaa-bbb-ccc where aaa, bbb, and ccc can be any alphanumeric
characters defined in the program

Have tons of printf statements so the user does not become lost in the command prompt
use strings of characters to hold the values

Sudo Code
describe program
have user enter in Id
check for error
if error re-enter value
have user enter password
check for error
if error re-enter value
have user enter access code
check for error
if error re-enter value
if all is good printf statement
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

void parse_string(char *);
void password_string(char *);

// main program starts here
int
main(void)
{

   char str[20];
   char str2[20];
   char str3[20];

	//clrscr();	// clear the screen
	system("CLS");

	// main program loop
	do
	{
		//clrscr();	// clear the screen
		system("CLS");

		// tell user what the program does and/or prompt for input
        printf("-----------------------------------------------------------------------\n");
        printf("This program has the user enter in data in a predetermined format\n");
        printf("The program then makes sure the data is implemented in the correct format\n");
        printf("-----------------------------------------------------------------------\n\n\n\n");// declare variables

		printf("Enter a string in the following format\n\n   xxx-yyyy\n\n");
		printf("Where: xxx can be a letters followed by a dash '-'\n");
		printf("Where: yyyy can be any digits 0-9\n\n");

		printf("OR...Enter 'q' to exit program\n\n");

        printf("ENTER STRING:    ");

		// scan the string
		scanf("%s", str);

        // check exit condition
		if (str[0] == 'q')
      	   break;

        // check the format of the string
		parse_string(str);

		// continue or exit?
		printf("\n\nPress any key\n\n\n");
		getch();
            printf("-----------------------------------------------------------------------\n");
            printf("Enter a 12 character password\n\n");
            printf("Can be any alphanumeric combination \n");


            printf("OR...Enter 'q' to exit program\n\n");

            printf("ENTER STRING:    ");
                // scan the string
            scanf("%s", str2);

            // check exit condition
            if (str2[0] == 'q')
               break;

            // check the format of the string
            password_string(str2);

		// continue or exit?
		printf("\n\nPress any key\n");
		getch();

	} while (1);

	//clrscr();
	system("CLS");
	printf("\nProgram Terminated\n");
}
// end of main program


// string parsing functions
void
parse_string(char *str)
{
	// assume no error at start
	int str_error = 0;
	int n;
    int len;

	// check length
	if ((len = strlen(str)) != 8)
	{
		printf("Error: String wrong length %d\n", len);
		str_error = 1;    // flag error
	}

    for (n = 0; n < 3; n++)
	{

		if (str[n] < 'a' || str[n] > 'z')
		{
            printf("Error: Bad letter at position %d\n", n+1);
            str_error = 1;   // flag error

		}

    }


	// check delimiter
	if (str[3] != '-')
	{
		printf("Error: Invalid delimiter\n");
		str_error = 1;   // flag error
	}

	// scan last 3 characters
	for (n = 4; n < 8; n++)
	{
		if (str[n] < '0' || str[n] > '9')
		{
			printf("Error: Bad number at position %d\n", n+1);
			str_error = 1;   // flag error
		}
	}
	if (!str_error)
		printf("String is valid format\n");

	}


	/////////////////////////////////////////////////////////
    void password_string(char *str2)
    {

        // main program loop
        do
        {

            // assume no error at start
            int str_error = 0;
            //int n;
            int len;

            // check length
            if ((len = strlen(str2)) >12 || (len = strlen(str2)) <4)

            {
                printf("Error: String  is the wrong length %d\n", len);
                str_error = 1;    // flag error
            }
                // check exit condition
                if (str2[0] == 'q')
                   break;

                // check the format of the string
                password_string(str2);

                // continue or exit?
                printf("\n\nPress any key\n");
                getch();

                 if (!str_error)
		printf("String is valid format\n");


        }

        while (1);
        {


        //clrscr();
        system("CLS");
        printf("\nProgram Terminated\n");
        }


    }


Was This Post Helpful? 0
  • +
  • -

#8 Frost_Hammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-February 14

Re: Password entry

Posted 12 February 2014 - 12:49 PM

Alright code works flawlessly. I just need to figure out how to check that there are no spaces and that its a combination of letters and numbers. Then I just need a code in aaa-bbb-ccc where the chuncks can be anything and I just need two spaces. Here is my updated code
 /*
    Sam Sloane C0353667


A program that will prompt the user to enter three items of data (text strings) in the specific
formats outlined below:

1. User identification. FORMAT: xxx-yyyy where xxx can be any letter combination in
lower case and yyyy is any number, 0-9999.
2. User password. FORMAT: 4 to 12 digits long, must contain a mixture of numbers and
letters but no spaces
3. Access code. FORMAT: aaa-bbb-ccc where aaa, bbb, and ccc can be any alphanumeric
characters defined in the program

Have tons of printf statements so the user does not become lost in the command prompt
use strings of characters to hold the values

Sudo Code
describe program
have user enter in Id
check for error
if error re-enter value
have user enter password
check for error
if error re-enter value
have user enter access code
check for error
if error re-enter value
if all is good printf statement
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

void parse_string(char *);
void password_string(char *);

// main program starts here
int
main(void)
{

   char str[20];
   char str2[20];
   char str3[20];

	//clrscr();	// clear the screen
	system("CLS");

	// main program loop
	do
	{
		//clrscr();	// clear the screen
		system("CLS");

		// tell user what the program does and/or prompt for input
        printf("-----------------------------------------------------------------------\n");
        printf("This program has the user enter in data in a predetermined format\n");
        printf("The program then makes sure the data is implemented in the correct format\n");
        printf("-----------------------------------------------------------------------\n\n\n\n");// declare variables

		printf("Enter a string in the following format\n\n   xxx-yyyy\n\n");
		printf("Where: xxx can be a letters followed by a dash '-'\n");
		printf("Where: yyyy can be any digits 0-9\n\n");

		printf("OR...Enter 'q' to exit program\n\n");

        printf("ENTER STRING:    ");

		// scan the string
		scanf("%s", str);

        // check exit condition
		if (str[0] == 'q')
      	   break;

        // check the format of the string
		parse_string(str);

		// continue or exit?
		printf("\n\nPress any key\n\n\n");
		getch();
            printf("-----------------------------------------------------------------------\n");
            printf("Enter a 12 character password\n\n");
            printf("Can be any alphanumeric combination \n");


            printf("OR...Enter 'q' to exit program\n\n");

            printf("ENTER STRING:    ");
                // scan the string
            scanf("%s", str2);

            // check exit condition
            if (str2[0] == 'q')
               break;

            // check the format of the string
            password_string(str2);

		// continue or exit?
		printf("\n\nPress any key\n");
		getch();

	} while (1);

	//clrscr();
	system("CLS");
	printf("\nProgram Terminated\n");
}
// end of main program


// string parsing functions
void
parse_string(char *str)
{
	// assume no error at start
	int str_error = 0;
	int n;
    int len;

	// check length
	if ((len = strlen(str)) != 8)
	{
		printf("Error: String wrong length %d\n", len);
		str_error = 1;    // flag error
	}

    for (n = 0; n < 3; n++)
	{

		if (str[n] < 'a' || str[n] > 'z')
		{
            printf("Error: Bad letter at position %d\n", n+1);
            str_error = 1;   // flag error

		}

    }


	// check delimiter
	if (str[3] != '-')
	{
		printf("Error: Invalid delimiter\n");
		str_error = 1;   // flag error
	}

	// scan last 3 characters
	for (n = 4; n < 8; n++)
	{
		if (str[n] < '0' || str[n] > '9')
		{
			printf("Error: Bad number at position %d\n", n+1);
			str_error = 1;   // flag error
		}
	}
	if (!str_error)
		printf("String is valid format\n");

	}


	/////////////////////////////////////////////////////////
    void password_string(char *str2)
    {
        int str_error = 0;
        // main program loop
        do
        {
            //int n;
            int len =strlen(str2);

            // check length
            if (len >12 || len <4)

            {
                printf("Error: String  is the wrong length %d\n", len);
                str_error = 1;    // flag error
            }
                // check exit condition
                if (str2[0] == 'q')
                   break;

                // continue or exit?
                printf("\n\nPress any key\n");
                getch();

                 if (!str_error)
		printf("String is valid format\n");


        }while(str_error ==1);
    }


Was This Post Helpful? 0
  • +
  • -

#9 Frost_Hammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-February 14

Re: Password entry

Posted 12 February 2014 - 01:23 PM

Need the code to check if the user enters a space. If a space is entered the input is not a valid format. I keep getting errors saying that a space is found. Even when I enter the full 12 characters. I am thinking because I have a 20 character array the blank or unused spaces are counted as spaces. Pretty new to coding so I could be wrong.
 void password_string(char *str2)
    {
        int str_error = 0;
        // main program loop
        do
        {
            int n;
            int len =strlen(str2);

            // check length
            if (len >12 || len <4)

            {
                printf("Error: String  is the wrong length %d\n", len);
                str_error = 1;    // flag error
            }
                // check exit condition
                if (str2[0] == 'q')
                   break;

               for (n = 0; n <= len; n++)
                {
                        if (str2[n] = ' ')
                        {
                            printf("Error: Input contains a space\n");
                            str_error = 1;   // flag error
                            break;
                        }

                }

                // continue or exit?
                printf("\n\nPress any key\n");
                getch();

                 if (!str_error)
		printf("String is valid format\n");


        }while(str_error ==1);
    }



ah needed it to be equivalent to and not equals
 if (str2[n] == ' ')

Was This Post Helpful? 0
  • +
  • -

#10 Frost_Hammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-February 14

Re: Password entry

Posted 12 February 2014 - 06:21 PM

So all I need to do now is for the second input check that the user entered in a number and a character. Thought of using
 if ((isdigit(str2[n]))&& (isalpha(str2[n])))
                    {
                        printf("String is in a valid format\n\n\n");
                        str_error =0;

                    }


That doesn't seem to work here is the full code
/*
    Sam Sloane C0353667


A program that will prompt the user to enter three items of data (text strings) in the specific
formats outlined below:

1. User identification. FORMAT: xxx-yyyy where xxx can be any letter combination in
lower case and yyyy is any number, 0-9999.
2. User password. FORMAT: 4 to 12 digits long, must contain a mixture of numbers and
letters but no spaces
3. Access code. FORMAT: aaa-bbb-ccc where aaa, bbb, and ccc can be any alphanumeric
characters defined in the program

Have tons of printf statements so the user does not become lost in the command prompt
use strings of characters to hold the values

Sudo Code
describe program
have user enter in Id
check for error
if error re-enter value
have user enter password
check for error
if error re-enter value
have user enter access code
check for error
if error re-enter value
if all is good printf statement
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

void parse_string(char *);
void password_string(char *);
void access_string(char *);

// main program starts here
int
main(void)
{

   char str[20];
   char str2[20];
   char str3[20];

	//clrscr();	// clear the screen
	system("CLS");

	// main program loop
	do
	{
		//clrscr();	// clear the screen
		system("CLS");

		// tell user what the program does and/or prompt for input
        printf("-----------------------------------------------------------------------\n");
        printf("This program has the user enter in data in a predetermined format\n");
        printf("The program then makes sure the data is implemented in the correct format\n");
        printf("-----------------------------------------------------------------------\n\n\n\n");// declare variables

		printf("Enter a string in the following format\n\n   xxx-yyyy\n\n");
		printf("Where: xxx can be a letters followed by a dash '-'\n");
		printf("Where: yyyy can be any digits 0-9\n\n");

		printf("OR...Enter 'q' to exit program\n\n");

        printf("ENTER STRING:    ");

		// scan the string
		fflush(stdin);
		scanf("%[^\n]s", str);

        // check exit condition
		if (str[0] == 'q')
      	   break;

        // check the format of the string
		parse_string(str);

		// continue or exit?
		printf("\n\nPress any key\n\n\n");
		getch();
            printf("-----------------------------------------------------------------------\n");
            printf("Enter a 12 character password\n\n");
            printf("Can be any alphanumeric combination \n");


            printf("OR...Enter 'q' to exit program\n\n");

            printf("ENTER STRING:    ");
                // scan the string
            fflush(stdin);
            scanf("%[^\n]s", str2);

            // check exit condition
            if (str2[0] == 'q')
               break;

            // check the format of the string
            password_string(str2);

		// continue or exit?
		printf("\n\nPress any key\n");
		getch();

		fflush(stdin);
        scanf("%[^\n]s", str3);

        //access_string(str3);//check teh format of the string
        // continue or exit?
		printf("\n\nPress any key\n\n\n");
		getch();

	} while (1);

	//clrscr();
	system("CLS");
	printf("\nProgram Terminated\n");
}
// end of main program


// string parsing functions
void
parse_string(char *str)
{
	// assume no error at start
	int str_error = 0;
	int n;
    int len;

	// check length
	if ((len = strlen(str)) != 8)
	{
		printf("Error: String wrong length %d\n", len);
		str_error = 1;    // flag error
	}

    for (n = 0; n < 3; n++)
	{

		if (str[n] < 'a' || str[n] > 'z')
		{
            printf("Error: Bad letter at position %d\n", n+1);
            str_error = 1;   // flag error

		}

    }


	// check delimiter
	if (str[3] != '-')
	{
		printf("Error: Invalid delimiter\n");
		str_error = 1;   // flag error
	}

	// scan last 3 characters
	for (n = 4; n < 8; n++)
	{
		if (str[n] < '0' || str[n] > '9')
		{
			printf("Error: Bad number at position %d\n", n+1);
			str_error = 1;   // flag error
		}

	}
	if (!str_error)
		printf("String is valid format\n");

	}


	/////////////////////////////////////////////////////////
    void password_string(char *str2)
    {
        int str_error = 0;
        // main program loop
        do
        {
            int n;
            int len =strlen(str2);

            // check length
            if (len >12 || len <4)

            {
                printf("Error: String  is the wrong length %d\n", len);
                str_error = 1;    // flag error
            }
                // check exit condition
                if (str2[0] == 'q')
                   break;

               for (n = 0; n <= len; n++)
                {
                        if (str2[n] == ' ')
                        {
                            printf("Error: Input contains a space\n");
                            str_error = 1;   // flag error
                            break;
                        }
                    if ((isdigit(str2[n]))&& (isalpha(str2[n])))
                    {
                        printf("String is in a valid format\n\n\n");
                        str_error =0;

                    }


                }

                }while(str_error ==1);

                // continue or exit?
                printf("\n\nPress any key\n");
                getch();

                 if (!str_error)
		printf("String is valid format\n");


        }


/*void access_string(char *str3)
{
    // assume no error at start
	int str_error = 0;

	do{

        int n;
        int len;

        // check length
        if ((len = strlen(str3)) != 11)
        {
            printf("Error: String wrong length %d\n", len);
            str_error = 1;    // flag error
        }


*/


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1