So many errors and not sure how to fix it - c

I'm currently still practicing my c programming skills but there are so many errors here that I'm confuse on what is wrong and how to fix it. It's for a database program that I was practicing on.
It keeps showing:
new2.c:86: error: request for member ‘previousreading’ in something not a structure or union
and
new2.c:94: error: ‘Break’ undeclared (first use in this function)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
int custid;
char custname;
float currentreading;
float previousreading;
double charge;
int choice;
unsigned cust;
int revenue, meterdifference, BILL;
printf("----------------------------------\n");
printf("Electricity Management System\n");
printf("----------------------------------\n");
printf("\n1. Record Usage");
printf("\n2. Add Customer");
printf("\n3. Edit Customer");
printf("\n4. Delete Customer");
printf("\n5. Show Customer");
printf("\n6. Show Total monthly income");
printf("\n7. Exit");
scanf("%d",&choice);
if(choice >=1 || choice <=7)
{
switch(choice)
{
case 1: //Record Usage
printf("Enter Customer ID\n");
FILE *cfPtr;
if ((cfPtr = fopen("customer.txt", "r"))== NULL)
puts("This file could not be opened");
else
{
puts("Enter the customer ID, name.");
scanf("%d%29s", &cust.custid, cust.custname);
puts("Enter the current reading in kWh");
scanf("%d", cust.currentreading);
if(cust.currentreading < cust.previousreading)
puts("Input invalid");
else
{
if (cust.currentreading>=200)
{
cust.charge = (cust.currentreading - cust.previousreading)*21.80;
printf("\nThe charge is RM%f\n", &cust.charge);
}
else
{
if (cust.currentreading>=300)
{
cust.charge= ((cust.currentreading - cust.previousreading)*33.40)+21.80;
printf("\nThe charge is RM%f", &cust.charge);
}
else
{
if (cust.currentreading>=600)
{
cust.charge= ((cust.currentreading - cust.previousreading)*51.60)+21.80;
printf("\nThe charge is RM%f", &cust.charge);
}
else
{
if (currentreading>=900)
{
cust.charge = ((cust.currentreading - cust.previousreading)*54.60)+21.80;
printf("\nThe charge is RM%f", &cust.charge);
}
else
{
cust.charge = ((cust.currentreading - cust.previousreading)*57.10)+21.80;
printf("\nThe charge is RM%f", &cust.charge);
}
}
}
}
}
}
Break;
case2: //Add Customer
puts("This option allows user to add new customer");
printf("Enter Customer ID and name.");
scanf("%d%c", &cust.custid, cust.custname);
puts("To return to menu");
Break;
case 3: //Edit Customer
puts( "This option allows user to edit customer info");
Break;
case 4: //delete customer
puts( "This option allows user to delete customer");
Break;
case 5: //Show Customer
printf("To show customer information\n");
FILE*tPtr;
char custid[100],custname[100];
int previousreading,currentreading;
double charge;
printf("\n Show Customer\n");
if((tPtr= fopen("customer.txt","r"))==NULL){
puts("File not found");
}
else{
printf("%-15s%-25s%-20s%-15s%-15s\n","ID","Name","Previous Reading","Current Reading","Charges");
while(!feof(tPtr)){
fscanf(tPtr,"%[^;];%[^;];%d;%d;%lf",cust.custid,cust.custname,&cust.previousreading,&cust.currentreading,&cust.charge);
printf("%s\t\t%-25s%-20d%-15d%-15.2lf",cust.custid,cust.custname,cust.previousreading,cust.currentreading,cust.charge);
}
fclose(tPtr);
}
printf("\n\n");
Break;
case 6: //Show total income(monthly)
puts("To show monthyly income");
printf("total usagekWh, meterdifference");
printf("%-15s%-35.2d\n", "Total UsagekWh","meterdifference");
scanf("%-16dtotal usage(kWh)%-24d: %.2f",&meterdifference);
printf("%-13dtotal revenue%-24d: %.2f",BILL);
revenue=BILL;
printf("revenue is %.2f", BILL);
Break;
case 7: //Exit
Break;
}
}
else
printf("\nError. Number not in choices.");
return 0;
}
typedef struct{
int custid[50];
char custname[100];
int previousreading;
int currentreading;
float charges;
}cust;

