Hey all,
I am having an issue with my final programming assignment. Basically we have to create a program that collects a course code, subject name and 2 marks for the subject and write them to a file. I can do this without to much difficulty except for the subject name, if I try to input spaces the program skipps over the last two scanf statements and seem to loop at my menu screen (in a function not shown here) if I don't input spaces it works fine. Depending on how i modify the highlighted line below the program will sometimes skip right over that input screen and move on to the next.
Code:
int addSubject()
{
//declare local variables
int i = 0;
int j = 0;
char courseCODE[8];
char subjectNAME[31];
float firstMARK;
float secondMARK;
FILE *fp;
// Get course code from user
system ("cls");
printf("\n\n Class Mark Subject Management \n\n");
printf(" ** Add Subject ** \n\n");
printf(" Please enter the 7 digit course code \n");
printf("\n Please press <Enter> to continue ");
scanf("%7s", courseCODE);
// Check course code for alpha chars and convert them to upper case
for(j=0; j<=7; j++)
{
if(isalpha(courseCODE[j]))
{
courseCODE[j]=toupper(courseCODE[j]);
}
}
// Get input for subjectNAME from user
system ("cls");
printf("\n\n Class Mark Subject Management \n\n");
printf(" ** Add Subject ** \n\n");
printf(" Please enter the subject name \n");
printf("\n Please press <Enter> to continue ");
scanf("%[^\n]", subjectNAME);
// Get input for the firstMARK from user
system ("cls");
printf("\n\n Class Mark Subject Management \n\n");
printf(" ** Add Subject ** \n\n");
printf(" Please enter the first mark \n");
printf("\n Please press <Enter> to continue ");
scanf("%f", &firstMARK);
// Get input for the secondMARK from user
system ("cls");
printf("\n\n Class Mark Subject Management \n\n");
printf(" ** Add Subject ** \n\n");
printf(" Please enter the second mark \n");
printf("\n Please press <Enter> to continue ");
scanf("%f", &secondMARK);
// Open the file in append mode and write the data to it and close the file
fp = fopen(MarkFile, "a");
fprintf(fp, "%s\n%s\n%.2f\n%.2f\n" ,courseCODE, subjectNAME, firstMARK, secondMARK);
fclose(fp);
return 0;
I have tried the highlighted peice of code like this as well
Code:
scanf("%30s", subjectNAME);
Any help would be appreciated.
Oh one other question relating to posting, I am sure I will have a few more questions on this assignment but not realted to this particulare problem, should I creat new threads for those for the sake of searchability or continue to post in this thread.
Thanks
clearrtc