Hi I am trying to teach myself C, and as an exercise I decided to try and make a program that generates random words which contains however many letters the user chooses. So far I have the user able to choose how ever many letters they want, and it will print out that many "characters. Here are my problems. First off, the random function isnt random; if you choose 5 characters it will always spit out the same thing. Next I am trying to have the comp choose the characters from an array of the 26 letters. The problem is is that the characters you see on the screen are not letters! Here is my script so far:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char letters [26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char randn(char letters) //random definition
{
return rand()%letters;
}
int choose;
int length;
int logic = 0;
printf("How many letters should your word consist of?\n");
scanf("%i",&length);
if(length>0)
{
while(logic == 0, logic <=length) //loop that displays the text
{
++logic;
choose = randn(letters);
printf("%c", choose);
}
}
else
{
printf("A word must contain at least one letter!\n\n");
}
printf("\n"); //seperate
system("PAUSE");
return 0;
}
I want to learn as much of this on my own as possible, so please try to give to much away, and also a thorough explanation of what I am doing wrong would be great.
thankyou in advance.