can you all helping correct this code. this is my first time learn c language.

0 nur syazwani · April 12, 2015
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

void addrecipe();

void viewrecipe();

struct recipe
{
    char name[30];
    char ingredients[500];
    char ways[700];
};

int main()
{
    int choice;

    printf("\n\n\t***********************************\n");
    printf("\t*WELCOME TO YOUR RECIPE BOOK*\n");
    printf("\t***********************************");

    while (1)
    {
        printf("\n\n\t\tMAIN MENU:");

        printf("\n\n\tADD RECORD\t[1]");

        printf("\n\tVIEW RECORD\t[2]");

        printf("\n\tEXIT\t\t[3]");

        printf("\n\n\tENTER YOUR CHOICE:");

        scanf("%d",&choice);

        switch(choice)
        {
        case 1:
            addrecipe();
            break;

        case 2:
            viewrecipe();
            break;

        case 3:
            printf("\n\n\t\tTHANK YOU FOR USING THE SOFTWARE ");
            getch();
            exit(0);

        default:
            printf("\nYOU ENTERED WRONG CHOICE..");
            printf("\nPRESS ANY KEY TO TRY AGAIN");
            getch();
            break;
        }
        system("cls");
    }
    return 0;
}

void addrecipe()
{
    system("cls");

    FILE *pFile;

    char another='Y';

    struct recipe chef;

    pFile=fopen("abc.txt","w");

    if(pFile==NULL)
    {
        printf("Error!");
        exit(1);
    }
    printf("\tEnter name of recipe : ");
    scanf("%c",&chef.name);
    fflush(stdin);

    printf("\tEnter the ingredient : ");
    scanf("%c",&chef.ingredients);
    fflush(stdin);

    printf("\tEnter the ways : ");
    scanf("%c",&chef.ways);
    fflush(stdin);

    printf("\nYOUR RECIPE IS ADDED...\n");

    printf ( "\n\tADD ANOTHER RECORD...(Y/N) " );
    another=getchar();
}

void viewrecipe()
{
    FILE *pFile;

    system("cls");

    struct recipe chef;

    printf("\n\n\t\t*******************************\n");

    printf("\t\t* HERE IS THE VIEWING MENU *");

    printf("\n\t\t*******************************\n\n");

    if((pFile=fopen("abc.txt","r"))==NULL)
    {
        printf("Error! Opening file");
        exit(1);
    }
    printf("Menu : %c",chef.name);
    printf("Ingredient : %c",chef.ingredients);
    printf("Ways : %c",chef.ways);
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Mahesh Sunuwar · April 13, 2015
It would have been great if you had added what you were trying to do
0 http://coding.developer.se/   · April 14, 2015
scanf("%c",&chef.name);


Did you perhaps mean to write this?

scanf("%29s", chef.name);

...

scanf("%699s", chef.ways);


I don't know... That doesn't seem right, either. I suggest reading the scanf manual thoroughly before you use it, similarly to how you would read the MSDS for a dangerous chemical before you use it for the first time.

I'm going to assume you know how to use a search engine, so I won't bother linking you to the scanf manual. Note that I'm clearly stating that my intention is to assume you are intelligent here. If you read it as though my intention is to assume that you're retarded, then you are committing an intentional fallacy:   http://en.wikipedia.org/wiki/Authorial_intent#New_Criticism

Don't blame me for the tone that you invent in your head. I'm just trying to convey factual information to help you...

Do not use fflush(stdin). Why not?   http://stackoverflow.com/questions/2979209/using-fflushstdin
0 Clark Alaan · April 14, 2015
Building on what coding.developer.se's reply, I suggest you use fgets instead.

Here's a link to an example:
http://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm
Most of the info above example may confuse you so don't mind them too much.

Here's how you will probably use it.

fgets(chef.name, 30, stdin);

Bucky also has a video on it here: https://www.thenewboston.com/videos.php?cat=14&video=20064
This will help the user from inputting too long of a name and messing your program up.

Hope we helped you.
0 Elvis Livingston · April 22, 2015
Hey what's up?
i"ve been stuck in this C code for over a week and cant seem to move on. I am new to programming.
Could you tell me please why my loop is not stopping where it should and why cant I loop and make more entries?

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

int main()
{

//declaring variables to be used in program
int option; //to be used in menu
int choice; //to continue or exit the case
int total=0; //total for claim amount
int count = 0;
int highest_pol=0;
int highest_claim=0;
int z;
int claim;//variable to input claim amount
int policy;//variable to input policy number

//initializing and inputting a value of zero in the array
int policy_num[15];
for(z=0;z<=15;z++)
policy_num[z]=0;

int claim_amt[15];
for(z=0;z<=15;z++)
claim_amt[z]=0;

while (option!= 0) //allows the user to return to menu after case (or switch)
{


printf ("PLEASE SELECT AN OPTION\n\n");
printf ("Option 1: Enter Claim Data\n");
printf ("Option 2: Print Total Claims Report\n");
printf ("Option 3: Print Policy Number with Highest Claim\n");
printf ("Option 4: Exit System\n\n");
printf ("You have selected option #:");
scanf("%d", &option);

system("cls");
switch(option)
{
case 1:

//the first while loop should check how many entries have been entered and start overwriting  the first index in the array that has an element of zero. Then when the array is full it displays an error message "database full". Other wise it should loop for more entries.
z = 0;  
while (policy_num[z] =0 && z<15);
                    z++;
  if (z==15)
{
printf("Sorry, The Database is Full \n\n");
system("pause \n\n");
}
else
printf( " Do you want to continue? press 1 for Yes, or 2 to No\n");
scanf("%d",&choice);
while (choice != 2 && count<15)

              {            
   printf("Claims Report \n");
           printf("------------------------\n");
           printf ("Please Enter policy number\n");
           scanf ("%d",&policy);
   printf ("Please Enter Claim Amount\n");
   scanf ("%d",&claim);
        policy_num[z]=policy;
       claim_amt[z]=claim;
       z++;
}
            if (claim>highest_claim)
highest_claim = claim;
            if (policy>highest_pol)
highest_pol = policy;
            total = total + claim;
            count++; 
            system ("cls");
     
        break;

case 2:
system ("cls");
printf("Total Claim Report\n");
printf("------------------------\n");
for (z=0; z<15;z++)
{
                printf("Policy Number %d \n",policy_num[z]);
printf("Claim Amount  %d \n",claim_amt[z]);
            }

printf("The total amount of claims are %d\n",count);
printf("The total claim amount is $%d\n",total);
   printf("\n");
   printf("Enter 2 to exit to Menu \n"); //in reality any number can exit the case
   scanf("%d" , &option);
   system("cls");
break;

case 3:
system ("cls \n");
   printf("The Policy Number with Highest Claim is %d \n",highest_pol);
   printf("The Highest Claim Amount is %d \n",highest_claim);
printf("\n");
printf("Press 2 to exit to Menu\n"); //any number can exit the case
scanf("%d" , &option);
system("cls");
break;

case 4:
system("cls\n");
printf("Thank you \n\n");
system("pause");
exit(0);

default:            
       printf("invalid entry, exiting system!\n");
       exit(0);
        
}

}

getch ();
return(0);
}
  • 1

C

106,980 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator