Jump to content

More code help...

duckwithanokhat

So I got this block of code in C:

 

EDIT: Its jobs is to calculate store credit and to see if a "customer" has exceeded the maximum store credit.

#include <stdio.h>
#include <stdlib.h>
#define MAX_CRED 500.00
int main()
{
    //Declarations
    char name1, name2, name3[21];
    float start_bal1, start_bal2, start_bal3;
    float end_bal1, end_bal2, end_bal3;
    float payment1, payment2, payment3;
    float new_charge1, new_charge2, new_charge3;
    float cred_left1, cred_left2, cred_left3;
    int acc_num1, acc_num2, acc_num3;
    //Prompt + User Input
    puts("Please enter a single-digit account number");
    scanf("%i", &acc_num1);
    puts("Please enter the corresponding customer name.");
    scanf("%s", &name1);
    puts("Please enter the the corresponding customer's opening balance:");
    scanf("%f", &start_bal1);
    puts("Please enter the the corresponding customer's new charges:");
    scanf("%f", &new_charge1);
    puts("Please enter the the corresponding customer's payments:");
    scanf("%f", &payment1);

    //Calculations
    end_bal1 = start_bal1 + new_charge1 - payment1;
    cred_left1 = MAX_CRED - end_bal1;

    //Prompt + User Input
    puts("Please enter a single-digit account number");
    scanf("%i", &acc_num2);
    puts("Please enter the corresponding customer name.");
    scanf("%s", &name2);
    puts("Please enter the the corresponding customer's opening balance:");
    scanf("%f", &start_bal2);
    puts("Please enter the the corresponding customer's new charges:");
    scanf("%f", &new_charge2);
    puts("Please enter the the corresponding customer's payments:");
    scanf("%f", &payment2);

    //Calculations
    end_bal2 = start_bal2 + new_charge2 - payment2;
    cred_left2 = MAX_CRED - end_bal2;

     //Prompt + User Input
    puts("Please enter a single-digit account number");
    scanf("%i", &acc_num3);
    puts("Please enter the corresponding customer name.");
    scanf("%s", &name3);
    puts("Please enter the the corresponding customer's opening balance:");
    scanf("%f", &start_bal3);
    puts("Please enter the the corresponding customer's new charges:");
    scanf("%f", &new_charge3);
    puts("Please enter the the corresponding customer's payments:");
    scanf("%f", &payment3);

    //Calculations
    end_bal3 = start_bal3 + new_charge3 - payment3;
    cred_left3 = MAX_CRED - end_bal3;

    //Display results in table
    printf("%5i%20s%20.2f%20.2f", acc_num1, name1, end_bal1, cred_left1);
    printf("%5i%20s%20.2f%20.2f", acc_num2, name2, end_bal2, cred_left2);
    printf("%5i%20s%20.2f%20.2f", acc_num3, name3, end_bal3, cred_left3);
    
    //Codeblocks usually closes console window after finishing a run, this line stops that.
    system("pause");
    
    return 0;
}

And when I run it and input my values I get to the very last prompt ("Please enter customer's payments:" for variable "payment3") and press enter, I get "Program received signal SIGSEGV, segmentation failed." and a warning appears on line 63,

which is printf("%5i%20s%20.2f%20.2f", acc_num1, name1, end_bal1, cred_left1); . What is happening?

Link to comment
Share on other sites

Link to post
Share on other sites

The problem seems to be in the %s specifier in displaying the results for the table. Any idea why that might be?

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×