-
string input issue
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
-
scanf() sucks for anything even remotely complicated like what you're doing.
The reason being scanf() only processes the MINIMUM number of characters for a given conversion. This typically means it leaves lots of \n on the input stream to confuse later input calls (like your ^\n attempt for example).
Code:
char buff[BUFSIZ];
fgets( buff, sizeof buff, stdin );
sscanf( buff, "%7s", courseCODE);
Each input is first read with fgets(), which reads a whole line (including a \n).
Then you can process that line using whatever you like, for example with sscanf.
> should I creat new threads for those for the sake of searchability or continue to post in this thread.
If it's continuing the same question, to clarify points raised, or very similar then stick to one thread.
If it's different question about a different part of the code, start a new thread.
-
Hey Salem
Thanks for the quck relpy.
I have implemented the code you suggested but i have one of my issue in a different location now. It complete skips the course code entry and displays the add subject portion.
I am not sure why
Code:
//declare local variables
int i = 0;
int j = 0;
char courseCODE[8];
char subjectNAME[31];
char buff[BUFSIZ];
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 ");
fgets( buff, sizeof buff, stdin );
sscanf( buff, "%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 ");
fgets( buff, sizeof buff, stdin );
sscanf( buff, "%30s", subjectNAME);
I am also not getting the correct output in my txt file this was what was in the file
Code:
面面面面面面面面
This
89.00
67.00
I was not able to type anything in for the first field which is the strange character above, in the next field that says "This" I had typed in: This is a test
Thanks in advance
clearrct
-
Ok I made a quick change I put a getchar(); above the fget and it seems to have fixed the first problem but it still is only capturing the first word before the space.
Code:
comp678
programing
65.00
78.00
where it says programing aboce I typed in programing in c
-
> but it still is only capturing the first word before the space.
Try the "%[^\n]" again.
> Ok I made a quick change I put a getchar(); above the fget
You've still got a scanf() call elsewhere then.
-
I assume you mean in the fget now?
tia
clearrtc
Excellent That worked great. Thanks for all your assistance.
Carl
-
speaking of fgets()...I used it in a program to enter a string after which I sent it of to some function to get rid of the '\n' and substitute it with a '\0'.
Code:
printf("\nInsert first name:");
fgets(name,sizeof(name),stdin);
clear_string(name,sizeof(name));
printf("\nInsert surname:");
fgets(surname,sizeof(surname),stdin);
clear_string(surname,sizeof(surname));
Well it kind of doesn't work cause as soon as it comes to the first fget() it doesn't wait for me to imput anything and it goes on and prints out the second print. I tried putting the two 'clear_string' functions in comments but it still doesn't wait for me to type anything in. Then I tried using fgets() alone in a smaller program:
Code:
#include <stdio.h>
int main()
{
char name[50];
char surname[50];
printf("\nInsert first name:");
fgets(name,sizeof(name),stdin);
printf("\nInsert surname:");
fgets(surname,sizeof(surname),stdin);
printf(">%s< & >%s<",name,surname);
char pause;
pause=getchar();
return 0;
}
and it works fine. Any ideas?
-
> Well it kind of doesn't work cause as soon as it comes to the first fget() it doesn't wait for me to imput anything
Look for residual scanf() calls earlier on in the code.
If you're using fgets() for any input, you should really use it for ALL input.
Trying to mix and match fgets() with scanf() is just too weird.
-
I've had a similar problem with it stoping after any spaces in input, so I tried the sscanf(yadda, yadda, yaada);...but it need a library, which one do i use for this?
-