While loop ..crating an infanat loop

This is a discussion on While loop ..crating an infanat loop within the C Programming forums, part of the General Programming Boards category; OK ... aparently I cant figure out how to write a while loop .. here is the code and I ...

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

    While loop ..crating an infanat loop

    OK ... aparently I cant figure out how to write a while loop .. here is the code and I am using dev C++ the code asks for for the student name and just keeps asking... were can I find good information on how to write this part corectly.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(int argc, char *argv[])
    {
       char studnam [30];  
       int bye = 0;
       int prtrep = 0;
       int score = 0;
       char grade [1];
       int average = 0;
    while (studnam != "bye")
          printf("Please enter the first and last name of the student.\n");
          scanf("%s",&studnam);
          printf("Please enter test score 1 for student %s.\n",studnam);
          scanf("%d",&score); 
          printf("Please enter test score 2 for student %s\n",studnam);
    	
    {       
    if (average  <= 59);
    printf("grade is 'F'\n.");
    
    if (average >= 60 <= 69);
    printf ("grade is 'D'.\n");
    
    if (average <= 70 >= 79)
    printf("grade is 'C'.\n");
    
    if (average <= 80 <= 89)
    printf("grade is 'B'.\n");   
    
    if (average <= 90 <= 100)
    printf("grade is 'A'\n");     
    }
    
    
           printf("Would you like to print the entered informaion?");
           scanf("%d", prtrep);
           printf("1: Print Report");
           printf("2: Exit Program");
    {
           if (prtrep == 2);
           printf("than print file grades record");
    
           if (prtrep == 1);
           printf("Hit enter to exit program now.");
    }       
      
           system("PAUSE");
           return 0;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Turn on warnings to a high level. Realize that C isn't C++ and you compare strings using strcmp. Initialize data. Using proper indentation and full bracing.
    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.*

  3. #3
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46
    Code:
    while (studnam != "bye")
    try
    Code:
    char bye[3];
    strcpy( bye, 'bye'); <---- or "bye"
    while ( strcmp(studnam, bye ) != 0 ){}

  4. #4
    old man
    Join Date
    Dec 2005
    Posts
    90
    This is your entire loop, as you've written it:

    Code:
    /* infinite loop */
    while (studnam != "bye")
          printf("Please enter the first and last name of the student.\n");
    Nothing else you've written past this point will execute because with no opening brace following the while statement, only the next line will execute. And obviously your condition can never be met (not just because of this, but also because, as has been mentioned, your test is broken).

    What you want is something like this:

    Code:
      while (strncmp (studname, "bye", 3) != 0)
      {
        /* stuff you want to do in the loop */
      }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your code is wrong for a few reasons:
    1 - You must use " when dealing with strings of characters. ' is used for single characters.
    2 - Your array bye doesn't have enough room in it for the null character, so you're not allowed to use that array as if it were a string. You'll need at least 4 bytes in your array if you plan on using it the way you currently are.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Code:
    scanf("%s",&studnam);
    In addition to everything said above, check this line. It will crash sooner or later. Right now, it's probably working because "bye" is the same size as the address of your array. Once it gets larger, you'll be in all kinds of trouble.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46
    Quote Originally Posted by quzah
    Your code is wrong for a few reasons:
    1 - You must use " when dealing with strings of characters. ' is used for single characters.
    2 - Your array bye doesn't have enough room in it for the null character, so you're not allowed to use that array as if it were a string. You'll need at least 4 bytes in your array if you plan on using it the way you currently are.


    Quzah.
    Thanks, quzah for pointing that out.
    I thought that a char[3] can store at to 3 characters, because one character takes 1 byte. Why do I need an extra byte for?
    Thanks in advance.

  8. #8
    old man
    Join Date
    Dec 2005
    Posts
    90
    Quote Originally Posted by Afrinux
    I thought that a char[3] can store at to 3 characters, because one character takes 1 byte. Why do I need an extra byte for?
    Thanks in advance.
    A string in C is nul-terminated by definition, so you need room for the \0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-19-2005, 11:30 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

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