nested loop, simple but i'm missing it

This is a discussion on nested loop, simple but i'm missing it within the C Programming forums, part of the General Programming Boards category; first off i'm new to the board, would like to say hello to all, look forward to working with everyone. ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    8

    nested loop, simple but i'm missing it

    first off i'm new to the board, would like to say hello to all, look forward to working with everyone.

    that being said here's my problem, I have a rather simple piece of code with a for loop, and i need to create another loop that makes the majority of the code i have written loop, including what's in the for loop. the code is as follows

    the 2nd loop should start after the first printf statement (only request the input once) and should encompass the rest of the code... when i attempt to write it i somehow cause and infinite loop with really skewed checksum values. if someone could point me in the right direction i'd really appreciate it.

    i'm also having trouble getting the loop to end on the input of only a '.' , I think that's probably related to my faulty loop.

    thanks for any help you can give!

    Code:
    // this program will read in a string of char's ending in a period, and then calculate
    // the checksum, and print it out in both the char and int formats
    
      #inclde<stdio.h> 
     
      #define START_CHAR ' ' 
      #define END_CHAR '.'
     
      int check_sum(int sum) //function prototype
     
      int main(void) {
    
      int char_code,sum,checksum,x; //declared variables
      char letter; //declared character
     
      /*ask for input*/
      printf("Enter a message ending in a period; or a period only to quit.\n\n");
    
         /*initialize sum at 0, until char_code = '.' , add it to the sum*/ 
         for(sum = 0; char_code != (int)END_CHAR; sum += char_code) {
              scanf(" %c", &letter);       //read in string ending in period
              char_code = (int)letter;    //swap character to integer
         }
     
      checksum = check_sum(sum);  // call function
     
    printf("Checksum: %c %d\n", (char)checksum, (int)checksum);   //print checksum
     
    return(0);
     
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    where is check_sum implemented?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    typos:
    Code:
     #inclde<stdio.h>
    missing ;
    Code:
    int check_sum(int sum)
    wheres the function?
    Code:
    checksum = check_sum(sum);  // call function

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    within my code i showed i call it and set it equal to variable "checksum" the source for check_sum is in another file that simply states

    Code:
      #include<stdio.h>
    
      #define SPACE_CHAR ' '
    
      int check_sum(int sum)  {
         int total;
      
      /* total = to sum of char codes mod by 64 + space char - 46 */
      /* coming from the period at the end                                      */
      total = (sum % 64) + (int)SPACE_CHAR - 46; 
    
      return(total);
    
    }

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    does your program even compile?!

    try and #include that file with the function implementation

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    haha, yes the program does compile, and it prints out the correct results. i apologize if it is hard to follow, i'm relatively new to the C language (as i'm sure you can probably tell) so any tips on cleaning it up and making it more efficient would be greatly appreciated. I don't at all mind constructive criticism, or if you just want to say it flat out sucks i won't be offended.

    the typo that sirpoopsalot found was simply my error typing it in here, that error isn't present in my code.

    basically i just want that code to loop, and i'm wondering what kind of loop would work best, and where it should be implemented.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i dont understand how it compiles without any errors.. you dont have to #include it to reference the check_sum function?

    anyways, i also dont really understand what the program is supposed to do.. if you could explain it to me i could try and help.

    what do you want this big loop to do, again? sorry if im totally missing it. you said you wanted it to only ask for input once, which it does.. then it reads in the input up to a "." , calculates the checksum and prints it.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    Alright. The program's function is to compile the checksum of a given string of characters that ends in a . , print the checksum in both character and integer form, and then my problem comes in that I want the program to prompt another input up until only a period is inserted. Currently my program just calculates the checksum once, then ends. Ideally it would calculate multiple checksums without having to be re-executed. I hope that wording makes more sense.

    As far doing the #include for the function check_sum... I'm using the vim editor in linux, and in this editor all that is required is the function prototype (which i have just under #define END_CHAR) and then it can be referenced at any time within the main driver. I've never actually seen a script using #include, but it may work just the same.

    Does this make more sense?

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    wow i never knew you could reference a file with a functions implementation without specifically saying so, hmm..

    --print the checksum in both character and integer form
    by character form you dont mean the original characters, right? just the character form of the integral checksum.

    ok, the loop seems to make more sense.. the question is now, under what condition do you want to STOP looping?

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    correct just the character form of the int. checksum.

    well, the loop would end when only a '.' (period) is read as input.

  11. #11
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    How about something like this :

    Code:
    int main()
    {
         int c;
         
         while((c=getchar) != '.')
         {
                //  If here, first character was a not a period.  So process first character c.
                .
                .
                .
                //  Now process the rest of the string as you were doing.
         }
    
         return 0;
    }
    That will keep you looping until a one period sentence.
    Also, scanf() is bad news if you don't understand it entirely. Use getchar(). Or even better, fgets().
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    oh, right.. sorry.. wasnt thinking clearly.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    okay thanks for the tips, i'll give it a whirl

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    okay so this is what i've got

    Code:
    
    
    // this program will read in a string of char's ending in a period, and then calculate
    // the checksum, and print it out in both the char and int formats
    
     
     #inclde<stdio.h> 
     
     #define START_CHAR ' ' 
     #define END_CHAR '.'
     
     int check_sum(int sum) //function prototype
     
     int main(void) 
     {
    
       int char_code,sum,checksum,x; //declared variables
       char letter; //declared character
     
       /*ask for input*/
       printf("Enter a message ending in a period; or a period only to quit.\n\n");
       
       while(scanf(" %c", &letter) != '.')
       {
    
         /*initialize sum at 0, until char_code = '.' , add it to the sum*/ 
         for(sum = 0; char_code != (int)END_CHAR; sum += char_code) 
         {
              scanf(" %c", &letter);       //read in string ending in period
              char_code = (int)letter;    //swap character to integer
         }
     
        checksum = check_sum(sum);  // call function
     
        printf("Checksum: %c %d\n", (char)checksum, (int)checksum);   //print checksum
      } 
    return(0);
     
     }
    and then the function is as i have listed it above, but its giving me bogus answers... when i remove the while loop the answers come out correctly. anybody see what it is i'm doing wrong within the while loop?

  15. #15
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    The return value of scanf is never going to equal '.'
    It will equal the number of successfully matched and assigned input items.

    The first letter you read in is not being used in your checksum function.
    You read it in and then do nothing with it. (you try to compare it to '.' but don't as stated above)
    Last edited by spydoor; 10-23-2006 at 03:54 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Nested For Loop
    By snugy in forum C Programming
    Replies: 3
    Last Post: 02-17-2009, 09:17 AM
  2. Can I make a nested loop out of this?
    By RpgActioN in forum C++ Programming
    Replies: 9
    Last Post: 02-15-2005, 09:59 PM
  3. Nested Loop problem
    By chead in forum C Programming
    Replies: 7
    Last Post: 01-03-2005, 11:47 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Nested Loop
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-07-2002, 12:40 AM

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