Home > OS >  Yes and no while loop not validating if incorrect string is entered in C
Yes and no while loop not validating if incorrect string is entered in C

Time:01-08

When correct option is selected the program loops fine but if the user selects a wrong choice the program keeps looping at the wrong answer. I tried many other solutions but none seem to be working.________________________________________________________________________________________

#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include <math.h>
#define correct 1
#define incorrect 0

int foo(int *val)
{
    return *val * *val;
}

int main(void)
{
    int  y, flags = incorrect;
    size_t r = 0;
    char s ='y';
    int v;  
    char courses[5][100];       
            
    char list_courses[5][100] = {"CSE1100", 
                                "CSE1101",
                                "CSE1102", 
                                "ITE1100",
                                "ITE1101"};
            
            
    do{
        printf("Enter Courses :\n Courses available: \n CSE1100 \n CSE1101 \n ITE1100\n ITE1101\n");
        scanf("%s", courses[r]);
        getchar(); 
        r  ;
        
        for(int i = 0;i < 5; i  )
        {
            y = strcmp(&list_courses[i][0],courses[i]);
            if(y == 0)
            {
                flags = correct;
            }        
        }   
        while(flags != correct)
        {
                
            for(size_t p = 0; p <r ; p  )
            {
                printf("%s incorrect choose from list of courses \n CSE1100 \n CSE1101 \n ITE1100\n ITE1101\n", courses[p]);
                printf("try again!:\n", courses[p]);   

                scanf("%s", courses[p]);
                getchar();   
            }
        }       
        for(int i = 0;i < 5; i  )
        {
            y = strcmp(&list_courses[i][0],courses[i]);
            if(y == 0)
            {
                flags = correct;
            }
        }
            
        printf("Would you like to enter another course? (y or n) \n"); 
        s = getchar();
    }while(s == 'y');
}

CodePudding user response:

I tried formatting the code and added some comments.

#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include <math.h>
#define correct 1
#define incorrect 0

int main()
{
int  y, flags = incorrect;
size_t r = 0;
char s ='y';
int v;  
char courses[5][100];       
char list_courses[5][100] = {"CSE1100", 
                             "CSE1101",
                             "CSE1102", 
                             "ITE1100",
                             "ITE1101"};
        
        
    do{
           printf("Enter Courses :\n Courses available: \n CSE1100 \n CSE1101 \n ITE1100\n ITE1101\n");//collects input from user
           scanf("%s", courses[r]);
           getchar(); 
           r  ;
         
           for(int i = 0;i < 5; i  ){
            y = strcmp(&list_courses[i][0],courses[i]);    // valdition if input entered matches the string in the list of courses
             if(y == 0){
             flags = correct;
                       }
                                    }   
           while(flags != correct){                        // checks to see if wrong input is entered
                 
                for(size_t p = 0; p <r ; p  ){
                 printf("%s incorrect choose from list of courses \n CSE1100 \n CSE1101 \n ITE1100\n ITE1101\n", courses[p]);// prompts user to try again after entering incorrect input
                 printf("try again!:\n", courses[p]);   
                    scanf("%s", courses[p]);
                    getchar();   
                                              } 
                                 }       
                for(int i = 0;i < 5; i  ){
                y = strcmp(&list_courses[i][0],courses[i]);// revalidates the input the user tries to enter again if input incorrect
                    if(y == 0){
                    flags = correct;
                               }
                                            }
          printf("Would you like to enter another course? (y or n) \n"); // prompts user to enter another course
           s = getchar();
                
       }while(s == 'y');    // if user selects yes to run program again
}

CodePudding user response:

The loop for(size_t p = 0; p <r ; p ) seems redundant, as it does not seem appropriate to print the error message several times.

The lines

scanf( "%s", courses[r] );
getchar();

will not work if the user enters more than one word of input. In that case, the getchar function call will not consume the newline character from the input stream. Also, it is always possible that scanf will fail, so you should always check the return value of that function. Additionally, you should always restrict the number of characters written to the buffer, otherwise, if the user enters too much data, this will cause a buffer overflow, and your program may crash. Therefore, is better to use the following code:

int c;

if ( scanf( "           
  •  Tags:  
  • Related