0

I'm not sure if this question has been asked before but I couldn't find the exact issue so here it is.

At the beginning of the program I have a log in screen that will hide the entered password. It will then be compared against a user name and password from a txt file. For the sake of this question I just set the credentials in the main.

After hitting the enter to break the password entry it exits the do while and should then start the while and ask for the choice from the menu using a scanf. The program ignores the scanf and runs infinitively. The functions are declared above and I have included stdio.h, conio.h, stdlib.h, string.h. The code is as follows:

    void main() {

int i, run = 0, choice, loc = 0;
node* top;
top = NULL;

login();
//While the run loop is 0 show the menu and take choice input
do {

    printf("\n\tMenu\n");
    printf("\n1 - Add survey");
    printf("\n2 - Display all survey");
    printf("\n3 - Display a survey");
    printf("\n4 - update a survey");
    printf("\n5 - delete a survey");
    printf("\n6 - generate survey statistics");
    printf("\n7 - Print to file");
    printf("\n8 - Quit");
    printf("\nPlease enter your choice:");
    scanf("%d", &choice);


    if (choice == 1) {
        addSurvey(&top);
    }// add a survey
    else if (choice == 2) {
        displaySurveys(top);
    }//display all surveys
    else if (choice == 3) {
        printf("\nEnter the PPS: ");
        scanf("%d", &loc);
        diplaySurvey(top, loc);
    }//display particular survey
    else if (choice == 4) {
        printf("\nEnter the PPS: ");
        scanf("%d", &loc);
        updateSurvey(top, loc);
    }// update a survey
    else if (choice == 5) {
        printf("\nEnter the PPS: ");
        scanf("%d", &loc);
        deleteSurvey(&top, loc);
    }// delete a survey
    else if (choice == 6) {
        generateStats(&top);
    }// generate statistics
    else if (choice == 7) {
        printToFile(&top);
    }//Print to file
    else if (choice == 8) {
        run = 1;
        exit(0);
    }//Quit

} while (run == 0);//while running

}//main

void login() {

int i, allGood = 1;
char letter, username[10], password[10], user[10] = "user", pass[10] = "pass";

//the login screen
do { // Do this while its not all good :(
    printf("\nTo log in please enter your credentials: ");
    printf("\nUsername: ");
    scanf("%", username);
    printf("Password: ");

    //Take in a password from the user and replace the input with ****
    for (i = 0; i < 10; i++) {
        letter = getch();
        //if the user hits the return key end password input 
        if (letter == '\r') {
            password[i] = '\0';
            printf("\nPress ENTER to continue!\n");
            break;
        }//if
         //If the user enters backspace delete the last char
         /*else if (letter == '\p') {
         i--;
         password[i - 1] = '\0';
         }//else if*/
        password[i] = letter;
        printf("*");
    }//for

    if (!(strcmp(user, username) && strcmp(pass, password))) {
        allGood = 0;
    }//if the inputted credentials & the actual match set all good to 0
} while (allGood == 1);
fflush(stdin);
getch();

}//Login

  • Unrelated to your problem, but you should definitely learn about [the `switch` statement](http://en.cppreference.com/w/c/language/switch). – Some programmer dude Apr 12 '16 at 10:49
  • 2
    Also, `scanf("%", username);`? – Some programmer dude Apr 12 '16 at 10:50
  • `scanf("%", username);` ?? or `scanf("%s", username);` – Gopi Apr 12 '16 at 10:50
  • As for your problem, there must be hundreds if not thousands of duplicates, but I just can't seem to find them. :/ To help you help yourself though, I suggest you step through the code line by line in a debugger, and actually *check* what character the first call to `getch` will return. Hint: What key did you press to end the input for the user-name? What character do that key correspond to? Where do you think that character is after you read the user-name? – Some programmer dude Apr 12 '16 at 10:52
  • 1
    @JoachimPileborg Perhaps you are thinking of questions like [Simple C scanf does not work?](http://stackoverflow.com/questions/3744776/simple-c-scanf-does-not-work) – kaylum Apr 12 '16 at 10:54
  • @kaylum Not a perfect match, but very close. :) – Some programmer dude Apr 12 '16 at 10:55
  • Hi Joachim, thanks for the reply, I am farmilair with the switch statement but decided to use if else if statements in this as I use switches throughout the project. I just use these to display knowledge of how to use these as this is a college project. I did not notice my overslight when taking in the username. But yes that solved the issue. Thank you very much. –  Apr 12 '16 at 10:55

0 Answers0