Click here to Skip to main content
15,902,817 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Boolean variables keeps changing to false even though I set them as true

What I have tried:

C++
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

#include "types.h"
#include "rental.h"
#include "sort.h"

bool isNumber(char strParam[])
{
    for( int i = 0; strParam[i] != '\0'; i++ ) {
	if( isdigit(strParam[i]) == 0 ) {
	    return false;
	}
    }

    return true;
}

bool readProperties( char *strFilename, RentalProperty **ppProperties )
{
    char strLine[512];
    char strArgument[512];
    char strRemainder[512];
    char comma;

    bool isStreetExist;
    bool isNumExist;
    bool isRentExist;
    bool isRoomExist;

    FILE *fileIn = fopen( strFilename, "r" );
    if( fileIn == NULL ){
	perror( strFilename ); 
	return false;
    }

    int rc = 0; 
    int propertyCounter = countProperties( *ppProperties );
    while ( !feof( fileIn ) ) 
    {
	isStreetExist = false;
	isNumExist = false;
	isRentExist = false;
	isRoomExist = false;

	fgets( strLine, 512, fileIn );
	*ppProperties = realloc( *ppProperties, ( propertyCounter + 1 ) * sizeof( RentalProperty ) );

	do {
	    rc = sscanf( strLine, "%[^,]%[,] %[^\n]", strArgument, &comma, strRemainder );
	    strcpy( strLine, strRemainder );
	    if( strncmp( "streetName=", strArgument, 11 ) == 0 ) {
		isStreetExist = true;
		( *ppProperties )[ propertyCounter ].streetName = ( char* )malloc(sizeof ( char ) );
		strcpy( ( *ppProperties )[ propertyCounter].streetName, strArgument + 11 );  
	    } else if ( strncmp( "streetNumber=", strArgument, 13 ) == 0 ) {
		if( isNumber( strArgument + 13 ) == false){
		    fprintf( stderr, "\nError: %s - Invalid Argument", strArgument);
		    return false;
		}

		( *ppProperties )[ propertyCounter ].streetNumber = atoi( strArgument + 13 );
		isNumExist = true;
	    } else if ( strncmp( "rent=", strArgument, 5 ) == 0 ) {
		if( isNumber( strArgument + 5 ) == false){
		    fprintf( stderr, "\nError: %s - Invalid Argument", strArgument);
		    return false;
		}

		( *ppProperties )[ propertyCounter ].rent = atoi( strArgument  + 5 );
		isRentExist = true;
	    } else if ( strncmp( "rooms=", strArgument, 6 ) == 0 ) {
		if( isNumber( strArgument + 6 ) == false){
		    fprintf( stderr, "\nError: %s - Invalid Argument", strArgument);
		    return false;
		}

		( *ppProperties )[ propertyCounter ].rooms = atoi( strArgument + 6 );
		isRoomExist = true;
	    } else {
		fprintf( stderr, "\nWarning: %s - Unknown argument", strArgument);
	    }

	} while( rc >  1 );
        
        if( isStreetExist || isNumExist || isRentExist || isRoomExist ){
          //SOME CODES
        }
	propertyCounter++;

    }

    *ppProperties = realloc( *ppProperties, ( propertyCounter ) * sizeof( RentalProperty ) );
    (*ppProperties)[propertyCounter - 1].rent = -1;

    *ppProperties = &(*ppProperties)[0]; 

    fclose( fileIn );
    return true;
}
Posted
Updated 2-Apr-18 21:22pm
v2
Comments
Patrice T 2-Apr-18 20:39pm    
Tell where this happen.

1 solution

You should learn to use the debugger. That tool will help you when and why your bools go crazy.

Your bools are local variables and so are overwritten on every loop run.

Check that the realloc has no negative side effects. I would calculate the count of properties out of the file size dividend by property size and allocate it first.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900