Put the typedef before main. typedefs must occure before you use them just as vaiables.
Replace unsigned cust; by cust cust;. unsigned cust; is the same as unsigned int cust; and declares an unsigned integer, you want to declare a cust.
Replace float charges; by float charge; in the typedef
Replace Break; by break;. Case matters in C. Break is not Break, just as Int is not int.
Then it compiles.
Now if it it runs correctly or not is another story.

There is not a single structure in your code, not in the form of a variable declaration nor as a type definition1, and you are treating cust which is simply an unsigned int as if it was a structure, perhaps you mean
struct {
float previousreading;
float currentreading;
/* And so on */
} cust;
Also, there is no Break keyword in c, it's break, all lower case.
But,
Don't do it, create a new struct so that you can use declare variables of type struct Costumer for example. Like at the end of your code, except that the compiler needs to know about it before using it, and the cust variable should have it's type.
A char is not a string type, if you want a string you need an array of char, so char custname; is not going to work for the name string.
Use meaningful names for your variables, and the members if your structure and the type name too. Like costumer instead of cust.
Additional NOTE
See Why while (!foef(file)) is always wrong. Your code will always attempt a read with fscanf() that will fail but it proceeds to print the data, it's very likely that your last row is printed twice once you make the code compile.
Instead, check the return value of fscanf(), if you don't know what it returns and don't fully understand it you can always read fscanf(3) documentation.
1At least not before you attempt to use it.

Related

Having problems displaying structure array content

I want to display structure members based on user input, but I don't know if I've stored the input properly.
When I try display all people, it just outputs random numbers.
These are the structures and function prototypes
#define MAX_NAME_LEN 15
#define MAX_NUM_PERSON 4
#define MAX_JOB_LENGTH 20
typedef struct birth_date
{
int month;
int day;
int year;
} person_birth_t;
typedef struct person
{
char pName[MAX_NAME_LEN];
char job[MAX_JOB_LENGTH];
person_birth_t birth_t;
} person_t[MAX_NUM_PERSON];
void print_menu (void);
void scanPerson(person_t p, int);
void displayPeople(person_t p);
This is the main code for the program, a menu is printed asking user to input a number, if a user enters 1 then it prompts them to add a person. Entering 2 displays all people entered.
int main(void)
{
/* TODO */
print_menu();
return 0;
}
void print_menu (void)
{
int choice;
person_t p;
static int index = 0;
int *indexP = NULL;
indexP = &index;
/*Print the menu*/
scanf("%d", &choice);
switch (choice)
{
case 1:
if (index < MAX_NUM_PERSON){
scanPerson(p, index);
++*indexP;
print_menu();
} else {
printf("Can't add more people - memory full \n");
print_menu();
}
break;
case 2:
displayPeople(p);
break;
case 3:
exit(0);
break;
default:
print_menu();
}
}
/*function called when add person is chosen from menu */
void scanFlight(person_t p, int index){
/*printf to enter name*/
scanf(" %s", p[index].pName);
/*printf to enter job*/
scanf("%s", p[index].job);
}
void displayPeople(person_t p){
for(int i = 0; i < MAX_NUM_PERSON; i++){
printf("%s %d-%d-%d %s \n",p[i].pName
,p[i].birth_t.month
,p[i].birth_t.day
,p[i].birth_t.year
,p[i].job);
}
}
I've tried other ways to take input and add it to a struct array, but I'm just not sure how to do it right.
person_t p;
Here, you use the local variable p (in print_menu function), so each recursion, you just print the parameters of the local variable that is not initialized.
To solve it, you can declare p as the global variable.
OT, in scanFlight function, to avoid overflow, you should change the scanf function to:
/*printf to enter name*/
scanf("%14s", p[index].pName);
/*printf to enter job*/
scanf("%20s", p[index].job);
And, rename scanPerson to scanFlight, because i do not see any implementation of scanPerson function in your code. I think it's typo, no ?
None of the methods were working, so instead of trying to figure it out, I scrapped the static index and indexP.
Instead, I initialized p with malloc:
person_t *p= malloc(MAX_NUM_PERSON * sizeof(person_t));
I changed the scan function to accommodate for the change and made index a pointer instead, and I made the display function pass the index.
When I ran it, the output was correct.

Passing and returning a struct from a function

