1. ## [Rookie] Defining Arrays

Hey everyone!

I'm new to these forums and fairly new to the C language. I'm three weeks into my first coding class at school and things are starting to pick up intensity. So, on to my question!

I'd like to prompt the user for a single 5-8 digit password and analyze it by redefining it as an array. I figured that an array would be easier to work with since I can analyze each digit specifically. A colleague of mine threw around the idea of using a remainder-loop sort of process to pick apart the code in a FIFO stack style.

Any help is appreciated!

EDIT: The passcode is entirely numerical, by the way

2. It's common to use arrays to hold data that is sequential in the order it is given or needs to be processed. You can work with a char array as if it were any kind of stack you want, just be using the indices to analyze the char you want, in the order you want.

You can work with numbers either as int's, floats, doubles, or simply as digits (chars).

3. I have a general idea of how an array works and how to target a specific element, but I'm trying to do something like this (not the actual syntax):

Code:
```scanf("%8d", &passcode);

// change (int passcode) into an int array or char array

if (passarray[0] = 0)
{
}```
It's just changing the integer into an array for analysis that I'm struggling with.

4. You're supposed to struggle!

You need to have a firm grasp on using a loop with an index variable to reference each sequential element of the array.
here if you need help.

Then, play around with div and mod and see if you can get individual digits out of the number. Note that there are at least 2 approaches to this, one of which will give you the reverse of the original (well, depending on how you handle it).
Hint: You will need to modify the original number throughout the process.

5. Code:
```scanf("%8d", &passcode);

// change (int passcode) into an int array or char array

if (passarray[0] = 0)
{
}```
1) You can't access a single digit of an integer like an element of an array.
2) = is the assignment operator, == the comparison operator

What kind of analysis do you want to do?

Generally I would regard a password as a string instead of as a number. This would make it easy to read it into a char array without needing to mess around with divisions and remainders.

Bye, Andreas

6. I know there were syntax errors, it was a very rough description of what I want to do.

Now that you mention string, I feel like an idiot though :P So how can I transform a string into an array? Or can a string be treated similarly to an array? Every time I've used strings, it's been for simple things, ie: "What is your name?" type of questions. Not to mention that I haven't fully studied pointers yet.

And the analysis I want to do is several points of inspection. The password must be 5-8 digits (done), must not start with a zero (will handle with array), last three digits must yield a prime number (will handle with array), and last five digits must form a palindrome (also handled with array comparisons).

7. Strings are char arrays:
Code:
```char myPasswordString[10];

fflush(stdout); //print the enter prompt, & leave the cursor where it is

8. Are you not allowed to use fgets()? If so I would avoid scanf() and read from STDIN with the former, then do some error checking on it to make sure it conforms to your assigment requirements.

9. Originally Posted by hex_dump
Are you not allowed to use fgets()? If so I would avoid scanf() and read from STDIN with the former, then do some error checking on it to make sure it conforms to your assigment requirements.
All of our assignments so far have been much simpler: just if...else and while or for loops. That being said, I'm trying not to get into too much too fast.

Quick question, how can I compare a string to an integer without the compiler giving me fits? I need to set -1 as a sentinel value to quit out of the password-testing loop.

10. Why not compare the char '-1' with the sentinel value of a char '-1'? Chars can be a positive number up to 127, and a negative numbers down to -128.

Why not compare the char '-1' with the sentinel value of a char '-1'? Chars can be a positive number up to 127, and a negative numbers down to -128.
Heh, char might be unsigned, though in that case it would still work, unless you actually need the value of (unsigned char)-1.

12. Igrad, the solution depends on whether or not you need to be able to read each individual digit as it entered, or once they press enter, analyze each character on the line they submitted. You can store them both in the same way, but how you get them and process them depends on that distinction.

13. Originally Posted by Darphant
Igrad, the solution depends on whether or not you need to be able to read each individual digit as it entered, or once they press enter, analyze each character on the line they submitted. You can store them both in the same way, but how you get them and process them depends on that distinction.
After the user hits enter, I'd like to be able to analyze each digit of the entered number individually.

14. Well, the idea of reading it as a string into an array of char has already been suggested. Do you understand how to work with that idea?

15. Roughly, yes. I probably just need to fool around with char and string concepts until I have a firm grasp on it. I'm just bumfuzzled as to how to initialize all these new char values without making my code 20 lines longer. Is there a cleaner, more efficient way to do it? (my current code's important features are below):

Code:
```#include <stdio.h>
#include <math.h>

int main(void)

{

char passcode[ 8 ] = {0};
char negOne = -1;
char zero = 0;
char maxlim = 99999999;
char minlim = 10000;
int i = 0;

int ProgQuit= 0;

printf("\n\nWelcome, Agent.\nThis program will analyze the password you enter to see if it meets agency standard. Your password must meet the following qualifications.\n\n\t1. must be all numbers\n\t2. must not start with a zero\n\t3. must end in a three-digit prime number\n\t4. last 5 digits must be a palindrome\n\t5. must be 5 to 8 digits long\n\nYou may enter -1 when you are finished");

do
{
/*Reset values to 0 for loop*/
i = 0;

/*Start passcode analyzing loop*/
scanf("%s", passcode);

/*Set sentinel value to -1*/
if(passcode == negOne)
{
printf("\n\nExiting...\n\n");
ProgQuit = -1;
}

// Other "else if" statements

} while(ProgQuit != -1);

return 0;
}```