I’m just starting off with characters and arrays. I wrote some code to generate a string of certain random and non-random characters. Well, actually it’s supposed to generate bargain codes for Dominos pizza. Ahem…
Anyway, this is the code, and I’m perplexed by lack of any output whatsoever. I compiled with gcc -Wall, and there’s no warning or errors. So it’s apparent that it’s some fundamental structural aspect regarding strings.
I’d appreciate any insights regarding this.
Code:
#include <stdio.h>
#include <stdlib.h>
#include "conio.h"
#include <time.h>
int genrandom(int,int);
char randAlph(void);
char letterize(int);
char randDigit(void);
char digitize(int);
void weaver(void);
void prtall(char[],int);
int main (void) {
srand(time(0));
weaver();
return 0;
}
void weaver(void) {
//BG5C?---1
char word[10];
word[0]='B';
word[1]='G';
word[2]='5';
word[3]='C';
word[4]=randDigit();
word[5]=randAlph();
word[6]=randAlph();
word[7]=randAlph();
word[8]='\0';
prtall(word,8);
}
void prtall(char worder[],int len){
int i;
for (i=0;(i=len);i++) {
if ( worder[i] != '\0' ){
printf("%c",worder[i]);
}
}
printf("\n");
}
int genrandom(int mino,int maxo) {
int val=mino+rand()/(RAND_MAX/(maxo-mino)+1);
return val;
}
char randAlph (void){
int val;
char text;
val=genrandom(0,26);
text=letterize(val);
return text;
}
char randDigit () {
int val;
char text;
val=genrandom(0,9);
text=digitize(val);
return text;
}
char letterize(int num) {
char letter='A'+num;
return letter;
}
char digitize(int num) {
char digit='0'+num;
return digit;
}
for (i=0;(i=len);i++)Did you mean i <= len ?
i=lenis anassignment, returns true if assignment succeeds.So what happens is
i gets assigned the value of len, (which is successful) hence returns true.
Hence for loop condition gets satisfied.
What you need is