Help with strings/scanf in C (for newbie)

Thread Solved

Join Date: Oct 2006
Posts: 18
Reputation: tehloki is an unknown quantity at this point 
Solved Threads: 0
tehloki tehloki is offline Offline
Newbie Poster

Help with strings/scanf in C (for newbie)

 
0
  #1
Oct 17th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 18
Reputation: tehloki is an unknown quantity at this point 
Solved Threads: 0
tehloki tehloki is offline Offline
Newbie Poster

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

 
0
  #2
Oct 17th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,615
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

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

 
0
  #3
Oct 18th, 2006
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 18
Reputation: tehloki is an unknown quantity at this point 
Solved Threads: 0
tehloki tehloki is offline Offline
Newbie Poster

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

 
0
  #4
Oct 18th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

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

 
0
  #5
Oct 18th, 2006
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.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 18
Reputation: tehloki is an unknown quantity at this point 
Solved Threads: 0
tehloki tehloki is offline Offline
Newbie Poster

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

 
0
  #6
Oct 18th, 2006
Originally Posted by andor View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

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

 
0
  #7
Oct 18th, 2006
Originally Posted by tehloki View Post
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. }
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 18
Reputation: tehloki is an unknown quantity at this point 
Solved Threads: 0
tehloki tehloki is offline Offline
Newbie Poster

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

 
0
  #8
Oct 18th, 2006
Originally Posted by andor View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,615
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

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

 
0
  #9
Oct 18th, 2006
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 ?
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

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

 
0
  #10
Oct 19th, 2006
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.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC