I am very new to C and have dabbled in Objective-C, AppleScript, and HTML/CSS. I'm sure that my problem is very easy to solve. I am trying to write something that will allow me to input source data and have it ordered in a certain way as output (in this case, citations). Basically, I want to save name, title, publisher, etc. as variables and print them in a certain order.
Here's the issue: The code here terminates too early and when I use fputs and fgets with stdout and stdin it gets stuck and asks the same question forever. What am I missing?
int source_type;
int NumberofAuthors;
char AuthorName1[20];
char AuthorName2[20];
char AuthorName3[20];
char title[20];
char url[100];
char publishingCity[20];
char publisher[20];
char yearPublished[20];
char pageNumbers[20];
int valid;
printf("Welcome to Jackson's Chicago Manual of Style Auto-Footnoter.\n");
fputs("Choose source type:\n a.Book\n b.Journal\n c.Article\n d.Website\n ", stdout);
source_type = getchar();
if (source_type == 'a') {
valid = 1;
} else {
printf("Invalid source selection");
}
while ( valid == 1 && source_type == 'a' )
{
printf("Number of authors [1 or 2]: ");
scanf( "%d", &NumberofAuthors);
if ( NumberofAuthors > 0 && NumberofAuthors < 3 ) {
valid = 1;
printf("Got it, %d author(s).\n", NumberofAuthors);
}
else {
printf( "That's not enough people to write a book.\n" );
}
if ( NumberofAuthors == 1 ) {
printf( "Author's name: " );
scanf("%c", &AuthorName1);
}
if (NumberofAuthors == 2) {
printf("First author's name: " );
scanf("%c", &AuthorName2);
printf("Second author's name: " );
scanf("%c", &AuthorName3);
}
else {
valid = 0;
}
printf("Book title: " );
fgets(title, sizeof(title), stdin);
printf("Publication city: " );
fgets(publishingCity, sizeof(publishingCity), stdin);
}
return 0;
You only change
source_type
outside of thewhile
loop, so once entered the loop the only way to get out is by assigning0
tovalid
. This is done every timeNumberofAuthors != 2
, since the firstif
is not within theif-else
chain. Perhaps you want this instead: