This thing is acting very, very strangely. It has some kind of bug that I can't seem to find and I'm eating up hours trying to find it. It seems it's taking anything that happens to be a selection from the menu (or, I guess, a case in my switch statement) and applying it as a selection. I attached a screenshot that shows this a little.
I select a for [A]dd an entry and am prompted with both of the printf lines in a row with no regard for the newline that's in the code for them. It just races past the first fgets, appartently, and prints the next line asking for the phone number. You enter somthing and press enter and just get a newline. You enter something again and press enter and it repeats (printing the request for both name and number - thought they are two separate printf lines in the code). If you happen to enter pp (maybe just p) it enters case 'p': and you see the prinf in that block of code printed to the screen.
I'm afraid I'll end up wasting what precious little time I have left on this crap. I'm still looking but I haven't seen anything that might be causing it. It reminds me of the thing I described before with my menu function. Where adding a newline to one of the printf in it had no effect on the output. Yes it's inside the parenthesis.
Code:
/*
Objective: Create a doubly linked list. Use a design in which it is the
current node that is used to create a new node or to delete. Prev and Next
are there for navigation and searching.
Tip:
Status: <Content censored>
Next steps:
*/
#include <ctype.h> //for tolower(), among other things?
#include <stdio.h>
#include <stdlib.h>
#include <string.h> //for strcopy
struct Node
{
char Name[32];
char Phone[18];
struct Node *Prev;
struct Node *Next;
};
struct Node *Curr = NULL;
void Menu(); //Done
//char ValidSelection(char S); //Done
void Insert(char N[], char P[]); //Done
//Delete();
//Search();
void Print(char Selection); //Done
int main(void)
{
Menu();
char Selection = ' ';
while(Selection != 'Q' || Selection != 'q')
{
scanf("%c", &Selection);
//ValidSelection(Selection);
switch(tolower(Selection))
{
case 'a':
{
//insert
char Name[32];
char Phone[18];
printf("Enter name... \n");
fgets(Name, sizeof(Name), stdin);
printf("Enter phone number... \n");
fgets(Name, sizeof(Name), stdin);
Insert(Name, Phone);
break;
}
case 'd':
{
//delete
break;
}
case 's':
{
//search
break;
}
case 'p':
{
//print
int SubSelection = 0; //initialize to zero?
printf("Would you like to: Print [A]ll or Print [E]ntry ?\n");
SubSelection = getchar();
//ValidSelection(Selection);
Print(Selection);
break;
}
} //end switch
} //end while
return 0;
} //end main
void Menu()
{
//display menu to user
printf("Please select one of the following...\n\n");
printf("[A]dd an entry\n");
printf("[D]elete an entry\n");
printf("[S]earch for an entry\n");
printf("[P]rint entries\n");
printf("[Q]uit\n");
printf("\n");
} //end function
//char ValidSelection(char S)
//{ //accept user's selection - include validation
// const char ValidSelection[13] = {'E', 'e', 'A', 'a', 'D', 'd', 'S', 's', 'P', 'p', 'Q', 'q', '\0'};
// int i;
// if(isalpha(S))
// {
// while(ValidSelection[i] != '\0')
//
// if(S == ValidSelection[i])
// {
// S = S;
// }
// else
// {
// printf("Invalid selection. Please enter your selection again...\n");
// printf("\n");
// }
// ++i;
// }
// }
// else
// {
// printf("Invalid Selection. Please enter your selection again...\n");
// printf("\n");
// }
// return S;
//} //end function
void Insert(char N[], char P[])
{
//create a new node with data entered by user
struct Node *Temp;
Temp = (struct Node *)malloc(sizeof(struct Node));
if(Temp != NULL)
{
strcopy(Temp->Name, *N);
strcopy(Temp->Phone, *P);
if(Curr == NULL)
{
Curr = Temp; //insert to curr - first entry
Curr->Prev = NULL; //make this the beginning of the list
Curr->Next = NULL; //make this the end of the list
}
else
{
Curr->Next = Temp;
Temp->Prev = Curr;
Curr = Temp;
Curr->Next = NULL;
}
}
} //end function
//Delete()
//{
//removwe an existing struct instance
//} //end function
//Search()
//{
//search for a specific, existing instance and print the entire instance
//} //end function
void Print(char Selection)
{
struct Node *Pointer = Curr;
if(Selection == 'A' || Selection == 'a') //user entered [A[ll)
{
//Print entire list
while(Pointer != NULL)
{
printf("%s", Pointer->Name);
printf("%d", *Pointer->Phone);
}
}
else if(Selection == 'E' || Selection == 'e')//user entered [E]ntry
{
//code to print a single entry - use search function call
}
else
{
printf("Invalid entry\n");
printf("Please enter your selection again...\n");
}
} //end function
---------------------
Edit:
Ta hell with it!
At every turn this stinkin thing bites me. Can't pass an array into the sob, acts completely bizare for no appartent reason that I can see. I completely started over on in another file, trying to get somwhere with this absolute horror of a program. I'm wasting my time with this little garbage crap and not even get to focus on the linked list at all.
Bookmarks