Thread: while loop structure

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    18

    while loop structure

    i am not sure how to write a while loop in the maner that i am working this is what I can figure;

    Code:
    while (studnam != bye)
    {
    
       printf("Please enter the first and last name of the student.\n")
       scanf("%s",&studnam)
       strcmp(studnam, "bye")
       printf("Please enter test scores of the student %s.\n",studnam)
    }

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    18
    I did a search on the board and am trying to pull up something in the faqs right now.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ( void )
    {
       char studnam[20];
       for ( ;; )
       {
          printf("Please enter the first and last name of the student: ");
          fflush(stdout);
          if ( fgets(studnam, sizeof studnam, stdin) )
          {
             char *newline = studnam + (strlen(studnam) - 1);
             if ( *newline == '\n' )
             {
                *newline = '\0';
             }
             if ( strcmp(studnam, "bye") == 0 )
             {
                break;
             }
             printf("Please enter test scores of the student %s.\n", studnam);
             /* ... */
          }
       }
       return 0;
    }
    
    /* my output
    Please enter the first and last name of the student: Dave Sinkula
    Please enter test scores of the student Dave Sinkula.
    Please enter the first and last name of the student: bye
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  4. Controlling Repetition in a Structure
    By Silence in forum C Programming
    Replies: 2
    Last Post: 08-23-2002, 05:35 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM