0

I am trying to use a loop so that the program will be constantly running. Its suppose to be a "mini karaoke system". However, after I call for the function start at the Main Function, the function start will check the username and compare to the ones in the array. Afterwards it should return if the username already exists or not, and then start the loop from the beginning again from zero. However, for some reason it will start the loop, call function Start and execute entirely without the user's input.

18102314@comunix:~$  vi karakoke2.c
18102314@comunix:~$ vi karaoke2.c
#include <time.h>
#include <stdbool.h>
#include <string.h>

char usern[5][6] = { "lol", " ", " ", " ", " "};
int roomuse =0, bl = 0;

void  menu(){
printf("printei");
}

int gtime(){
clock_t ttime;
return ttime;
}

int checkName(char name[] /*, char usern[5][6]*/){
int i;
for ( i = 0; i < 6; i++){
if(strcmp(name, usern[i]) == 0){
printf("This username already exists. Please try again. \n");
return 0 ;
break;
}
else{ printf("return 4");
return 1; break; }
} }

int start(/*char usern[5][6]*/){
bl = 1;
char ans, name[6];
printf("Welcome, did you reserve a room already? (Y/N)");
scanf("%c",&ans);

if(ans == 'N'){
printf("Please enter a username: (Max 6 characters) ");
scanf("%s", &name);
        if( checkName(name, usern)==1){
        menu(); printf(" return 3"); return 0; }
        else{ printf("return 2");
        bl = 0;
        return 0;  } }
else {printf(" return 1" ); menu(); return 0;}
}

int main(void){

/*char ans, usern[5][6] = {  "lol", " ", " ", " ", " "},  name[6];*/

printf("%d \n", gtime());

Output:

Welcome, did you reserve a room already? (Y/N) N Please enter a username: (Max 6 characters) lol This username already exists. Please try again. return 2Welcome, did you reserve a room already? (Y/N) return 1printeiWelcome, did you reserve a room already? (Y/N)

Expected Outuput:

Welcome, did you reserve a room already? (Y/N) N Please enter a username: (Max 6 characters) lol This username already exists. Please try again. return 2 Welcome, did you reserve a room already? (Y/N)

user438383
  • 5,716
  • 8
  • 28
  • 43
  • Removed putty tag – pmg Dec 05 '21 at 17:16
  • The poor formatting with `printf("return 4"); return 1; break;` is rather odd. In fact you seem fond of the `return; break;` combination. – Weather Vane Dec 05 '21 at 17:21
  • I hazard a guess that `scanf("%c", &ans);` needs to be `scanf(" %c", &ans);` Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). – Weather Vane Dec 05 '21 at 17:23
  • My main problem is that, after I answer 'lol', the next ' Welcome, did you reserve a room already? (Y/N) return 1printei' happens alone. As if after answering 'lol' it calls the function start and skip the 'scanf("%c", &ans); ' and jump staright to the ' else {printf(" return 1" ); menu(); return 0;} } ' – user17596328 Dec 05 '21 at 17:26
  • Is it not necessary to put a 'break' after writing ' return' ? I am a beginner so I am rather confuse, sorry and thank you for pointing that out. – user17596328 Dec 05 '21 at 17:28
  • *"It skips the `'scanf("%c", &ans); '`"*. Please read my [previous comment](https://stackoverflow.com/questions/70236625/c-using-putty#comment124158772_70236625). – Weather Vane Dec 05 '21 at 17:36
  • thank you, however the problem still the same after changing to `' scanf(" %s", &name); '` and `' scanf(" %c",&ans); '` – user17596328 Dec 05 '21 at 17:48
  • `name[6]` is max **five** characters. `scanf("%5s", name);` – Weather Vane Dec 05 '21 at 17:52

0 Answers0