Problem with do_while loop when trying to fill an array[n]. [Fairly Beginner in C]

This is a discussion on Problem with do_while loop when trying to fill an array[n]. [Fairly Beginner in C] within the C Programming forums, part of the General Programming Boards category; Hello, world! I would like some help with a block of code here. [CODE]do{ printf("Give me the number %d amino-acid ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    11

    Problem with do_while loop when trying to fill an array[n]. [Fairly Beginner in C]

    Hello, world! I would like some help with a block of code here. [CODE]do{ printf("Give me the number %d amino-acid or enter ""0"" to stop.\n", i+1); scanf("%c", &amino_acids[i]); i++;} while (amino_acids[i-1]!='0' && i

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    11
    Sorry, made a mistake while editing. Give me a second to rewrite the thread.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    11
    Well, as i was saying before I made that retarded editing mistake (sorry, but I am generally new at forums, always thought I could manage to do everything without help )
    So..The code is here:
    Code:
    int i=0,pos;
    char amino_acids[300];
    for (i=0;i<300;i++){
    amino_acids[i]='1';}
    i=0;
    do{
        printf("Give me the number %d amino-acid or enter ""0"" to stop.\n", i+1);
        scanf("%c", &amino_acids[i]);
        i++;}
        while (amino_acids[i-1]!='0' && i<300);
    Well, in the first scan, everything is fine, but then it skips the second scan, even though it prints me the message I wanted. If anyone out there could help, I would be really glad! And again, sorry for the forum mistake!
    Thanks in Advance,
    Nick!
    PS:This one
    Code:
    for (i=0;i<300;i++){
    amino_acids[i]='1';}
    was made "just in case", for the compiler's sake.

  4. #4
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    You need to indent your code a little better, and the "just in case" code is actually pretty vital or your loop conditions acts on completely random data (because amino_acids isn't initialised otherwise).

    When you say it "skips" the second scan, you actually mean it skips every other request for input. That's because the end-of-line character is also read as a separate character (you can see this by adding:

    Code:
    printf("%i\n", amino_acids[i]);
    immediately after your scanf(). It will print your ASCII code of your input and then the next scanf will print "10", which is the line-feed character (might also see a "13" on Windows - the carriage-return character). If you enter more than one character (e.g. the number 10) it will actually pass through multiple scanf loops before it stops.

    That's what's happening. How you solve it, I leave as an exercise to the reader.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    11
    Thanks a lot for your help!
    That would be a little hard to figure out on my own, I guess, so thanks!
    Btw, I fixed it with this line of code:
    Code:
    if (i%2==0){
        printf("Give me the number %d amino-acid or enter ""0"" to stop.\n", (i+2)/2);}
        
        scanf("%c", &amino_acids[i]);
        i++;}
        while (amino_acids[i-1]!='0' && i<600);
    I presume it's not the best choice, but I think it would work..:-)
    If you do have, though, an advanced opinion on how to solve this, I would really like to know (knowledge is power!).
    Oh, and for the "Just in Case", it really was just in case, because I would rewrite every single element of the array anyway. I could just put the printf after and make some minor modifications, but, well, lazy-ass me is lazy-ass me!
    Again though, thanks a lot for the reply!

    *Edit: It doesn't solve the "multiple-digits" problem, but for my Program (which requires for input a single ASCII char), it works ok :-) )
    Last edited by nick412; 12-14-2011 at 05:09 AM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Change this from this:
    Code:
    scanf("%c", &amino_acids[i]);
    To this:
    Code:
    scanf(" %c", &amino_acids[i]);
    Otherwise, you'll have a problem with the newline char being interpreted as a char for the amino acids[i]. That is what's causing the "skipping" you report.

    And welcome to the forum, Nick!

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    11
    Thanks Adak, that's much better!
    But how does the black space "negate" the newline char?
    Oh, and thanks for the welcome!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Problem with Loop
    By ToweyLeake in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2011, 10:12 AM
  2. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  3. Beginner at C! help...Array problem
    By niceguy in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 07:31 AM
  4. Fairly simple problem
    By fatdunky in forum C Programming
    Replies: 1
    Last Post: 11-14-2005, 10:34 PM
  5. Please help! Beginner stuck on table and array problem!
    By robsmith in forum C Programming
    Replies: 2
    Last Post: 03-10-2005, 10:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21