I have a function with a book struct array, but when i try to return it to my main it does not return the values and store them in the array. If the addBook function has to be void how would i work around that so that i can access the array elements later.
void addBook(struct Book book[], int *size) {
if (*size == MAX_BOOKS) {
printf("The inventory is full\n");
}
else {
printf("ISBN:");
scanf("%d", &book[*size]._isbn);
printf("Title:");
scanf("%s", book[*size]._title);
getchar();
printf("Year:");
scanf("%d", &book[*size]._year);
printf("Price:");
scanf("%f", &book[*size]._price);
printf("Quantity:");
scanf("%d", &book[*size]._qty);
*size++;
printf("The book is successfully added to the inventory.\n");
}
return book;
}
int main(void) {
struct Book book[MAX_BOOKS];
int size = 0;
int i;
int option;
printf("Welcome to the Book Store\n");
printf("=========================\n");
do {
menu();
printf("Select: ");
scanf("%d", &option);
switch (option) {
case 0:
printf("Goodbye!\n");
break;
case 1:
displayInventory(book, size);
break;
case 2:
addBook(book, &size);
break;
case 3:
//checkPrice();
break;
default:
printf("Invalid input, try again:\n");
}
} while (option != 0);
}
Your return statement isn't going to do what you're intending as the addBook's function signature says it returns void. I'm surprised that the code as is actually compiled without an error about this.
Anyways, the book data can be returned from the same way it was passed in - as an input and output parameter.
Essentially your code could look like the following (which is only meant to be an example of code that compiles and works to save info entered in from standard input into the book):
#include <stdio.h>
struct Book {
int value;
};
#define MAX_BOOKS 2
void addBook(struct Book book[], int *size) {
if (*size == MAX_BOOKS) {
printf("The inventory is full\n");
}
else {
printf("Value:");
scanf("%d", &book[*size].value);
(*size)++;
printf("The book is successfully added to the inventory.\n");
}
}
int main(void) {
struct Book book[MAX_BOOKS];
int size = 0;
addBook(book, &size);
printf("Book 1: Value=%d\n", book[0].value);
}
And here's how this looks when run:
$ ./main
Value:9
The book is successfully added to the inventory.
Book 1: Value=9
Hope this answers your question.
I think your problem is in the line *size++;. You should use parenthesis, otherwise you are modifying the pointer to size, not the value. It should be (*size)++.
Also, the addBook function should not return anything, since it is void, and it is changing the content of the array book already.

printing with type long

I'm working on my school project form data structure and I'm having a problem with the type long.
I want to design a small project like a university registration system.
First the person get to chose if he want to register or exit.
If he want to register then he will enter his name and gpa to select the major that is available based on his gpa and the system will generate an id automatically.
The problem is the id is not incremented correctly.
Additional info:
I'm using doubly linked list structure
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
struct student{
char name[30];
int GPA;
unsigned long ID;
char colloege[100];
student *next, *prev;
};
student *stu, *cur, *head, *tail;
void insertfront();
void displaylist(){
unsigned long id=214000000;
cur=head;
printf(" ");
if(cur==NULL)
printf(" ");
else
while(cur!=NULL){
printf("Name:%s\t",cur->name);
printf("ID:%d\t",cur->ID=id++);
printf("GPA:%d\t",cur->GPA);
printf("Colloege of:%s\t",cur->colloege);
printf("\n");
cur=cur->next;
}
}
void main(){
clrscr();
int x;
printf("\n\t\t\t\t******* King Faisal University *********\n");
while(x!=2){
printf("\n press 1 to insert your information ,press 2 to exit\n");
scanf("%d",&x);
insertfront();
displaylist();
}
stu=NULL;
cur=NULL;
head=NULL;
tail=NULL;
getch();
}
void insertfront(){
int x,c;
stu=(student*)malloc(sizeof(student));
fflush(stdin);
printf("Enter your name:\n");
scanf("%[^\n]%*c",stu->name);
printf("Enter your GPA:\n");
scanf("%ul",&stu->GPA);
printf("\n Available Colloeges\n");
if(stu->GPA>=85)
{
printf("1.Colloege of Medicine\n 2.Colloege of Engenering\n 3.Colloege of Computer Science\n 4.Colloege of Business\n 5.Colloege of Art\n");
printf("Enter the colloege number \n");
scanf("%d",&c);
switch(c){
case 1: strcpy(stu->colloege,"Medicien");break;
case 2: strcpy(stu->colloege,"Engenering");break;
case 3: strcpy(stu->colloege,"Computer Science");break;
case 4: strcpy(stu->colloege,"Business");break;
case 5: strcpy(stu->colloege,"Art");break;
}
}
else if(stu->GPA>=75)
{
printf("1.Colloege of Computer Science\n 2.Colloege of Business\n 3.Colloege of Art\n");
printf("Enter the colloege number\n");
scanf("%d",&c);
switch(c){
case 1: strcpy(stu->colloege,"Computer Science");break;
case 2: strcpy(stu->colloege,"Business");break;
case 3: strcpy(stu->colloege,"Art");break;
}
}
else
strcpy(stu->colloege,"Art");
if(head==NULL)
{
stu->next=NULL;
stu->prev=NULL;
head=stu;
tail=stu;
}
else
{
stu->next=head;
stu->prev=NULL;
head->prev=stu;
head=stu;
}
}
You are using an incorrect conversion in your program
printf("ID:%d\t",cur->ID=id++);
// cur->ID is of type unsigned long
// %d is used for values of type int
To print a value of type long you need to use "%ld" in the printf() conversion
long longvalue = 42;
printf("%ld\n", lonvgalue);
To print a value of type unsigned long you need to use "%lu" in the printf() conversion
unsigned long ulongvalue = 42;
printf("%lu\n", ulongvalue);
In addition to the format specifier being wrong, you have a problem with assigning ID:s.
Your code assigns new ID:s every time you display the list.
I would generate the ID only once for each student record, when the record is created.
So add this in insertfront:
stu->ID=id++;
and change the printing in displaylist to:
printf("ID:%lu\t",cur->ID);
and move the declaration of id either to global scope (outside of any function) or as a static variable inside insertfront:
static unsigned long id=214000000;

