[ back to toc ]

code won't read 'gets' after first time.

Date: 2002/03/16 12:24

Q:
I know it is really scattered, but i only jst started to learn C. and this
is a University Assignment so dont be confused about the subject of the
coding.
i'm using Dev C/C++ for windows 98. my prgoram wont read the 'gets' input
when the program is repeated.

#include <stdlib.h>
#include <windows.h>
#include <string.h>
#include <math.h>

float day, month, year, hour, minute, second, result, luckyno;
int monthi, resulti;
char name[40];
char *montharray[12] = { "Janruary" , "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November",
"December" };
char *luckyarray[27] = { "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" };

void getname ()
{
printf("\n");
printf("Please Enter the Person's Name: \n");
gets(name);
printf("\n");

}

void information()
{
printf("\n");
printf("Please Enter the Hour(hh): ");
scanf(" %f", &hour);
printf("\n");
printf("Please Enter the Minute(mm): ");
scanf(" %f", &minute);
printf("\n");
printf("Please Enter the Second(ss): ");
scanf(" %f", &second);
printf("\n");
printf("Please Enter the Day(dd): ");
scanf(" %f", &day);
printf("\n");
printf("Please Enter the Month(mm): ");
scanf(" %f", &month);
printf("\n");
printf("Please Enter the Year(yy): ");
scanf(" %f", &year);
printf("\n");
monthi = month - 1;

}

void calc(){

result = (day * 100000000) + (month * 1000000) + (year * 10000) + (hour
* 100) + (minute);
result = result + (second / 100);
result = result / 3.14;
result = sqrt (result);
result = result * 10;
luckyno = result;
for( ; result > 1 ; ) {
result = result - 1;
}
result = result * 10;
resulti = result;
system("CLS");
}

void printticket()

{

printf
("************************************************************************
*****\n");
printf ("*\n");
printf ("*______________ _-_ BEST OF
BOTH WORLDS \n");
printf ("*\n");
printf ("* \\__(--------/_-_ ____.---'---'---.____ XXIV
INTERNATIONAL CONFERENCE \n");
printf ("*\n");
printf ("* \\_ \\ \\----._________.----/ VENUE: Sydney
Entertainment Centre\n");
printf ("*\n");
printf ("* \\ \\ / / '-_-' DATE: %2.0f, %s,
200%1.0f\n", day, montharray[monthi], year);
printf ("*\n");
printf ("* __,--'.'-'..'-_
\n");
printf ("*\n");
printf ("* /____ || TICKET HOLDER:
%s\n", name);
printf ("*\n");
printf ("* '--.____.-' LUCKY DOOR PRIZE #:
%7.0f%s\n", luckyno, luckyarray[resulti]);
printf ("*\n");
printf
("************************************************************************
*****\n");
printf ("\n");

}

int main(){
start:
getname();
information();
getname();
calc();
printticket();
system("PAUSE");
if(MessageBox(0, "Do You Wish to Enter More Names?", "Ticket Master",
MB_YESNO || MB_ICONQUESTION) == IDOK)

{ system("CLS");

goto start;

}
else
{
system("EXIT");
}
return 0;
}

Thanks
A:
The issue is that the scanf function reads up to the next space or new
line, but does not work up the new line itself. It means that after the
line

scanf(" %f", &year);

is executed there remains a new line character in the input buffer and the
gets function sees that as an empty line. I would rather use a buffer,
read a line into the buffer each time you need a value calling gats and
then use sscanf to get the value out of that.

Also do not forget to include stdio.h to have the proper prototypes for
printf, scanf and alikes.

Change the

MB_YESNO || MB_ICONQUESTION

to

MB_YESNO | MB_ICONQUESTION

The || you use is logical OR which results 0 or 1. In this case on these
constants it is 1. That is not what you need. What you need is the bitwise
OR of the two values. Use a single | instead of double. This is the reason
why your msg box says Yes/Cancel instead of Yes/no.

Do not call system("EXIT")! This maked no harm, but is totally useless!
Say

exit(0);

in your code instead.

Regards,
Peter

[ back to toc ]