944,172 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 6386
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 17th, 2006
0

Help with strings/scanf in C (for newbie)

Expand Post »
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:

  1. for ( x = 0; x < 512; x++ ) {
  2.  
  3. scanf("%c", &sequencetemp);
  4.  
  5. if ((sequencetemp=='A') || (sequencetemp=='T') || (sequencetemp=='G') || (sequencetemp=='C')){
  6. sequence[x] = sequencetemp;
  7. } else if (sequencetemp=='X'){
  8. break;
  9. }else{}
  10.  
  11. }

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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tehloki is offline Offline
18 posts
since Oct 2006
Oct 17th, 2006
0

Re: Help with strings/scanf in C (for newbie)

Upon reading some of our textbook "C For Dummies", it really looks like this assignment is asking us for something which is really outside our knowledge. So I guess a solution using string functions is probably fine, if not essential.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tehloki is offline Offline
18 posts
since Oct 2006
Oct 18th, 2006
0

Re: Help with strings/scanf in C (for newbie)

Using &quot;scanf&quot; 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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Oct 18th, 2006
0

Re: Help with strings/scanf in C (for newbie)

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tehloki is offline Offline
18 posts
since Oct 2006
Oct 18th, 2006
0

Re: Help with strings/scanf in C (for newbie)

For obtaining single char you can use getchar which returns int. You can read this and this for obtaining the whole string.
Last edited by andor; Oct 18th, 2006 at 6:32 am.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Oct 18th, 2006
0

Re: Help with strings/scanf in C (for newbie)

Click to Expand / Collapse  Quote originally posted by andor ...
For obtaining single char you can use getchar which returns int. You can read this and this for obtaining the whole string.
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tehloki is offline Offline
18 posts
since Oct 2006
Oct 18th, 2006
0

Re: Help with strings/scanf in C (for newbie)

Click to Expand / Collapse  Quote originally posted by tehloki ...
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?
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
  1. for ( x = 0; x < 511; ) { /* x < 511 space for '\0' */
  2.  
  3. scanf("%c", &sequencetemp); /* dont like this part but if teacher said than ... */
  4.  
  5. if ((sequencetemp=='A') || (sequencetemp=='T') || (sequencetemp=='G') || (sequencetemp=='C')){
  6. sequence[x++] = sequencetemp;
  7. }
  8. else if (sequencetemp=='X'){
  9. sequence[x++] = sequencetemp;
  10. sequence[x] = '\0';
  11. break;
  12. }
  13. else {
  14. printf("ERROR\n");
  15. return 1;
  16. }
  17. }
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Oct 18th, 2006
0

Re: Help with strings/scanf in C (for newbie)

Click to Expand / Collapse  Quote originally posted by andor ...
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
  1. for ( x = 0; x < 511; ) { /* x < 511 space for '\0' */
  2.  
  3. scanf("%c", &sequencetemp); /* dont like this part but if teacher said than ... */
  4.  
  5. if ((sequencetemp=='A') || (sequencetemp=='T') || (sequencetemp=='G') || (sequencetemp=='C')){
  6. sequence[x++] = sequencetemp;
  7. }
  8. else if (sequencetemp=='X'){
  9. sequence[x++] = sequencetemp;
  10. sequence[x] = '\0';
  11. break;
  12. }
  13. else {
  14. printf("ERROR\n");
  15. return 1;
  16. }
  17. }
This still doesn't give any input to the sequence array... when i try printing it, all of the values are still null.

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tehloki is offline Offline
18 posts
since Oct 2006
Oct 18th, 2006
0

Re: Help with strings/scanf in C (for newbie)

What condition should occur if the user enters 'X' without filling the entire 512 element char array ? What if he enters the third character as 'X' then what should happen, what should the remaining array contain ?
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Oct 19th, 2006
0

Re: Help with strings/scanf in C (for newbie)

If you using scanf than:
input: G (hit enter) X (hit enter) your output GX
If using getchar
input: GX your output GX.
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int sequencetemp;
  6. char sequence[512];
  7. unsigned short x;
  8.  
  9. for ( x = 0; x < 511; ) { /* x < 511 space for '\0' */
  10.  
  11. sequencetemp = getchar();
  12.  
  13. if ((sequencetemp=='A') || (sequencetemp=='T') || (sequencetemp=='G') || (sequencetemp=='C')){
  14. sequence[x++] = sequencetemp;
  15. }
  16. else if (sequencetemp=='X'){
  17. sequence[x++] = sequencetemp;
  18. sequence[x] = '\0';
  19. break;
  20. }
  21. else {
  22. printf("ERROR\n");
  23. return 1;
  24. }
  25. }
  26. puts("The string is: ");
  27. puts(sequence);
  28.  
  29. return 0;
  30. }
Last edited by andor; Oct 19th, 2006 at 4:57 am.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Explain what program does
Next Thread in C Forum Timeline: am i using fgets correctly? (Cprog)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC