I've been working on this school assignment which creates a restaurant menu, and I have an array to store everything that a person orders. The array that stores the orders ordered is array OrderedItems [30]. This array is basically a counter. When a person orders order number 1 as an example OrderItems[1] increases by 1.
I've tried to initialize by using OrderedItems [30] = {0}, and using a for loop to initialize every spot individually, However, that didn't work. Please help me initialize this array. I've also tried memset(OrderedItems, 0, 30); and this didn't work too so I really have no clue what to do.
I also want to add that I've tried to globally declare the OrderedItems array since I've heard that all global declarations are automatically initialized to 0, but that also didn't work.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
FILE *fPointer1; //for food//
FILE *fPointer2; //for invoice//
int count;
char name[50];
float price;
void FunctionToPrintFoodItems (void)
{
fPointer1 = fopen ("Food.txt", "a+");
float price;
printf("ITEMCODE\tDESCIPTION\tPRICE (RM)\n");
while (!feof (fPointer1))
{
fscanf(fPointer1, "%d %s %f", &count, name, &price);
printf("%d\t\t%s\t\t%.2f\n", count, name, price);
}
fclose (fPointer1);
}
void clrscr()
{
system("#cls||clear"); //might not need. Will delete it not needed//
}
void debug (void)
{
printf("THIS IS PRINTED");
}
#define clear clrscr ();
#define printfood FunctionToPrintFoodItems();
#define de debug();
int main ()
{
fPointer1 = fopen("Food.txt", "w");
fprintf(fPointer1, "1 BigM 10.40\n");
fprintf(fPointer1, "2 Cheeseburger 9.45");
fclose (fPointer1);
int i;
int MainMenuCode;
int additems = 0;
int orderitems;
int item;
int ordered;
int OrderedItems[30] = {0};
memset(OrderedItems, 0, 30);
for (i=0 ; i < 30 ; i++)
{
OrderedItems[i] = NULL;
printf("%d: %d\n", i, OrderedItems);
}
do
{
printf("WELCOME TO RESTOURANT MAC C - Main Menu\n\n");
printf("[1] Add new food items\n\n");
printf("[2] Take order\n\n");
printf("Enter ITEM CODE (0 to Quit) : ");
scanf("%d", &MainMenuCode);
if (MainMenuCode == 1)
{
clear;
additems = 1;
printf("WELCOME TO RESTAURANT MAC C - Add Menu\n\n");
printfood;
printf("\n");
while ( additems == 1 )
{
printf("Enter description (0 to Main Menu) : ");
scanf("%s", name);
if (strcmp (name, "0") == 0)
{
additems = 0;
clear;
break;
}
printf("Enter price (RM) : ");
scanf("%f", &price);
count ++;
fPointer1 = fopen ("Food.txt", "a");
printf("\n%d\t\t%s\t\t%.2f\n\n", count, name, price);
fprintf(fPointer1, "\n%d %s %.2f", count, name, price);
fclose (fPointer1);
}
}
else if (MainMenuCode == 2)
{
clear;
orderitems = 1;
printf("WELCOME TO RESTAURANT MAC C- Take Order\n\n");
printfood;
while (orderitems == 1)
{
fPointer1 = fopen ("Food.txt", "a+");
printf("Enter ITEM CODE (0 to Quit, 100 for Main Menu) : ");
scanf("%d", &item);
if (item == 100) break;
else if (item == 0) //final approach//
{
fPointer2 = fopen ("Invoice.txt", "a+");
de;
fclose (fPointer2);
return 0;
}
else if (item == 900)
{
for (i=0 ; i < 30 ; i++)
printf("%d: %d\n", i, OrderedItems);
}
else if (item > count || item < 1)
{
printf("\n\nITEM CODE not available. Try again\n\n");
}
else
{
OrderedItems[item]++;
printf("%d %d", item, OrderedItems);
}
fclose (fPointer1);
}
}
else printf("Please enter a valid Menu Code\n");
} while (MainMenuCode != 0);
return 0;
}
- printf("%d: %d\n", i, OrderedItems);
+ printf("%d: %d\n", i, OrderedItems[i]);
OrderedItems is an address of the array's first element.
OrderedItems[0] is a value of the array's first element.
OrderedItems[i] is a value of the array's i-th element.
The call to memset should specify how many bytes to set. You need to tell it the size of the type to initialise.
memset(OrderedItems, 0, 30 * sizeof(int))
Related
I want to use a function to scanf up to 10 values for an array with the size 10, and also keep track of the number of values that are in the array because I'll need it later for solving some maths about the array, (max value, min value, etc.).
#include <stdio.h>
int enter(int MeasurmentData[], int nrOfmeasurments)
{
for(int i=0;i<10;++i)
{
int MeasurmentData[10];
scanf("%d",&MeasurmentData[i]);
int nrOfmeasurments = 0;
nrOfmeasurments ++;
return nrOfmeasurments;
}
int main()
{
int MeasurmentData[10];
int nrOfmeasurments;
char menuoption;
while (1)
{
printf("Measurment tool 2.0\n");
printf("v (View)\n");
printf("e (Enter)\n");
printf("c (Compute)\n");
printf("r (Reset)\n");
printf("q (Quit)\n");
printf("enter your option:\n");
scanf(" %c", &menuoption);
if (menuoption =='e') \\ enter values
{
int MeasurmentData[10];
int nrOfmeasurments;
enter(MeasurmentData, nrOfmeasurments);
}
else if(menuoption == 'v') \\\ view values
{
//printf("%d", MeasurmentData[]);
}
else if(menuoption == 'c')
{
}
if(menuoption == 'q')
{
printf("Exiting Measurment tool 2.0\n");
return 0;
}
}
}
When I run the program it should print Measurment tool 2.0, after the the user has the choice of inputting e(enter) which will scan in up to 10 values into an array, if the user clicks q(quit) while in the enter option already he will be returned to the main menu where he can do whatever.
V(view) prints out the array for the user so that he can view what elements are inside.
C(compute) uses the elements inside and the nr of elements to calculate the highest value element, lowest.
There are some errors in your code. Ill try to explain. You have over declared your variables too many times. And since you have a fixed loop you don't need to count the measurements you will always read 10 measurements.
Below are the code with some modifications. Feel free to ask anything about it.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXIMUM_MEASURMENT 10
int enter(int MeasurmentData[])
{
char input[100];
int nrMeasurement = 0;
// reseting Measurment data
for(int i=0;i<MAXIMUM_MEASURMENT;++i) MeasurmentData[i] = 0;
for(int i=0;i<MAXIMUM_MEASURMENT;++i)
{
scanf("%99s", input);
if(strcmp(input, "q") == 0) {
break;
}
MeasurmentData[i] = (int) strtol(input, (char **)NULL, 10);
nrMeasurement++;
}
return nrMeasurement;
}
void showMeasurments(int* MeasurmentData, int length) {
int i = 0;
printf(" ======== Measurment ======== \n");
for(i = 0; i < length; i++) {
printf("%d ", MeasurmentData[i]);
}
printf("\n");
}
int main()
{
int MeasurmentData[MAXIMUM_MEASURMENT];
int nrOfmeasurments;
char menuoption;
while (1)
{
printf("Measurment tool 2.0\n" "v (View)\n" "e (Enter)\n" "c (Compute)\n" "r (Reset)\n" "q (Quit)\n enter your option:\n");
scanf(" %c", &menuoption);
if (menuoption =='e') // enter values
{
enter(MeasurmentData);
}
else if(menuoption == 'v') // view values
{
// show
showMeasurments(MeasurmentData, MAXIMUM_MEASURMENT);
}
else if(menuoption == 'c')
{
}
if(menuoption == 'q')
{
printf("Exiting Measurment tool 2.0\n");
break;
}
}
return 0;
}
Edit: i have updated the code. So i have read the comments of your question and there you have explained a little better what you are trying to accomplish. So since you have the requirement to press 'q' to stop reading values. I have to read all measurments as string and convert to integer if it is not the character q.
Edit 2: Thanks to #user3629249 to point out some of the flaws from the code ill update with his suggestions.
I'm new to programming so I'm trying to write a small program were I can show car information but also add cars to my "library"
right now my out come for 1.Show cars looks like this:
ID BRAND PICS
bbb188 BMW 1 2 3
AAA-999 VOLVO 4 5 6
CCC-999 CITROEN 1 2 3
but after I add a new car the PICS does not show.
so if I would add AAA-111 VOLVO 1. this is the outcome:
bbb188 BMW 1 2 3
AAA-999 VOLVO 4 5 6
CCC-999 CITROEN 1 2 3
AAA-111 VOLVO -398253632 3 3
I just get random numbers for pics and always 3 values.
Could anyone help me with this, and please show me how to do it instead.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define MAX 1000
#define IDSIZE 20
#define BRANDSIZE 50
#define PICSIZE 10
typedef struct{
char id[IDSIZE+1];
char brand[BRANDSIZE+1];
int *pic;
} Car;
void printCar(Car *pCar,int imagecount)
{
printf(" %s ",pCar->id);
printf(" %s ",pCar->brand);
for(int i=0; i<imagecount; i++){
printf(" %d",pCar->pic[i]);
}
printf("\n");
}
Car initCar(char itsId[],char itsBrand[],int *itsPic, int imagecount)
{
Car newCar;
strcpy(newCar.id, itsId);
strcpy(newCar.brand, itsBrand);
newCar.pic = itsPic;
return newCar;
}
void PrintList(Car aLista[],int imagecount, int carcount)
{
for(int i = 0; i<imagecount; i++)
printCar(&aLista[i],carcount);
}
void AddCar(Car aList[], int *pAt, Car theCar)
{
aList[(*pAt)++]=theCar;
}
Car NewCar(Car minapatienter[], int patientCount)
{
Car newCar;
gets(newCar.id);
printf("type in ID \n");
gets(newCar.id);
printf("type in brand\n");
gets(newCar.brand);
bool imageInputDone = false;
int imageCount=0;
while(imageInputDone == false)
{
printf("type in image reference \n");
int newImage;
scanf("%d",&newImage);
newCar.pic = &newImage;
imageCount++;
printf("vill du \n1.Add another image reference \n2.exit\n");
int input;
scanf("%d", &input);
printf("input: %i\n",input);
switch(input)
{
case 1:
printf("Adding one more image\n");
break;
case 2:
printf("Leaving loop\n");
imageInputDone = true;
break;
default:
while (input<1 || input<2)
;
printf("Input correct number\n");
break;
}
return newCar;
}
}
int main(void)
{
int carCount=0;
int imagecount=0;
Car myCar[MAX];
int input;
int test[3]={1,2,3};
int test2[3]={4,5,6};
myCar[0]= initCar("bbb188","BMW", test, 3);
myCar[1] = initCar("AAA-999","VOLVO", test2, 3);
myCar[2] = initCar("CCC-999", "CITROEN", test,3);
carCount=3;
imagecount=3;
do {
printf("1. Show cars \n2. Add car \n");
scanf("%d",&input);
switch(input)
{
case 1:
printf("ID BRAND PICS \n");
PrintList(myCar,carCount, imagecount);
break;
case 2:
AddCar(myCar,&carCount,NewCar(myCar,carCount));
printf("ID BRAND PICS \n");
PrintList(myCar,carCount, imagecount);
} //break;
} while (input < '1'|| input < '2');
return 0;
}
Your NewCar function have some problems. The newImage is in stack memory. When you do assignment newCar.pic = &newImage; the newCar.pic will point to undefined memory region because newImage was out of its scope. better way, we just use its value only, don't use address operator here. And one more thing, the newCar.pic is an pointer (array of int). So you need to allocate it before use. When you add more image item, you need to reallocate it. And initialize the pic to NULL pointer as well.
Here is my modification your NewCar function:
Car NewCar(Car minapatienter[], int patientCount)
{
Car newCar;
gets(newCar.id);
printf("type in ID \n");
gets(newCar.id);
printf("type in brand\n");
gets(newCar.brand);
newCar.pic = NULL;
bool imageInputDone = false;
int imageCount=0;
while(imageInputDone == false)
{
printf("type in image reference \n");
int newImage;
scanf("%d",&newImage);
// Rellocation
newCar.pic = realloc(newCar.pic, (imageCount+1)*sizeof(int));
newCar.pic[imageCount] = newImage;
imageCount++;
printf("vill du \n1.Add another image reference \n2.exit\n");
int input;
scanf("%d", &input);
printf("input: %i\n",input);
switch(input)
{
case 1:
printf("Adding one more image\n");
break;
case 2:
printf("Leaving loop\n");
imageInputDone = true;
break;
default:
while (input<1 || input<2)
;
printf("Input correct number\n");
break;
}
return newCar;
}
}
You get the same number of images printed for each car because you only have a global counter. You need a counter per image:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define MAX 1000
#define IDSIZE 20
#define BRANDSIZE 50
#define PICSIZE 10
typedef struct Car
{
char id[IDSIZE+1];
char brand[BRANDSIZE+1];
int *pic;
int imagecount;
} Car;
With this change there is no need to pass a count for printing:
void printCar(Car *pCar)
{
printf(" %s ", pCar->id);
printf(" %s ", pCar->brand);
for(int i=0; i<pCar->imagecount; i++)
{
printf(" %d",pCar->pic[i]);
}
printf("\n");
}
The counter needs to be stored during initialization:
Car initCar(char itsId[], char itsBrand[], int *itsPic, int imagecount)
{
Car newCar;
strcpy(newCar.id, itsId);
strcpy(newCar.brand, itsBrand);
newCar.pic = itsPic;
newCar.imagecount = imagecount;
return newCar;
}
When you print your list, you mix up count of images and count of cars:
void PrintList(Car aLista[], int imagecount, int carcount)
{
for(int i = 0; i<imagecount; i++)
printCar(&aLista[i],carcount);
}
This must be:
void PrintList(Car aLista[], int carcount)
{
for (int i = 0; i < carcount; i++)
printCar(&aLista[i]);
}
Adding the car to your array is basically OK, but you might check if you reach MAX cars.
void AddCar(Car aList[], int *pAt, Car theCar)
{
aList[(*pAt)++]=theCar;
}
Now the biggest problem. This function hads issues with memory usage and weird loops.
Car NewCar(void)
{
Car newCar = {0}; // initialze with empty strings and NULL pointers.
// TODO: Replace gets with fgets!
// gets(newCar.id); // WHY read before you prompt??
printf("type in ID \n");
gets(newCar.id);
printf("type in brand\n");
gets(newCar.brand);
bool imageInputDone = false;
int imageCount=0;
while(imageInputDone == false)
{
printf("type in image reference \n");
int newImage;
scanf("%d",&newImage);
imageCount++;
int *newpics = realloc(newCar.pic, (imageCount) * sizeof(int));
newpics[imageCount-1] = newImage;
newCar.pic = newpics;
// TODO: Check for NULL
printf("vill du \n1.Add another image reference \n2.exit\n");
int input;
scanf("%d", &input);
printf("input: %i\n",input);
while (input < 1 || input > 2)
switch(input)
{
case 1:
printf("Adding one more image\n");
break;
case 2:
printf("Leaving loop\n");
imageInputDone = true;
break;
default:
printf("Input correct number\n");
break;
}
newCar.imagecount = imageCount;
return newCar;
}
}
And finally...
int main(void)
{
int carCount=0;
Car myCar[MAX];
int input;
int test[3] = {1,2,3};
int test2[3] = {4,5,6};
myCar[0] = initCar("bbb188", "BMW", test, 3);
myCar[1] = initCar("AAA-999", "VOLVO", test2, 3);
myCar[2] = initCar("CCC-999", "CITROEN", test, 3);
carCount=3;
do
{
printf("1. Show cars \n2. Add car \n");
scanf("%d", &input);
switch(input)
{
case 1:
printf("ID BRAND PICS \n");
PrintList(myCar, carCount);
break;
case 2:
AddCar(myCar, &carCount, NewCar());
printf("ID BRAND PICS \n");
PrintList(myCar, carCount);
} //break;
} while (input < 1 || input > 2); // compare as integers, not characters. Don't use < operator
return 0;
}
The code is not tested. Remaining errors are left for exercise. ;)
I am trying to store data for Value, Weight and Cost from a file containing a table which contains three columns of numbers for Value, Weight and Cost. The table may vary in length (9 or 21 rows) depending on the file chosen by the user.
I am having trouble trying to store the data from this file to use in a brute force function to solve the problem.
This is what I have so far.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int nr_rows;
int i = 1;
int j = 2;
int k = 3;
int l,m,n = 0;
int budget_cost;
int weight_lim;
char file_name[50];
float c[nr_rows][3];
char stringA[20] = "objectsA.txt";
char stringB[20] = "objectsB.txt";
printf("Enter the filename containing item listing: ");
scanf("%s", &file_name);
if(strcmp(file_name,stringA)==0)
{
nr_rows = 9;
}
else if(strcmp(file_name,stringB)==0)
{
nr_rows = 21;
}
printf("The number of rows is %d", nr_rows);
float value[nr_rows], weight[nr_rows], price[nr_rows];
FILE *fpointer;
fpointer = fopen(file_name, "r");
if(!fpointer)
{
printf("The file %s could not be opened.", file_name);
return 1;
}
j=0;
while(j<nr_rows)
{
i=0; // Skip the first line
while(i<3)
{
fscanf(fpointer, "%f", &c[j][i]);
//printf("%.0f\n", c[j][i]);
if(i=1) /* Change this if statement so that 1 ,4 ,7 ,10
etc. activates*/
{
c[j][i] = v[l];
l++;
printf("%f", v[l]);
}
else if(i=2)/* Change this if statement so that 2,5,8 etc.
activates*/
{
c[j][i] = w[m];
m++;
}
else if(i=3)/* Change this if statement so that 3,6,9 etc.
activates*/
{
c[j][i] = p[n];
n++;
}
i++;
}
j++;
}
fclose(fpointer);
//1. Read carefully your file name by doing:
scanf("%s", file_name); // instead of scanf("%s", &file_name);
//2. Declare c[nr_rows][3] only after reading "nr_rows"
//3. The main "while" loop could look like this
while(j < nr_rows)
{
fscanf(fpointer, "%f %f %f", &c[j][0], &c[j][1], &c[j][2]);
}
// Then,
fclose(fpointer);
I wrote a program in C which takes as an input a value and an ordered Array of integers and performs a ternary search to find the value(if it exists) inside the Array.
I have seen all the possible problems with the usage of scanf and the related topics here in Stackoverflow.
I have noticed that there is a difference if I call the 2 scanf functions in reverse order.
If I use the code as it is below. First read the value and after the array from the user, the program and scanf functions as expected.
printf("Enter the value to be searched in the Array: ");
int k;
scanf(" %d", &k);
printf("Type elements of A(sorted) separated by spaces (type 'end' to stop): ");
i = 0;
while(scanf("%d", &A[i]) == 1) {
i++;
}//while
Although if I use the scanf inputs in the reverse order the second scanf never stops to get user input and read values left in the buffer.
printf("Type elements of A(sorted) separated by spaces (type 'end' to stop): ");
i = 0;
while(scanf("%d", &A[i]) == 1) {
i++;
}//while
printf("Enter the value to be searched in the Array: ");
int k;
scanf(" %d", &k);
I cannot understand what is the difference in the calling order.
I have tried the solutions mentioned in the other threads but none worked.
Just as a reference here is the whole code(working as expected):
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int ternarySearch(int A[], int l, int r, int k){
int i;
int first,second;
if(l>r){
return -1;
}
i= (r - l)/3;
if(i==0){
i++;
}
first = i+l-1;
second = i*2+l-1;
if(A[first]==k){
return first;
}
else if(A[first]>k){
ternarySearch(A, l, first-1, k);
}
else
{
if(A[second]==k)
return second;
else
if(A[second]>k)
ternarySearch(A, first+1,second-1, k);
else
ternarySearch(A, second+1,r, k);
}
}
int main(){
const int maxarraylen = 1000;
int i;
int n;
int A[maxarraylen];
char string[250];
printf("Enter the value to be searched in the Array: ");
int k;
scanf(" %d", &k);
printf("Type elements of A(sorted) separated by spaces (type 'end' to stop): ");
i = 0;
while(scanf("%d", &A[i]) == 1) {
i++;
}//while
n=i-1;
//We assume the array is sorted otherwise we can use any sorting algorithm e.g. code from task1
scanf(" %d", &k);
int result;
result=ternarySearch(A, 0, n, k);
if(result==-1){
printf("The value was not found in the Array.\n");
}
else{
printf("The value was found in position no. %d.\n", result);
}
return 0;
}
Your problem is that you are not 'stepping over' your end input.
We can see this by doing an experiment using the following program:
#include <stdio.h>
#include <stdlib.h>
void main(void) {
FILE *f;
long f_pos;
int ret;
int i;
int data[5];
int data_last;
int search;
f = fopen("./input.txt", "r");
if (f == NULL) {
perror("fopen()");
return;
}
/* read in the values for the array */
data_last = -1;
for (i = 0; i < 5; i++) {
ret = fscanf(f, "%d", &(data[i]));
printf("fscanf(data[%d]): ret: %d\n", i, ret);
f_pos = ftell(f);
printf("ftell(): %ld\n", f_pos);
if (ret != 1) {
break;
}
data_last = i;
}
/* check that we read in at least one value */
if (data_last == -1) {
printf("no input data!\n");
return;
}
/* insert 'fix' here */
/* pre-load the 'search' with known garbage */
search = 987;
/* now read in the search value */
ret = fscanf(f, "%d", &search);
printf("fscanf(search): ret: %d\n", ret);
f_pos = ftell(f);
printf("ftell(): %ld\n", f_pos);
/* print out our info */
for (i = 0; i <= data_last; i++) {
printf("data[%d]: %d\n", i, data[i]);
}
printf("search for: %d\n", search);
return;
}
With the following data in input.txt:
123
456
end
456
The output is as follows:
fscanf(data[0]): ret: 1
ftell(): 3
fscanf(data[1]): ret: 1
ftell(): 7
fscanf(data[2]): ret: 0
ftell(): 8
fscanf(search): ret: 0
ftell(): 8
data[0]: 123
data[1]: 456
search for: 987
ftell() tells us where the file's cursor is, and in this case we can see that it is at byte 8... the e of the input line end.
It doesn't get past it, and thus the next attempt to read a number (%d) will fail too!
It's also a good idea to check the return values! We can see that the fscanf(&search) call has failed to read a number!
The solution is to insert this snippet just after we check that we recieved array values:
/* this is the 'fix' */
ret = fscanf(f, "end");
printf("fscanf(end): ret: %d\n", ret);
f_pos = ftell(f);
printf("ftell(): %ld\n", f_pos);
I have been trying to figure out why my code isn't working properly. I know my code below is a mess (I am a rather poor C programmer thus far). Its a work in progress. Specifically
printf("Please enter the index of the contact you wish to view. \nThis should be a positive integer\n\n");
scanf("%d", &vIndex);
fgetc(stdin);
printf("The value of vIndex is %d", &vIndex);
I find that when i run my program I might select a keyboard input of 1, meaning I am looking at my second record in my file entries.txt. The printout of vIndex however is a number much much larger, much likely the last information stored there. Running in debug however, i find that vIndex change to 1 and but prints the strange number. My entire code is below.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct rec
{
int i;
float PI;
char A;
} Record;
typedef struct
{
char fname[20];
char lname[50];
char phone[15];
} Contact;
Record * arrayAlloc(int size);
char * stringAlloc(int size);
Contact * contactAlloc();
void structAlloc();
int main(void)
{
int *ptrInt;
Record * ptrRec;
int i = 0;
char * myName;
Contact * contacts;
ptrInt = (int *)malloc(sizeof(int));
int vIndex=0;
int displayMenu();
Contact * contactAlloc();
// void searchIndex();
void searchFirst();
void searchLast();
void searchPhone();
contacts = contactAlloc();
char choice;
choice = displayMenu();
while (choice !=5)\
{
if (choice == 1)
// searchIndex();
{
printf("Please enter the index of the contact you wish to view. \nThis should be a positive integer\n\n");
// fgets(vIndex, 700, stdin);
scanf("%d", &vIndex);
fgetc(stdin);
printf("The value of vIndex is %d", &vIndex);
printf("You have selected to view the %d contact.\nFirst name:\t%c. \nLast Name:\t%c. \nPhone Number:\t%c.\n\n ", &vIndex, contacts[vIndex].fname, contacts[vIndex].lname, contacts[vIndex].phone);
}
else if (choice == 2)
searchFirst();
else if (choice == 3)
searchLast();
else if (choice == 4)
searchPhone();
choice = displayMenu();
}
printf("Thank for you using this program.\n");
return 0;
}
int displayMenu()
{
int choice = 0;
while (choice!= 1 && choice != 2 && choice != 3 && choice != 4 && choice!=5)
{
printf("\nWelcome to the phone book application. Please choose from the following options:");
printf("\n\n\t 1) Search the phone book by index. \n\t 2) Search the phone book by first name. \n\t 3) Search the phone book by last name. \n\t 4) Search the phone book by phone number. \n\t 5) Quit.\n\n");
scanf("%d", &choice);
}
return choice;
}
Contact * contactAlloc()
{
FILE * fin;
int count = 0, i = 0;
char aLine[100];
Contact * ptrContact;
fin = fopen("entries.txt", "r");
if (fin != NULL)
{
while( fgets(aLine, sizeof(aLine), fin) != NULL )
{
count++;
}
fseek(fin, 0L, SEEK_SET);
count = count / 3;
ptrContact = (Contact *) calloc(count, sizeof(Contact));
count = 0;
while( fgets(aLine, sizeof(aLine), fin) != NULL )
{
if (aLine[strlen(aLine) - 1] == '\n')
{
aLine[strlen(aLine) - 1] = '\0';
}
if (i % 3 == 0)
{
strcpy(ptrContact[count].lname, aLine);
}
else if (i % 3 == 1)
{
strcpy(ptrContact[count].fname, aLine);
}
else if (i % 3 == 2)
{
strcpy(ptrContact[count].phone, aLine);
//printf("Line %d at count %d: %s\n", i, count, aLine);
count++;
}
i++;
}
//count=count*3;
printf("%d contacts loaded.\n\n", count);
fclose(fin);
}
return ptrContact;
}
/*
void searchIndex()
{
int vIndex=0;
printf("Please enter the index of the contact you wish to view. This should be a positive integer");
scanf("%d", &vIndex);
fgetc(stdin);
printf("You have selected to view the %d contact.\nFirst name:\t%c. \nLast Name:\t%c. Phone Number:\t%c.\n\n ", &vIndex, &Contact[vIndex].fname, &Contact[vIndex].lname, &Contact[vIndex].phone);
}
*/
void searchFirst()
{
}
void searchLast()
{
}
void searchPhone()
{
}
You are printing the address of vIndex instead of the value:
printf("The value of vIndex is %d", &vIndex);
Change this line to the following:
printf("The value of vIndex is %d", vIndex);