Question: I ' m trying to run some test cases as you can see below for my isValidPostalCode function but for some reason it keeps failing

I'm trying to run some test cases as you can see below for my isValidPostalCode function but for some reason it keeps failing with the code C0000005 I need some help figuring this out to help get my test to pass. its telling me that the problem is within my isValidPostalCode function particularly this line of code postalCode[i]= toupper(postalCode[i]) so I need some help to figure it out.
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include "customer.h"
struct Customer getCustomerInfo(void){
struct Customer customer;
printf("Enter your first name: ");
fgets(customer.firstName, sizeof(customer.firstName), stdin);
customer.firstName[strcspn(customer.firstName, "
")]='\0';
while (1){
printf("Enter your last name: ");
fgets(customer.lastName, sizeof(customer.lastName), stdin);
customer.lastName[strcspn(customer.lastName, "
")]='\0';
if (strlen(customer.lastName)>0){
break;
}
else {
printf("Invalid Entry: ");
}
}
printf("Enter your street address: ");
fgets(customer.address, sizeof(customer.address), stdin);
customer.address[strcspn(customer.address, "
")]='\0';
printf("Enter your city: ");
fgets(customer.city, sizeof(customer.city), stdin);
customer.city[strcspn(customer.city, "
")]='\0';
printf("Enter your province: ");
fgets(customer.province, sizeof(customer.province), stdin);
customer.province[strcspn(customer.province, "
")]='\0';
int c;
while ((c = getchar())!='
' && c != EOF);
while (1){
printf("Enter your postal code: ");
fgets(customer.postalCode, sizeof(customer.postalCode), stdin);
customer.postalCode[strcspn(customer.postalCode, "
")]='\0';
if (isValidPostalCode(customer.postalCode)){
break;
}
else {
printf("Invalid Entry: ");
}
}
return customer;
}
int isValidPostalCode(char postalCode[8]){
int i;
for (i =0; i <6; i++){
postalCode[i]= toupper(postalCode[i]);
}
if (isalpha(postalCode[0]) && isdigit(postalCode[1]) && isalpha(postalCode[2]) &&
postalCode[3]=='' &&
isdigit(postalCode[4]) && isalpha(postalCode[5]) &&
strlen(postalCode)==7){
return 1;
}
else {
return 0;
}
}
int main(void){
struct Customer customer = getCustomerInfo();
printf("
You entered:
%s %s
%s,
%s,%s,
%s
",
customer.firstName, customer.lastName,
customer.address,
customer.city, customer.province,
customer.postalCode);
return 0;
}
#pragma once
#ifndef CUSTOMER_H
#define CUSTOMER_H
struct Customer {
char firstName[50];
char lastName[50];
char address[100];
char city[50];
char province[3];
char postalCode[8];
};
int isValidPostalCode(char postal_code[8]);
struct Customer getCustomerInfo(void);
#endif
#include "pch.h"
#include "CppUnitTest.h"
#include "CustomerTest_r.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
TEST_CLASS(CustomerInfoTest_Blackbox)
{
public:
TEST_METHOD(BlackBox_ValidPostalCode)
{
Assert::IsTrue(isValidPostalCode("K1M 2G9"));
}
TEST_METHOD(BlackBox_InvalidLength)
{
Assert::IsFalse(isValidPostalCode("A1B 2R4X"));
}
TEST_METHOD(BlackBox_ExtraSpace)
{
Assert::IsFalse(isValidPostalCode("A1B 2R4"));
}
TEST_METHOD(BlackBox_MissingSpace)
{
Assert::IsFalse(isValidPostalCode("A1B2R4"));
}
TEST_METHOD(BlackBox_AlphanumericCharacters)
{
Assert::IsFalse(isValidPostalCode("A1B 2R4X"));
}
TEST_METHOD(BlackBox_SpecialCharacters)
{
Assert::IsFalse(isValidPostalCode("A1B 2R4#"));
}
};
TEST_CLASS(CustomerInfoTest_Whitebox)
{
public:
TEST_METHOD(WhiteBox_UppercaseConversion)
{
Assert::IsTrue(isValidPostalCode("a1b 2r4"));
}
TEST_METHOD(WhiteBox_EmptyString)
{
Assert::IsFalse(isValidPostalCode(""));
}
TEST_METHOD(WhiteBox_NullPointer)
{
Assert::IsFalse(isValidPostalCode(nullptr));
}
TEST_METHOD(WhiteBox_IndividualChecks_NumericOnly)
{
Assert::IsFalse(isValidPostalCode("123456"));
}
TEST_METHOD(WhiteBox_IndividualChecks_AlphabeticOnly)
{
Assert::IsFalse(isValidPostalCode("ABC DEF"));
}
TEST_METHOD(WhiteBox_IndividualChecks_SpaceOnly)
{
Assert::IsFalse(isValidPostalCode(""));
}
TEST_METHOD(WhiteBox_LongInvalidPostalCode)
{
Assert::IsFalse(isValidPostalCode("M1A 1A1X123"));
}
TEST_METHOD(WhiteBox_CaseSensitivity_Uppercase)
{
Assert::IsTrue(isValidPostalCode("a1b 2r4"));
}
TEST_METHOD(WhiteBox_CaseSensitivity_Lowercase)
{
Assert::IsTrue(isValidPostalCode("A1B 2R4"));
}
};

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!