Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making y[i] a modifiable variable in C

Tags:

c

I am building a program that randomly generates a password using the ascii tabe of values and can only contain one of each char. it generates a password that is 8 char long.

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define SIZE 10

char Charactor(char x);
void Check(char* y);

int main()
{
char string[SIZE];//defines varriables
char* point;
point=&string[SIZE];
srand(time(NULL));
for (int i=0;i<SIZE-1;i++)//empties out the string
{
    string[i]='\0';
}
for (int i=0;i<SIZE-2;i++)//randomizes a char for each space in the string
{
    string[i]=Charactor(*point);
}
Check(point);/checks the string for duplicated values
printf("%s\n",string);//prints string on screen
}

char Charactor(char x)
{
int rnd=0;
rnd=rand()%2;//chooses char or number using ascii
if (rnd==0)
{
    rnd=rand()%10+48;
}
else
{
    rnd=rand()%26+65;
}
x=(char)rnd;
return x;
}

void Check(char* y)
{
int run=0;
for (int i=0; i<SIZE-2;i++)
{
    for (int x=0; x<SIZE-2; x++) 
    {
        if (y[i]==y[x] && run=0)
        {
            run++;
            continue;
        }
        else
        {
            y[i]='\0';
            y[i]=Charactor(*y);
        }
    }
}
return;
}

with those changes the code is running now I just have to figure out how to change the correct value so I dont have any duplication.

like image 859
Footsure Avatar asked Sep 06 '13 17:09

Footsure


People also ask

How do you create a variable type in C++?

To create a variable, you must specify the type and assign it a value: Syntax. type variable = value; Where type is one of C++ types (such as int), and variable is the name of the variable (such as x or myName). The equal sign is used to assign values to the variable.

What are variables in C++?

Variables are containers for storing data values. In C++, there are different types of variables (defined with different keywords), for example: int - stores integers (whole numbers), without decimals, such as 123 or -123 double - stores floating point numbers, with decimals, such as 19.99 or -19.99

How do you modify a const variable?

If you need to modify a const, copy it to a non-const variable and then work with it. It's const for a reason. Trying to "sneak" around a const can cause serious runtime issues. i.e. the optimizer has likely used the value inline, etc. If you need to modify a const (not constant) object, don't define it as const in the first place.

What is the syntax for variables declaration in C?

The syntax for variables declaration is as follows. data_type: Indicates types of data it stores. Data types can be int, float, char, double, long int, etc. variable_name: Indicates the name of the variable. It can be anything other than the keyword. For example, 1, int is a data type, and a is a variable name.


2 Answers

Fix:

char* point =&string[0]; //Make it to point to first element

Since your Charactor(*point); is really not doing anything based on *point and later you use Check(point); probably to start a scan from start of string.

And

 if (y[i]==y[x] && run==0)
                      ^^Use Equality check

You cannot modify a boolean outcome of y[i]==y[x] && run as zero.

Note : However if (y[i]==y[x] && (run=0) ) wouldn't have thrown this error.

like image 144
P0W Avatar answered Sep 27 '22 19:09

P0W


Your error seems to be that you are mistakenly setting run=0 in

if (y[i]==y[x] && run=0)

This is the part that most likely confuses your compiler. Doesn't have to do anything with Y.

Fix to:

if (y[i]==y[x] && run==0)
like image 28
Pudpuduk Avatar answered Sep 27 '22 21:09

Pudpuduk