| | |
Help with strings/scanf in C (for newbie)
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 18
Reputation:
Solved Threads: 0
I am taking a programming course, and since we have not been introduced to string functions (puts(), gets(), etc) I need to take some input from the user using scanf.
The assignment is to read a DNA sequence, storing it in a char array. The array must be 512 characters, and accept only the characters 'G', 'A', 'T', and 'C'. Space, tab, and enter should be ignored. Additionally, the user enters 'X' to end the DNA sequence. If the user enters any character other than those allowed, the program must return -1 on exit (an error).
The input from the user would look something like: "GATCACTACX"
My problem is that I cannot seem to assign any values to my array. My code so far (just for reading the string) looks like this:
Upon testing, my char array is just full of random junk characters. Is there something I am doing wrong? Does anyone have a better solution? Can I do this assignment without using string functions?
Any help would be appreciated.
The assignment is to read a DNA sequence, storing it in a char array. The array must be 512 characters, and accept only the characters 'G', 'A', 'T', and 'C'. Space, tab, and enter should be ignored. Additionally, the user enters 'X' to end the DNA sequence. If the user enters any character other than those allowed, the program must return -1 on exit (an error).
The input from the user would look something like: "GATCACTACX"
My problem is that I cannot seem to assign any values to my array. My code so far (just for reading the string) looks like this:
C Syntax (Toggle Plain Text)
for ( x = 0; x < 512; x++ ) { scanf("%c", &sequencetemp); if ((sequencetemp=='A') || (sequencetemp=='T') || (sequencetemp=='G') || (sequencetemp=='C')){ sequence[x] = sequencetemp; } else if (sequencetemp=='X'){ break; }else{} }
Upon testing, my char array is just full of random junk characters. Is there something I am doing wrong? Does anyone have a better solution? Can I do this assignment without using string functions?
Any help would be appreciated.
Using "scanf" to read character input is a recepie for disaster, however small the purpose maybe. Post your entire code and I will show you an alternative. PS: ALso declare your array as
char sequence[ BUFSIZ ] = {\'0'}; Last edited by ~s.o.s~; Oct 18th, 2006 at 12:30 am.
I don't accept change; I don't deserve to live.
•
•
Join Date: Oct 2006
Posts: 18
Reputation:
Solved Threads: 0
I'm afraid that IS my entire code (other than headers and such). I haven't progressed beyond actually reading the user's input at all, because I can't get anything to work.
Oh, and the professor really mustn't be teaching us very good C practices.. we didn't even learn about the null character. heh.
Can you show me the alternative to using scanf? It would be greatly appreciated.
Oh, and the professor really mustn't be teaching us very good C practices.. we didn't even learn about the null character. heh.
Can you show me the alternative to using scanf? It would be greatly appreciated.
•
•
Join Date: Oct 2006
Posts: 18
Reputation:
Solved Threads: 0
•
•
•
•
For obtaining single char you can use getchar which returns int. You can read this and this for obtaining the whole string.
Could this be done in a loop?
•
•
•
•
hmm... fgets seems useful, but how would I modify the method in the second "this" to read a sequence of up to 512 characters, adding only G,A,T, and C to the array, and stopping the input before the X at the end?
Could this be done in a loop?
Check this
C Syntax (Toggle Plain Text)
for ( x = 0; x < 511; ) { /* x < 511 space for '\0' */ scanf("%c", &sequencetemp); /* dont like this part but if teacher said than ... */ if ((sequencetemp=='A') || (sequencetemp=='T') || (sequencetemp=='G') || (sequencetemp=='C')){ sequence[x++] = sequencetemp; } else if (sequencetemp=='X'){ sequence[x++] = sequencetemp; sequence[x] = '\0'; break; } else { printf("ERROR\n"); return 1; } }
If you want to win, you must not loose (Alan Ford)
•
•
Join Date: Oct 2006
Posts: 18
Reputation:
Solved Threads: 0
•
•
•
•
sorry I didn't read your post carefully. I asume that U must use scanf, so then U must. Ok the problem is your loop. Dont increment x every time.
Check this
C Syntax (Toggle Plain Text)
for ( x = 0; x < 511; ) { /* x < 511 space for '\0' */ scanf("%c", &sequencetemp); /* dont like this part but if teacher said than ... */ if ((sequencetemp=='A') || (sequencetemp=='T') || (sequencetemp=='G') || (sequencetemp=='C')){ sequence[x++] = sequencetemp; } else if (sequencetemp=='X'){ sequence[x++] = sequencetemp; sequence[x] = '\0'; break; } else { printf("ERROR\n"); return 1; } }
I think I am just going to abandon scanf. Could you show me how to read the string (ending before X, GATC only, all others error) with the fgets() function?
If you using scanf than:
input: G (hit enter) X (hit enter) your output GX
If using getchar
input: GX your output GX.
input: G (hit enter) X (hit enter) your output GX
If using getchar
input: GX your output GX.
C Syntax (Toggle Plain Text)
#include <stdio.h> int main() { int sequencetemp; char sequence[512]; unsigned short x; for ( x = 0; x < 511; ) { /* x < 511 space for '\0' */ sequencetemp = getchar(); if ((sequencetemp=='A') || (sequencetemp=='T') || (sequencetemp=='G') || (sequencetemp=='C')){ sequence[x++] = sequencetemp; } else if (sequencetemp=='X'){ sequence[x++] = sequencetemp; sequence[x] = '\0'; break; } else { printf("ERROR\n"); return 1; } } puts("The string is: "); puts(sequence); return 0; }
Last edited by andor; Oct 19th, 2006 at 4:57 am.
If you want to win, you must not loose (Alan Ford)
![]() |
Similar Threads
- C help to newbie (C)
Other Threads in the C Forum
- Previous Thread: Explain what program does
- Next Thread: am i using fgets correctly? (Cprog)
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches initialization intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






