Hi,
I am making a long integer arthimetic calculator that performs +and*. I am adding an element and then printing the node.The problem I am having is that I can the value I inserted but I also get random garbage in front that I never inserted.
For example if I insert 10 I get 0 -38 10
My code is:
Code:
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <ctype.h>
#define FALSE 0
typedef struct node
{
int integerone;
struct node * link;
} list_head;
/* Printing the list Contents */
void printList(list_head*head){
list_head*temp=head;
while(temp){
printf("%d " , temp->integerone);
temp=temp->link;
}
}
void Menu(int *choice) {
char local;
printf("\n1.Insert first number\n");
printf("2. Insert second number\n");
printf("3. Add numbers\n");
printf("4. Multiply numbers\n");
printf("5. Process file\n");
printf("6. Quit\n");
printf("Enter Option: ");
do {
local = getchar ();
} while (isdigit ((unsigned char) local) == FALSE);
*choice = (int) local - '0';
}
int insert_to_list(list_head**head,int digit){
list_head *tmp;
tmp=NULL;
tmp = malloc(sizeof(list_head));
tmp->integerone=digit;
tmp->link=NULL;
if(*head==NULL){
*head=tmp;
}
else{
list_head*temp=*head;
while(temp->link!=NULL){
temp=temp->link;}
temp->link=tmp;
return *head;
}
}
//allocate memory for a new list header
list_head *create_list(void){
list_head*p=NULL;
p=malloc(sizeof(list_head));
p->link=NULL;
return p;
}
int main(void){
list_head *head=NULL;
int choice;
char c;
head=create_list();
do {
Menu (&choice);
switch (choice) {
case 1:
printf("1. Insert first number\n");
scanf("%c",&c);
do{
head=insert_to_list(&head,c-'0');
}while((c = getchar()) != '\n');
break;
case 2:
printf("2.print list\n");
printList(head);
break;
case 3:
printf("3. Add numbers\n");
break;
case 4:
break;
printf("4. Multiply numbers\n");
case 5:
printf("5. Process file\n");
break;
case 6:
break;
default:
printf ("Invalid menu choice - try again\n");
break;
}
} while (choice != 6);
}