Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have been working in Visual C++ 2008 & puTTY to figure out how to make this wo

ID: 3620838 • Letter: I

Question

I have been working in Visual C++ 2008 & puTTY to figure out how to make this work. puTTY keeps giving me segmentation errors; Visual C++ gives me the following:
"Unhandled exception at 0x10296af2 in ForProj3.exe: 0xC0000005: Access violation writing location 0x00000000."
(Note: select 5 at the switch statement)

Here is where Visual C++ brings me to (lines 177-179, I tried to flag in the code text below; extra comments were removed):

fscanf(dataPtr, "%d",aCtr);
printf("%d read from file ",aCtr);
AddToList( &sPtr, aCtr );

What is causing the segmentation error? I'm a C rookie and have tried everything i can find in the book, online, etc. I've rebuilt this project from the ground up; it worked when i was manually entering values (option 1 at the switch statement), but doesn't input the Input.txt (just a list of random numbers) into the dynamic list. Any help is greatly appreciated.


/* Started with textbook example, Fig 12.3: fig12_o3.c
Operating and maintaining a list
C How to Program, Deitel & Deitel, 4th ed. Pgs 471-474
Grossly expanded to develop a working solution for Proj3, dynamic lists */
#include <stdio.h>
#include <stdlib.h>
// #include "DynList.h"

/* *************************************** to move to DynList.h *****************************************************/
//self-referential structure
struct listNode
{
int data; //each listNode contains an integer
struct listNode *nextPtr; //pointer to next node
}; //end structure listNode

typedef struct listNode ListNode; //synonym for struc listNode
typedef ListNode *ListNodePtr; //synonym for listNode
typedef ListNodePtr List; //the 'List' will be a pointer to the first node...

//prototypes:
void openFile(void); //open input file
List CreateList(void); //function to create the pointer to the first node in the list...
void AddToList( ListNodePtr *sPtr, int value );
int RemoveFromList( ListNodePtr *sPtr, int value ); //***************** is this a cpp thing? reserved in C? "delete" appeared blue, so i changed it to "del"
int isEmpty( ListNodePtr sPtr );
void printList( ListNodePtr currentPtr );
void instructions( void );
void DeleteList(ListNodePtr *sPtr);
int EndofList(ListNodePtr currentPtr, int ); //to be called by another function to determine if the node referenced is the last node in the list

/* **************************************end of to move to DynList.h *****************************************************/

int main()
{
ListNodePtr startPtr = NULL; //initially there are no nodes
int choice; //user's choice
int num; //int entered by user

instructions(); //display menu
printf("? ");
scanf( "%d", &choice );

//looop while user does not choose 3
while( choice != 7 )
{
switch ( choice )
{
case 1:
printf( "Enter an integer: " );
scanf( " %c", &num );
AddToList( &startPtr, num ); //insert num in list
printList( startPtr );
break;

case 2:
//if list is not empty
if( !isEmpty( startPtr ))
{
printf( "Enter integer to be deleted: ");
scanf( " %c", &num );

//if integer is found, remove it
if( RemoveFromList( &startPtr, num )) //remove num
{
printf( "%c deleted. ", num);
printList( startPtr );
} //end if
else
{
printf( "%c not found. ", num );
} //end else

} //end if ( !isEmpty( startPtr ))
else
printf( "List is empty. " ); //end else
break;

case 3:
//if list is not empty
if( !isEmpty( startPtr ))
{
DeleteList( &startPtr );
printf("The list was deleted. ");
}
else
printf("List is empty. " );
break;

case 4:
if( !isEmpty( startPtr ))
{
printf( "Enter integer to determine if it is the end of the list: ");
scanf( " %c", &num );
printf(" ");

//if integer is found, print if it is the end of the list
if( EndofList( startPtr, num ) == 1 ) //function requires a bit more work...
{
printf( "%c was at the end of the list. ", num);
printList( startPtr );
} //end if
else
{
printf( "%c was NOT the last value of the list. ", num);
} //end else

} //end if ( !isEmpty( startPtr ))
else
printf( "List is empty. " ); //end else
break;

case 5:
openFile();
break;

case 6:
printList( startPtr );
break;


default:
printf( "Invalid choice. " );
instructions();
break;
} //end switch

printf( "? " );
scanf( "%d", &choice );
}//end while

printf( "End of run. " );

return 0; //indicate successful termination
} //end main


//*****************************************to go to DynList.c ***********************************************************************************
//display program instructions to user
void instructions ( void )
{
printf( "Enter your choice: "
" 1 to insert an element into the list. "
" 2 to delete an element from the list. "
" 3 to delete the entire list. "
" 4 to determine if the requested integer is the end of the list. "
" 5 to open Input.txt file. "
" 6 to print the list. "
" 7 to end. " );
} //end functions instructions(void)

//insert a new value into the list in sorted order
void openFile( void )
{
FILE *dataPtr; //pointer to read-in file
ListNodePtr *sPtr; // previously initialized this to NULL; what was passed in the function was already initialized to NULL //Also tried to add List to this - added to point to the first node; still did not work
int aCtr = 0; //temp variable for reading from Input.txt

//open Data, test to see if it opened correctly
if((dataPtr = fopen("Input.txt","r")) == NULL )
{
printf( "File could not be opened, terminating program. " );
system("pause");
exit(1); //terminate
}//end if(fopen)
else
printf("Input file opened correctly ");

sPtr = CreateList(); //what do we do with sPtr? are we to use List from here on out?
printf("CreateList completed successfully ");

// (*sPtr)->nextPtr = List;
//(*sPtr)->nextPtr = NULL;
//sPtr = malloc( sizeof( ListNode ) ); //initialize sPtr; reserve memory for this pointer...
do
{ //***********************here is the problem line 177*******************
fscanf(dataPtr, "%d",aCtr);//scan data from Data to aCtr //removed & from &aCtr --> fscanf(dataPtr, "%d",aCtr); based on what i read on a post; also tried if(1==fscanf());; same error - unitialized local variable sPtr
printf("%d read from file ",aCtr); //indicate what value was read from the file
AddToList( &sPtr, aCtr ); //calling the function to add the variable to the list
//fscanf(dataPtr,"%d",&aCtr); //***************************************try scanning as part of the do:while function
}while (!feof(dataPtr)); //p 434: "while (!feof(stdin)){}" for similar purpose
/* printf(" sPtr->data: %d"
" sPtr->nextPtr: %p ",(*sPtr)->data, (*sPtr)->nextPtr);
system("pause");
printList( sPtr ); //testing****************************************/
fclose(dataPtr); //close input file when finished inputing data
printf("File closed. ");
}//end function openFile()


List CreateList() //create a pointer to an empty node
{
ListNodePtr sPtr = NULL; //pointer to first node in list

sPtr = malloc( sizeof( ListNodePtr ) ); //create node
printf(" In CreateStack "
" startPtr created ");

free(sPtr);

return NULL;
}//end CreateList()


void AddToList(ListNodePtr *sPtr, int value )
{
ListNodePtr newPtr; //pointer to new node
ListNodePtr previousPtr; //pointer to previous node in list
ListNodePtr currentPtr; //pointer to current node in list

newPtr = malloc( sizeof( ListNode ) ); //create node

if ( newPtr != NULL ) // is space available?
{
//if((*sPtr)->nextPtr == NULL)// (*sPtr)->nextPtr = &newPtr; //set the nextPtr of sPtr equal to the address of the newPtr

// (*sPtr)->nextPtr = &newPtr; //set the nextPtr of sPtr equal to the address of the newPtr
newPtr->data = value; //place value in node
newPtr->nextPtr = NULL; //ensure the node does not link to another node

previousPtr = NULL; //initilize placeholder for the previous node to NULL
currentPtr = *sPtr; //current pointer gets the address from the startPtr (newly added node)

/* printf("value read from file is %d ", value); //read value: 5
printf("newPtr->data was set to %d ", newPtr->data); //newPtr->data: 5
printf("newPtr->nextPtr was set to %d ", newPtr->nextPtr); //newPtr->nextPtr: 0
y
printf("previousPtr was set to %d ", previousPtr); //previousPtr: 0
printf("sPtr was set to %p ", &sPtr); //sPtr: jiberish... sPtr needs the address of the first node.
printf("sPtr->data was set to %d ", (*sPtr)->data); //sPtr->data: breaks here....
printf("currentPtr was set to %d ", currentPtr->data); //currentPtr: error
printf("currentPtr->data was set to %d ", currentPtr->data); //currentPtr->data: error?
system("pause");
exit(1);
*/
//loop to find the correct location in the list *********************************this chould be the mechanism for sorting....***********************************
while ( currentPtr != NULL && value > currentPtr->data ) //start with the newly added node, search for the first value in the list that is > the new value
{
previousPtr = currentPtr; //walk to .... //set the prevPtr placeholder to the next node if it is < new value
currentPtr = currentPtr->nextPtr; //... next node //set the currPtr placeholder to the current node's nextPtr to complete 'walking' to the next node
} //end while ( currentPtr != NULL && value > currentPtr->data ) //the appropriate location has been found

printf("current value: %d ",currentPtr);

//insert new node at beginning of list if value is less than the value in the first node of the list
if( previousPtr == NULL) //this only happens if the newly added value is the lowest value in the existing list
{
newPtr->nextPtr = *sPtr; //set the added node's nextPtr to the address of the first node in the list
*sPtr = newPtr; //change the starting address of the list to the newly added node which contains the now smallest value
} //end if( previousPtr == NULL)
else // insert new node between previousPtr & currentPtr
{
previousPtr->nextPtr = newPtr; //goes to the address of the containing the next smaller value; changes the address in it's nextPtr to the added node
newPtr->nextPtr = currentPtr; //goes to the added node's nextPtr; adds the address of the next node to the added node's nextPtr
} //end else

} // end if ( newPtr != NULL )
else
{
printf( "%c not inserted. No memory available. ", value );
} //end else to if ( newPtr != NULL )

} //end function AddToList()

//delete a list element
int RemoveFromList( ListNodePtr *sPtr, int value )
{
ListNodePtr previousPtr; //pointer to previous node in list
ListNodePtr currentPtr; //pointer to current node in list
ListNodePtr tempPtr; //temporary node pointer - hangs on to address of node to be removed for free()

//delete first node if the value to be deleted is the first node in the list
if(value == ( *sPtr )->data ) //compares the value to be deleted to the value in the first node of the list
{
tempPtr = *sPtr; //hold onto node being removed
*sPtr = ( *sPtr )->nextPtr; //de-thread the node - set the address for the start of the list to the second node in the list
free( tempPtr ); //free the de-threaded node
return value;
} //end if(value == ( *sPtr )->data )
else{
previousPtr = *sPtr; //set the prevPtr to the address of the start of the list for the walking through the list while loop
currentPtr = ( *sPtr )->nextPtr; //set the 'current' pointer to the address of the second node in the list

//loop to find the currect location in the list
while ( currentPtr != NULL && currentPtr->data != value ) //walk through the list until the node containing the variable to be removed is located
{
previousPtr = currentPtr; //walk to ... //set the address of the 'current' node to the prevNode placeholder
currentPtr = currentPtr->nextPtr; // ... next node //set the address of the 'next' node to the currentNode placeholder
} //end while ( currentPtr != NULL && currentPtr->data != value )

//delete node at currentPtr
if( currentPtr != NULL ) //as long as the end of the list wasn't reached...
{
tempPtr = currentPtr; //save the address of the node to be removed
previousPtr->nextPtr = currentPtr->nextPtr; //set the previous node's next pointer to the current pointer's next pointer - the node to be removed is no longer referenced by this list
free( tempPtr ); //free the memory of the node containing the value to be deleted
return value;
} // end if( currentPtr != NULL )
} //end else

return '';
} //end function del ()


//delete the entire list
void DeleteList( ListNodePtr *sPtr )
{
// ListNodePtr currentPtr; //pointer to current node in list ******not used in DeleteList();
ListNodePtr tempPtr; //temporary node pointer - hangs on to address of node to be removed for free()

while( ( *sPtr ) != NULL )
{
tempPtr = *sPtr; //set address to the start of the list
*sPtr = ( *sPtr )->nextPtr; //change the address in the pointer to the first node of the list to the second node
free(tempPtr);
} //end while; the end of the list has been reached & the only thing that remains is the pointer to an empty list

} //end DeleteList()
//Return 1 if the list is empty, 0 otherwise
int isEmpty( ListNodePtr sPtr)
{
return sPtr == NULL;
}// end function isEmpty( ListNodePtr sPtr)

//Print the list
void printList( ListNodePtr currentPtr)
{
//if list is empty
if( currentPtr == NULL)
{
printf( "The list is empty. " );
} //end if( currentPtr == NULL)
else
{
printf( "The list is: " );

//while not at end of list
while ( currentPtr != NULL )
{
printf( "%c --> ", currentPtr->data );
currentPtr = currentPtr->nextPtr;
} //end while ( currentPtr != NULL )

printf( "NULL " );
} //end else to if( currentPtr == NULL)

} //end function printList( ListNodePtr currentPtr)

int EndofList(ListNodePtr sPtr, int value )
{
ListNodePtr previousPtr; //pointer to previous node in list
ListNodePtr currentPtr; //pointer to current node in list

int response = 0;

if ( sPtr->data == value )
{
if ( sPtr->nextPtr == NULL )
response = 1;
else
response = 0;
}
else
{
previousPtr = sPtr; //set the prevPtr to the address of the start of the list for the walking through the list while loop
currentPtr = sPtr->nextPtr; //set the 'current' pointer to the address of the second node in the list

while ( currentPtr != NULL && currentPtr->data < value ) //walk through the list until the node containing the variable to be removed is located
{
previousPtr = currentPtr; //walk to ... //set the address of the 'current' node to the prevNode placeholder
currentPtr = currentPtr->nextPtr; // ... next node //set the address of the 'next' node to the currentNode placeholder
} //end while ( currentPtr != NULL && currentPtr->data != value )
if ( value == currentPtr->data )
{
if ( currentPtr->nextPtr == NULL )
response = 1;
}
else
response = 0;
}
return response;
}//end of function EndofList()

Explanation / Answer

The third argument for fscanf should be a pointer not a value. Change fscanf(dataPtr, "%d",aCtr); to fscanf(dataPtr, "%d",&aCtr); and you'll be fine. Also, you should be receiving several compiler warnings about type mismatches. You should fix these or else use a cast to change between types. If you are not getting these warnings you may need to dig around the VS help file files to find out how to turn them on.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote