Thread: My setup() function is being skipped.

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    33

    My setup() function is being skipped.

    Why is my setup() function skipped after the login_process() function is finished running? I am not able to go through and answer the questions specified by the setup() function?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <time.h>
    #include <inttypes.h>
    
    #define __STDC_FORMAT_MACROS
    
    void startup_message();
    void login_process();
    void setup();
    void menu_and_startup();
    void add_transferees();
    void view_transferees();
    void remit_money();
    void time_display();
    void display_information();
    void thank_you_message();
    
    int main ()
    {
        startup_message();
    
        login_process();
    
        setup();
    
        menu_and_startup();
    
        return 0;
    }
    
    void startup_message()
    {
        printf("Welcome to the newly introduced smsTransfer© (remittances through SMS text messaging)!\n");
        printf("a joint project of PioneerBank and Globe Telecom\n");
    
        printf("NOTES:\n");
        printf("1. Both the transferers and the transferees must have active accounts with both companies to take part in this service.\n");
        printf("2. If this is your first time using the service, please add your account information immediately after logging into this service.\n\n");
    }
    
    void login_process()
    {
        char username[10];
        char correct_username[10];
        char second_part_of_correct_username[5];
        char firstname[10];
        char lastname[20];
        uint64_t cin;
        uint64_t correct_password;
        uint64_t password;
        short int attempts=0;
    
        printf("Enter your first name:\n");
        scanf("%10s",firstname);
    
        strncpy(correct_username,firstname,2);
        correct_username[2]='\0';
    
        printf("Enter your last name:\n");
        scanf("%20s",lastname);
    
        strncpy(second_part_of_correct_username,lastname,3);
        second_part_of_correct_username[3]='\0';
        strncat(correct_username,second_part_of_correct_username,5);
    
        printf("Enter your Customer Identification Number(CIN):\n");
        scanf("%"PRIu64"",&cin);
        correct_password=(cin%10)+1;
    
        printf("Please note that usernames are case sensitive.\n");
        printf("Enter username:\n");
        scanf("%10s",username);
    
        printf("Enter password:\n");
        scanf("%"PRIu64"",&password);
    
        if ((password==correct_password) && strcmp(username,correct_username)==0)
            {
                printf("Login successful!\n");
            }
        if ((password==correct_password) && strcmp(username,correct_username)==0)
            {
                FILE *file;
                file=fopen("account_information.txt","a");
                fprintf(file,"%10s\t%"PRIu64"\t",username,password);
                fclose(file);
            }
    
        while ((password!=correct_password) || strcmp(username,correct_username)!=0)
            {
                printf("\nThe login details are invalid.Please try again.\n");
    
                printf("Enter username:\n");
                scanf("%10s",username);
    
                printf("Enter password:\n");
                scanf("%"PRIu64"",&password);
    
                attempts++;
    
                if (attempts>=5)
                    printf("Too many failed login attempts. Please re-enter the program and attempt to complete the login process again.\n");
                if (attempts>=5)
                    exit(0);
            }
    }
    
    void setup()
    {
        char response;
        float current_balance;
        printf("Is this your first time using this service?\n");
        printf("Enter 'y' for yes or 'n' for no.\n");
        scanf("%c",&response);
        if (response=='y')
            printf("Enter the desired amount from the chequing account to be accessible by this service:$\n");
        if (response=='y')
            scanf("%f",&current_balance);
        else
            current_balance=0;
    
        printf("%.2f",current_balance);
    }
    
    void menu_and_startup()
    {
        int option;
    
        system("cls");
    
        do
        {
        printf("\nMAIN MENU\n");
        printf("1. Add transferees\n");
        printf("2. View current transferee list\n");
        printf("3. Remit money\n");
        printf("4. Account information and settings\n");
        printf("5. Exit the application\n\n");
        printf("Please choose from the above options: \n");
        scanf("%d",&option);
        switch(option)
            {
                case 1: system("cls");
                        add_transferees();
                        break;
                case 2: system("cls");
                        view_transferees();
                        break;
                case 3: system("cls");
                        remit_money();
                        break;
                case 4: system("cls");
                        view_transferees();
                        break;
                case 5: system("cls");
                        thank_you_message();
                        exit(0);
                default: printf("Please enter a valid choice from 1-5, inclusive.\n");
                         break;
            }
        } while(option>0 && option<6);
    }
    
    void add_transferees()
    {
      FILE *file;
      int i;
      char firstName[32];
      char lastName[32];
      char email_address[40];
      uint64_t account_number;
      uint64_t phone_number;
      int addchoice;
    
      file = fopen("transferee_list.txt", "a");
      if (!file)
      {
        printf("File could not be opened\n\a\a");
        getchar();
        exit(-1);
      }
    
      printf("Enter number of transferees you wish to add:\n");
      scanf("%d",&addchoice);
    
      for (i = 0; i<addchoice; ++i)
      {
        printf("Transferee #%d\n", i+1);
    
        printf("Enter first name:\n"); scanf("%32s", firstName);
        printf("Enter last name:\n"); scanf("%32s", lastName);
        printf("Enter PioneerBank account number:\n"); scanf("%"PRIu64"", &account_number);
        printf("Enter e-mail address:\n"); scanf("%40s", email_address);
        printf("Enter Globe Telecom phone number:\n"); scanf("%"PRIu64"",&phone_number);
        printf("\n");
    
        fprintf(file, "%32s\t%32s\t%"PRIu64"\t%40s\t%"PRIu64"\n", firstName, lastName,account_number,email_address,phone_number);
      }
    
      printf("Transferee list successfully updated!\n");
    
      fclose(file);
    }
    
    void view_transferees()
    {
      char firstName[32];
      char lastName[32];
      char email_address[40];
      uint64_t account_number;
      uint64_t phone_number;
      int counter=0;
      int found=0;
    
      FILE *file;
      file = fopen("transferee_list.txt", "r");
      if (!file)
      {
        printf("File could not be opened\n\a\a");
        exit(-1);
      }
    
      while (fscanf(file, "%32s\t%32s\t%"PRIu64"\t%40s\t%"PRIu64"\n", firstName,lastName,&account_number,email_address,&phone_number)==5)
        {
          printf("Transferee #%d:\n First Name:%32s\n Last Name:%32s\n Account Number:%"PRIu64"\n E-mail address:%s\n Phone Number:%"PRIu64"\n", counter, firstName, lastName, account_number, email_address, phone_number);
    
          found = 1;
        }
    
      if (!found)
        printf("Record could not be found");
    
      fclose(file);
    }
    
    void remit_money()
    {
        double current_balance;
        double transfer_amount;
        char transferee_name[70];
    
        printf("\nCurrent smsTransfer© balance:%.2f",current_balance);
        printf("Please enter the transferee's legal name (first & last only):");
        fgets(transferee_name,60,stdin);
        printf("Please enter the transferee's phone number:");
        printf("\nEnter the desired transfer amount:$");
        scanf("%lf",&transfer_amount);
    
        if (transfer_amount<5000 && transfer_amount<current_balance && current_balance-transfer_amount>=10000)
            current_balance-=transfer_amount;
        if (transfer_amount<5000 && transfer_amount<current_balance && current_balance-transfer_amount>=10000)
            printf("Transaction approved! Your current balance is now $%.2f",current_balance);
        if (transfer_amount<5000 && transfer_amount<current_balance && current_balance-transfer_amount>=10000)
            time_display();
    
        while (transfer_amount>=5000)
        {
            printf("In accordance with the Mobile Banking Act of 2024, customers are prohibited from sending amounts in excess of $1,000 per transaction.");
            printf("\nEnter the desired transfer amount:$");
            scanf("%lf",&transfer_amount);
    
            if (transfer_amount<5000 && transfer_amount<current_balance && current_balance-transfer_amount>=10000)
                    current_balance-=transfer_amount;
            if (transfer_amount<5000 && transfer_amount<current_balance && current_balance-transfer_amount>=10000)
                    printf("Transaction approved! Your current balance is now $%.2f",current_balance);
            if (transfer_amount<5000 && transfer_amount<current_balance && current_balance-transfer_amount>=10000)
                    time_display();
        }
    }
    
    void time_display()
    {
       time_t rawtime;
       struct tm *info;
       char buffer[80];
    
       time(&rawtime);
    
       info = localtime(&rawtime);
    
       strftime(buffer,80,"%A %B %d,%Y %H:%M (%Z)", info);
       printf("Transaction completed on: %s\n", buffer);
    }
    
    void thank_you_message()
    {
        printf("Thanks for choosing and using smsTransfer©!\n");
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    "%c" will read '\n' left after you enter password in the input stream

    use " %c" to skip whitespaces before you start reading character from the stream
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    as a PS
    you need to fix all your errors and warnings before running program
    Code:
    gcc -c -o obj/test.o src/test.c -Wall -pedantic -march=core2 -Iinclude -std=c99
    src/test.c: In function ‘remit_money’:
    src/test.c:244:11: warning: ‘current_balance’ is used uninitialized in this function [-Wuninitialized]
         printf("\nCurrent smsTransfer© balance:%.2f",current_balance);
               ^
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Apr 2015
    Posts
    33
    Thanks for your response! I should have noticed the problem about the newline character and the scanf function! I have also fixed the unintialized variable. Initially, I just wasn't sure how I would use this variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My getline function is skipped whenver I use case 10.
    By howardbc14 in forum C++ Programming
    Replies: 2
    Last Post: 05-17-2015, 12:12 AM
  2. My setup() function is skipped.
    By howardbc14 in forum C Programming
    Replies: 3
    Last Post: 05-14-2015, 11:29 PM
  3. gets function skipped 1st time
    By ambrown782 in forum C Programming
    Replies: 6
    Last Post: 07-26-2014, 12:30 AM
  4. gets() skipped!
    By ncode in forum C Programming
    Replies: 14
    Last Post: 02-06-2012, 05:01 PM
  5. Unsure of how to setup this function
    By Khem101 in forum C Programming
    Replies: 4
    Last Post: 10-03-2006, 07:32 AM