Strings Became Symbols When They Output in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a problem when i output some strings in my codes. When i input 1 string and then output it, there is no problem at all. But, when i input at least 2 strings, it went fail. The strings became symbols, except the last one. Here is the screenshot:
I've been using fflush(stdin) and fflush(stdout), but the problem is still exist.
So, what should i do? I need your advice.
Here is my complete codes:
#include <stdio.h>
#include <stdlib.h>
void menu();
void entry();
void search();
void PrintSingle();
void PrintComplete();
float TotalUsed(int i);
float RegularCost(int i);
float tax(int i);
float discount(int i);
float TotalPayment(int i);
#define NMaks 101
typedef enum {false=0,true=1} boolean;
typedef struct {int BillNumber,BillClass;float LastMeter,CurrentMeter;char name[];} BillDatabase;
BillDatabase bill[NMaks];
int DataAmount=0;
int main() {
menu();
return 0;
}
void menu() {
int i;
repeat:
system("cls");
printf("\t\t.: Electric Billing System :.\n\n");
printf("[1] Entries customer information\n");
printf("[2] Search customer\n");
printf("[3] Print single bill\n");
printf("[4] Print complete billing report\n");
printf("[5] Exit\n\n");
printf("Select the menu[1..5]: ");scanf("%d",&i);
switch (i) {
case 1 : entry();break;
case 2 : search();break;
case 3 : PrintSingle();break;
case 4 : PrintComplete();break;
case 5 : {
printf("\n\nGoodbye!");
getch();
break;
}
default : {
printf("\n\nWrong menu!");
goto repeat;
}
}
}
void entry() {
char repeat;
do {
system("cls");
printf("\t\t.: Electric Billing System :.\n\n");
printf("[1] Entries customer information\n\n");
DataAmount++;
printf("Bill number: ");scanf("%d",&bill[DataAmount].BillNumber);
printf("Customer name: ");fflush(stdin);fflush(stdout);gets(bill[DataAmount].name);
printf("Class[1..3]: ");scanf("%d",&bill[DataAmount].BillClass);
printf("Last meter: ");scanf("%f",&bill[DataAmount].LastMeter);
printf("Current meter: ");scanf("%f",&bill[DataAmount].CurrentMeter);
printf("\nEntry again[y/n]: ");repeat=getche();
} while (tolower(repeat)=='y');
menu();
}
void search() {
int i,BN;
boolean found;
char repeat;
do {
system("cls");
printf("\t\t.: Electric Billing System :.\n\n");
printf("[2] Search customer\n\n");
printf("Enter the bill number: ");scanf("%d",&BN);
found=false;
for (i=1;i<=DataAmount;i++)
if (BN==bill[i].BillNumber) {
found=true;
break;
}
if (found) {
printf("\nCustomer found!\n\n");
printf("Bill number\tN a m e Class\t\tLast meter\tCurrent meter\n");
fflush(stdin);fflush(stdout);
printf("%d\t\t%15.15s\t%d\t\t%.2f kWh\t%.2f kWh\n",bill[i].BillNumber,bill[i].name,bill[i].BillClass,bill[i].LastMeter,bill[i].CurrentMeter);
}
else
printf("\nCustomer not found!\n");
printf("\nSearch again[y/n]: ");repeat=getche();
} while (tolower(repeat)=='y');
menu();
}
void PrintSingle() {
int i,BN;
boolean found;
char repeat;
do {
system("cls");
printf("\t\t.: Electric Billing System :.\n\n");
printf("[3] Print single bill\n\n");
printf("Enter the bill number: ");scanf("%d",&BN);
found=false;
for (i=1;i<=DataAmount;i++)
if (BN==bill[i].BillNumber) {
found=true;
break;
}
if (found) {
printf("\nCustomer found!\n\n");
printf("Bill number\tN a m e Class\t\tLast meter\tCurrent meter\n");
fflush(stdin);fflush(stdout);
printf("%d\t\t%15.15s\t%d\t\t%.2f kWh\t%.2f kWh\n",bill[i].BillNumber,bill[i].name,bill[i].BillClass,bill[i].LastMeter,bill[i].CurrentMeter);
printf("Total used\tRegular cost\tTax\t\tDiscount\tTotal Payment\n");
printf("%.2f kWh\t$%7.2f\t$%7.2f\t$%7.2f\t$%7.2f\n",TotalUsed(i),RegularCost(i),tax(i),discount(i),TotalPayment(i));
}
else
printf("\nCustomer not found!\n");
printf("\nPrint again[y/n]: ");repeat=getche();
} while (tolower(repeat)=='y');
menu();
}
void PrintComplete() {
int i;
system("cls");
printf("\t\t.: Electric Billing System :.\n\n");
printf("[4] Print complete billing report\n\n");
printf("Bill number\tN a m e\t\tClass\tTotal Payment\n");
for (i=1;i<=DataAmount;i++) {
fflush(stdin);fflush(stdout);
printf("%d\t\t%15.15s\t\t%d\t$%.2f\n",bill[i].BillNumber,bill[i].name,bill[i].BillClass,TotalPayment(i));
}
printf("\nPress any key to the main menu");
getch();
menu();
}
float TotalUsed(int i) {
return bill[i].CurrentMeter-bill[i].LastMeter;
}
float RegularCost(int i) {
float price;
switch (bill[i].BillClass) {
case 1 : price=10.0;break;
case 2 : price=7.5;break;
default : price=13.75;
}
return TotalUsed(i)*price;
}
float tax(int i) {
float TaxPercentage;
switch (bill[i].BillClass) {
case 1 : TaxPercentage=1.5/100;break;
case 2 : TaxPercentage=0.25/100;break;
default : TaxPercentage=3.5/100;
}
return RegularCost(i)-(RegularCost(i)*TaxPercentage);
}
float discount(int i) {
float DiscountPercentage;
switch (bill[i].BillClass) {
case 1 : DiscountPercentage=3.0/100;break;
case 2 : DiscountPercentage=2.0/100;break;
default : DiscountPercentage=5.5/100;break;
}
if (RegularCost(i)>100.0)
return RegularCost(i)-(RegularCost(i)*DiscountPercentage);
else
return 0.0;
}
float TotalPayment(int i) {
return RegularCost(i)+tax(i)-discount(i);
}
Note: I use Code Blocks for the IDE.
I see a number of problems here.
First of all, your over-use of global variables is troubling. The array bill and the int DataAmount should definitely not be global. This is not good c programming practice and I would not trust someone whose code looks like this. Please give your "menu", "entry", "printcomplete", and "search" functions arguments so that you can make these variables local, not global.
Second, there are very few cases where it is acceptable to have many statements on the same line as in
printf("Customer name: ");fflush(stdin);fflush(stdout);gets(bill[DataAmount].name);
I'm getting a headache just looking at it!!! pleeease don't!
Third, You are not using array bounds correctly in this for loop, which is the cause of the bug you are asking about in the first place.
for (i=1;i<=DataAmount;i++) {
fflush(stdin);fflush(stdout);
printf("%d\t\t%15.15s\t\t%d\t$%.2f\n",bill[i].BillNumber,bill[i].name,bill[i].BillClass,TotalPayment(i));
}
Remember, in the C language, array indices start at 0 and end at "n - 1". Your loop is starting at 1 and ending at "n".
The reason for the bogus character is that you are accessing a BillDataBase that is outside the boundaries of the array bill.
Fourth, as Sami Kuhmonen said, you are not allocating any memory for your name array. Although this is probably not causing the bug you are seeing, it will almost certainly cause your program to crash with a segmentation fault at some point in the future. In order to fix this looming problem, you have 2 options
The easy way out: change your BillDatabase struct to the following
typedef struct
{
int BillNumber, BillClass;
float LastMeter, CurrentMeter;
char name[128];
} BillDatabase;
This is easy, but not good, because the name can never be more than 128 chars. Also, please don't EVER put the entire struct on one line. I almost started hitting myself over the head with my saxophone.
The harder way out: learn to use malloc(), and manually allocate the name arrays.
You are not allocating any memory for your names, so you're just writing into random places in memory. This is undefined behaviour.

When I run this program, after entering a name, I get a segmentation fault. How can I fix this to make it work properly?

Restauraunt.c
This program allows you to create a restaurant menu, stores it in a file, and then rates each item on the menu. It uses file functions to output everything into a file that can then be viewed through almost any program. When the program gets to the line in nametofile() 'fprintf(restauraunt, "%s Restauraunt\n\n",name);' the program gives a segmentation fault. I do not know why it is doing this, I have attempted several different methods of debugging, but none have worked. If you have any suggestions, please comment them below.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
FILE *restauraunt;
char name[20];
char item[20];
char price[20];
int count=0;
void nametofile();
void rate();
void itemtofile();
void counter();
void renamefile();
int main()
{
int i,j;
int num;
printf("Restauraunt Creator\n\n");
printf("Enter the name of your restauraunt:\n");
scanf("%s",&name);
nametofile();
printf("\nEnter the number of items to be included in your menu:\n");
scanf("%d", &num);
/* Cycles through each entry to the menu */
for(i=0;i<num;i++)
{
counter();
fpurge(stdin);
printf("\nPlease enter the name of item number %d:\n",count);
scanf("%s", &item);
printf("\nPlease enter the price of item number %d:\n",count);
scanf("%s", &price);
itemtofile();
rate();
}
renamefile();
}
/*void nametofile()
{
restauraunt = fopen("restauraunt","w");
fprintf(restauraunt, "%s Restauraunt\n\n",name);
fclose(restauraunt);
}*/
/* The function that sends the restaurant name to the file */
void nametofile()
{
int i;
i = strlen(name);
name[i+1] = '\0';
restauraunt = fopen("restauraunt","w");
/* the line that gives a segmentation fault */
fprintf(restauraunt, "%s Restauraunt\n\n",name);
fclose(restauraunt);
}
/* rates each menu item */
void rate()
{
int rating;
srandom((unsigned)time(NULL));
restauraunt = fopen("restauraunt", "a");
rating = random() % 5 + 1;
fprintf(restauraunt,"Your food's rating was:\t%d stars!",rating);
switch(rating)
{
case 1:
{
fprintf(restauraunt," Here's why: Your food was not very good tasting and the price was ridiculously high.\n");
break;
}
case 2:
{
fprintf(restauraunt," Here's why: Your food was mildly good tasting and the price was too high.\n");
break;
}
case 3:
{
fprintf(restauraunt," Here's why: Your food was somewhat good tasting and the price was fair.\n");
break;
}
case 4:
{
fprintf(restauraunt," Here's why: Your food was quite good tasting and the price was very nice.\n");
break;
}
case 5:
{
fprintf(restauraunt," Here's why: Your food was very delicious and the price was amazingly low.\n");
break;
}
}
}
/* sends each item to the file */
void itemtofile()
{
restauraunt = fopen("restauraunt","a");
fprintf(restauraunt, "%s: $%s\nRating:",item,price);
fclose(restauraunt);
}
/* counts up one each time function is called */
void counter()
{
count += 1;
}
/* renames the file at the end */
void renamefile()
{
int x,y;
char bridge[] = { "menu" };
name[0] = tolower(name[0]);
x = strcat(name,bridge);
y = rename("restauraunt",name);
}
name is a char array. When you pass it to scanf or other functions, it decays to a pointer, so you do not need the & operator:
scanf("%19s", name);
When you read strings with scanf, it is a good idea to pass the size limit: this lets you avoid buffer overruns. Since name is declared as char[20], you pass 19, because one more char needs to be reserved for the null terminator.

Resources