Menu For Side Order Didn't Pop - c

After user choose their pizza,they can't choose their side order as the menu for side order are missing.
Is there anything wrong with the looping?
It is suspected to have something to do with while looping.
int main()
{
int cont;
int cust;
int i;
int j;
double side=0.00;
double pizza=0.00;
double total=0.00;
for(cust=0;cust<5;cust++)
{
printf("Welcome To Pizza Hut\n");
printf("Pizza Menu :\n");
printf("1=Chicken\n");
printf("2=Meat\n\n");
printf("Enter Pizza Flavor : ");
scanf("%d",&i);
if(i==1)
{
pizza=5.50;
}
if(i==2)
{
pizza=4.50;
}
while(cont==1) /*This is where the menu for side order didn't show*/
{
printf("Side Order Menu :\n");
printf("1=coke\n");
printf("2=pepsi\n");
printf("3=bread\n");
printf("4=salad\n\n");
printf("Enter Side Order : ");
scanf("%d",&j);
if(j==1)
{
side=1.50;
}
if(j==2)
{
side=1.30;
}
if(j==3)
{
side=2.50;
}
if(j==4)
{
side=2.60;
}
printf("Add Order? (1=yes||0=no) : ");
scanf("%d",&cont);
}
}
total=pizza+side;
printf("Total : %.2f",total);
cust++;
return 0;
}

Here, you haven't given a value to the cont variable so while loop doesn't properly work.

I think there are some errors:
You should initialize the cont variable to 1
You should to add a breaking mechanism inside your while loop in order to escape from it
You should reset your cont variable to 1 after the while loop again.
This might should work:
int main()
{
int cont = 1;
int cust;
int i;
int j;
double side=0.00;
double pizza=0.00;
double total=0.00;
for(cust=0;cust<5;cust++)
{
printf("Welcome To Pizza Hut\n");
printf("Pizza Menu :\n");
printf("1=Chicken\n");
printf("2=Meat\n\n");
printf("Enter Pizza Flavor : ");
scanf("%d",&i);
if(i==1)
{
pizza=5.50;
}
if(i==2)
{
pizza=4.50;
}
while( cont == 1 ) /*This is where the menu for side order didn't show*/
{
printf("Side Order Menu :\n");
printf("1=coke\n");
printf("2=pepsi\n");
printf("3=bread\n");
printf("4=salad\n\n");
printf("Enter Side Order : ");
scanf("%d",&j);
if(j==0)
{
cont = 0;
break;
}
if(j==1)
{
side=1.50;
}
if(j==2)
{
side=1.30;
}
if(j==3)
{
side=2.50;
}
if(j==4)
{
side=2.60;
}
printf("Add Order? (1=yes||0=no) : ");
scanf("%d",&cont);
}
cont = 1;
}
total=pizza+side;
printf("Total : %.2f",total);
cust++;
return 0;
}

Where you have the line int cont;, replace it with int cont=1;. cont will have indeterminate value, so you'll never enter the loop to set it to anything else.

If I'm not wrong, when you say
while(cont==1)
You're saying, while cont it's equals to 1, proceed, and cont is never 1.
You have to initialize the variable count to 0 too.

Related

How to pass array of structure as a parameter of int function

I would like to pass array of structure as a parameter of int function but every time I try to do it. I get the [Error] expected ';', ',' or ')' before '.' token on line 19 where the int function name add is. If I change to int add(int totalCost,struct items *input,int quantity) there will be [Warning] passing argument 2 of 'add' makes pointer from integer without a cast.
#include<stdio.h>
#include<string.h>
struct items
{
int No;
char singleitem[29];
int cost;
};
struct items input[5] = { {0, "Aluminium-air battery",355}, { 1, "Bunsen cell",500}, { 2, "Dry cell",550 }, { 3,"Galvanic cell",350},
};
struct items* ptr = input;
int main()
{
int nonRechargeableBatteries,i=0,choice,c=1,a[10],totalCost=0,quantity=0;
int add(int totalCost,struct items input[i].cost,int quantity)
{
totalCost+=input[i].cost*quantity;
return totalCost;
}
for(i=0; i<5; i++)
a[i]=0;
do
{
//C is 1 by default
if(c==1)
{
printf("\npress number in front of command that you would like to do\n");
printf("1 - non-rechargeable batteries\n2 - cart summary\n");
printf("Enter : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
int nonRechargeableBatteries;
printf("\nselect various kind of battery to buy\n1 - Aluminium–air battery\n2 - Bunsen cell\n3 - Dry cell\nAny other number to exit\n");
scanf("%d",&nonRechargeableBatteries);
switch(nonRechargeableBatteries)
{
case 1:
{
int num;
printf("You chose Aluminium–air battery.\nPress 1 to add to the cart.\nAny other number to cancel\n");
scanf("%d",&num);
if(num==1)
{
printf("enter how many items you want : ");
scanf("%d",&quantity);
a[0]+=quantity;
totalCost = add(totalCost,input[0].cost,quantity);
}
break;
}
.
.
.
}
break;
}
case 2:
{
printf("No\tItems%-21sQuantity\tCost\n"," ");
for(i = 0 ; i<5 ; i++,ptr++)
{
if(a[i]!=0)
{
printf("%d\t%s%-4s%d\t\t%d\n",input[i].No,input[i].singleitem," ",a[i],(input[i].cost*a[i]));
}
}
printf("\nTotal Cost\t\t\t\t\t%d\n",totalCost);
printf("continue shopping Enter\n1 to Add Item\n2 to Delete Items\n3 Add or subtract existing item(s) in cart \nAny other number to Exit\n");
scanf("%d",&c);
}
default:
{
if(c==1)
printf("Enter Valid Categories Choice\n");
else;
break;
}
}
if(choice>0&&choice<2)
{
printf("No\tItems%-21sQuantity\tCost\n"," ");
for(i=0; i<5; i++)
{
if(a[i]!=0)
{
printf("%d\t%s%-4s%d\t\t%d\n",input[i].No,input[i].singleitem," ",a[i],(input[i].cost*a[i]));
}
}
printf("\nTotal Cost\t\t\t\t\t%d\n",totalCost);
printf("continue shopping Enter\n1 to Add Item\n2 to Delete Items\n3 Add or subtract existing item(s) in cart \nAny other number to Exit\n");
scanf("%d",&c);
}
else;
}
}
while(c==1 || c==2 ||c==3);
printf("Your total cost is %d\n",totalCost);}
If I change to int add(int totalCost,struct items *input,int quantity) there will be [Warning] passing argument 2 of 'add' makes pointer from integer without a cast.
Of course if you declare the function parameter input to be of type struct items *, you must not pass an int argument input[0].cost, but a struct items * as declared:
totalCost = add(totalCost, input, quantity);

My C code for a genetic algortihtm is not functioning, it dosent enter into the if condition

This is GA for a timetable problem. I'm trying to create an initial population, but it isn't working as it isn't entering the if condition. can someone point out the error?
I tried inserting statements in each condition, but everything checks out. Still, I don't seem to find a solution.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<time.h>
int random_number_creator(int upper, int lower)
{
int n;
n = rand() % (upper-lower)+ lower;
return n;
}
struct pop{
int subjects[6];
int days[5][9];
}oldpop, bench;
main()
{
int i,j,s=1,d,h,stop=1,cou=0;
for(i=0;i<5;i++)
{
for(j=0;j<9;j++)
if(j!=6)
bench.days[i][j]=0;
else
bench.days[i][j]=11111;
}
for(i=0;i<6;i++)
{
if(i<4)
oldpop.subjects[i]=3;
else
oldpop.subjects[i]=2;
}
for(i=0;i<5;i++)
{
printf("\n");
for(j=0;j<9;j++)
printf(" %d",bench.days[i][j]);
}
for(i=0;i<6;i++)
{
printf(" \n %d",oldpop.subjects[i]);
}
cou=0;
for(i=0;i<6;i++)
{
cou=cou+ oldpop.subjects[i];
}
printf("\n%d\n",cou);
do
{
s=random_number_creator(5,0);
printf("\nsubject number:%d\n",s);
printf("\nloop 1 entery %d",cou);
do
{
printf("\nloop 2 entry\n");
d=random_number_creator(5,0);h=random_number_creator(8,0);
printf("\nDay Number:%d \nHour Number:%d\n",d,h);
if(bench.days[d][h]==0&&oldpop.subjects[s]!=0)
{
printf("\nif condition reached\n");
oldpop.days[d][h]=10+s;
bench.days[d][h]=11111;
stop=0;
cou--;
oldpop.subjects[s]--;
}
else
{
printf("\nIf condition not satisified.\n");
break;
}
}while(stop!=0);
}while(cou!=0);
for(i=0;i<5;i++)
{
printf("final entery \n");
for(j=0;j<9;j++)
printf(" %d",oldpop.days[i][j]);
}
}
I want the oldpop variable to be initialized for this timetable problem but the code does not enter the if condition in the do while loop.
The problem in your program comes from the subject selection (after adding the missing }):
s=random_number_creator(5,0);
Will return a random number between 0 and 4 included.
To correct this, just replace this line by
s=random_number_creator(7,0);
To pick a number between 0 and 6. So the cou variable will be able to reach 0
Your code can be improved:
Instead of this kind of block:
for(i=0;i<5;i++)
{
printf("final entery \n");
for(j=0;j<9;j++)
printf(" %d",oldpop.days[i][j]);
}
Create a function, learn to use printf:
void print_table(struct pop pop, const char *label)
{
int i, j;
printf("\n%s\n", label);
for (i=0;i<5;i++)
{
for (j=0;j<9;j++)
{
printf(" %5d",pop.days[i][j]);
}
}
}
And use it this way
print_table(oldpop, "oldpop");

Function Crashes Main After Finishing Execution - C

I made this simple program that asks the user to input the number of columns the matrix called arp is going to have because, that way when the program asks the user to input a number so it can find matches on the numbers stored at the array without comparing all 10 columns allocated on memory with array to pointer type.
The problem here comes when the user inputs into the columns size definition 2. All works fine before the last function of p3() does its job, then it doesn't even return to main to execute the infinite loop defined there. I have tried removing the pointers and didn't work; I also tried removing other parts of the code but still nothing...
Update: Tried removing the function to find elements felmnt() and still the same.
Here is The Buggy Code:
#include <stdio.h>
#include <stdlib.h>
int loop = 1;
void keepalive(void)
{
int ckr = 0;
fflush(stdin);
printf("\n\n ******[s]<< CONTINUE | EXIT >>[n]******\n");
while(printf(" > ") && (ckr = getchar()) != 's' && ckr != 'n') fflush(stdin);
getchar();
if(ckr == 'n') loop = 0;
system("CLS");
}
void felmnt(int *colu, int (*arp)[10])
{
int nius=0, colmts, x, i, ejct;
do
{ // loop to let the user find more elements inside matrix
colmts=0;
printf("\n Insert The Number To Find\n > ");
scanf("%d", &nius);
for(x=0; x<*colu; x++) // search of element inside matrix
{
for(i=0; i<=9; i++)
if(nius == arp[i][x])
colmts++;
}
if(colmts>1) // results printing
{
printf("\n %d Elements Found", colmts);
}else if(colmts)
{
printf("\n 1 Element Found");
}else
{
printf("\n Not Found");
}
printf("\n TRY AGAIN? s/n\n > ");
ejct=getchar();
getchar();
}while(ejct=='s');
}
void mat(int *colu, int (*arp)[10])
{
int ci, cn, tst=0;
for(ci=0; ci<*colu; ci++)
{
for(cn=0; cn<10; cn++)
{
printf("\n Input The Number [%d][%d]\n > ", ci+1, cn+1);
scanf(" %d", &arp[cn][ci]);
}
}
printf(" Numbers Inside Matrix> ");
for(ci = 0; ci<*colu; ci++)
{
for(cn=0; cn<10; cn++) printf(" %d", arp[cn][ci]);
}
}
void p3(void)
{ // >>>>main<<<<
int colu=0;
while(loop)
{
printf("\n Input The Quantity Of Columns To Use\n > ");
scanf("%d", &colu);
int arp[10][colu];
mat(&colu, arp);
felmnt(&colu, arp);
keepalive();
}
}
int main(void)
{
int ck = 0, ndx;
while(1)
{ // infinite loop
p3();
fflush(stdin);
printf("\nPause !!!");
getchar();
}
return 0;
}

DFA is not accepting strings

my program is supposed to implement a dfa for a binary string... dfa is a machine that can be in only one state at a time (when you enter a string the machine should use it and at the last step it should reach to the final state. if so, we say that string is accepted by the machine) ...
this machine works this way:
at first program asks the number of states, if u consider my pic, u see that it has 3 states(q0,q1,q2) so we enter 3. then it asks about the number of inputs. my inputs are 0,1 so I enter 2... then it asks to enter the inputs, we enter 0 then 1! then it asks the number of final states, the final state here is only q1... so we enter it... then u see this: (q0,0) = q it means q0 goes to which state with 0... for example here q0 goes to q0 with 0 ... others are like that. after filling this part, we enter the sring and it would say if string is valid or not
here is the code:
#include<stdio.h>
#include<conio.h>
int ninputs;
int check(char,int ); //function declaration
int dfa[10][10];
char c[10], string[10];
int main()
{
int nstates, nfinals;
int f[10];
int i,j,s=0,final=0;
printf("enter the number of states that your dfa consist of \n");
scanf("%d",&nstates); // 3
printf("enter the number of input symbol that dfa have \n");
scanf("%d",&ninputs); // 2
printf("\nenter input symbols\t");
for(i=0; i<ninputs; i++)
{
printf("\n\n %d input\t", i+1);
printf("%c",c[i]=getch()); // 01
}
printf("\n\nenter number of final states\t");
scanf("%d",&nfinals); // 1
for(i=0;i<nfinals;i++)
{
printf("\n\nFinal state %d : q",i+1);
scanf("%d",&f[i]); // 1
}
printf("-----------------------------------------------------------------------");
printf("\n\ndefine transition rule as (initial state, input symbol ) = final state\n");
for(i=0; i<ninputs; i++)
{
for(j=0; j<nstates; j++)
{
printf("\n(q%d , %c ) = q",j,c[i]);
scanf("%d",&dfa[i][j]);
// q(0,0)=0
// q(1,0)=0
// q(2,0)=2
// q(0,1)=1
// q(1,1)=2
// q(2,1)=1
}
}
do
{
i=0;
printf("\n\nEnter Input String.. ");
scanf("%s",string); // 01
while(string[i]!='\0')
{
if((s=check(string[i++],s))<0)
break;
for(i=0 ;i<nfinals ;i++)
{
if(f[i] ==s )
final=1;
if(final==1)
printf("\n valid string");
else
printf("invalid string");
getch();
printf("\nDo you want to continue.? \n(y/n) ");
}
}
}
while(getch()=='y');
getch();
}
int check(char b,int d)
{
int j;
for(j=0; j<ninputs; j++)
if(b==c[j])
return(dfa[d][j]);
return -1;
}
the problem is that when I enter my string, the program says it's invalid, however it should be accepted by the machine... for example consider the machine in the photo and test this string on it: 01
so what's wrong with the code?
Optimized C code for Accepting String using DFA
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int a,b,i,j,k,state,ch;
char s[10][10],*st,v[10],ss[10];
printf("Enter the number of state:\n");
scanf("%d",&a);
printf("Enter State :\n");
for(i=0;i<a;i++)
{
fflush(stdin);
scanf("%c",&ss[i]);
}
printf("Enter th no. of var..:\n");
scanf("%d",&b);
printf("Enter variable :\n");
for(i=0;i<b;i++)
{
fflush(stdin);
scanf("%c",&v[i]);
}
printf("Enter table:\n");
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
fflush(stdin);
scanf("%c",&s[i][j]);
}
}
printf("Enter string :\n");
fflush(stdin);
gets(st);
i=0;
state=0;
while(st[i]!='\0')
{
for(j=0;j<b;j++)
{
if(st[i]==v[j])
{
if(s[state][j]=='-')
{
goto check;
}
else
{
for(k=0;k<a;k++)
{
if(s[state][j]==ss[k])
{
printf("State:%c\n",s[state][j]);
state=k;
goto o;
}
}
}
o:
}
}
i++;
}
check:
ch=1;
for(i=0;i<b;i++)
{
if(s[state][i]!='-')
{
ch=0;
}
}
if(ch==1)
{
printf("String is matching..");
}
else
{
printf("String is not matching..");
}
getch();
return 0;
}
Just put an space before %c . And edit some lines of the code.
This code is successfully complied on Code::Blocks 13.12 version.
As it C++ Compiler save the file with .cpp extension.
And Click On the " output image " . You will see how to input & output from
console
<--------Here Is the edited code && It will work fine ---> #
output image
#include<stdio.h>
#include<cstdio>
#include<iostream>
int ninputs,bb;
int check(char,int ); //function declaration
int dfa[10][10];
char c[10], string[10],b;
int main()
{
int nstates, nfinals;
int f[10];
int i,j,s=0,final=0;
printf("enter the number of states that your dfa consist of \n");
scanf("%d",&nstates);
printf("enter the number of input symbol that dfa have \n");
scanf("%d",&ninputs);
printf("\nenter input symbols");
for(i=0; i<ninputs; )
{
bb =i;
printf("\n %d input",bb+1);
// printf(" %c",c[i]=getchar());
scanf(" %c" , &c[i]); //just put an space before %c
i++;
}
printf("\n\nenter number of final states\t");
scanf("%d",&nfinals);
for(i=0;i<nfinals;i++)
{
printf("\n\nFinal state %d : q",i+1);
scanf("%d",&f[i]);
}
printf("-----------------------------------------------------------------------");
printf("\n\ndefine transition rule as (initial state, input symbol ) = final state\n");
for(i=0; i<ninputs; i++)
{
for(j=0; j<nstates; j++)
{
printf("\n(q%d , %c ) = q",j,c[i]);
scanf("%d",&dfa[i][j]);
}
}
do
{
i=0;
printf("\n\nEnter Input String.. ");
scanf("%s",string);
while(string[i]!='\0')
if((s=check(string[i++],s))<0)
break;
for(i=0 ;i<nfinals ;i++)
if(f[i] ==s )
final=1;
if(final==1)
printf("\n valid string");
else
printf("invalid string");
printf("\nDo you want to continue.? \n(y/n) ");
}
while(b =='y');
scanf(" %c", &b); // chnge here
}
int check(char b,int d)
{
int j;
for(j=0; j<ninputs; j++)
if(b==c[j])
return(dfa[d][j]);
return -1;
}
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
main()
{
int state,symbol;
char str[30];
cout<<"Enter the string which you want to check :: "<<endl;
fgets(str,sizeof(str),stdin);
cout<<"Enter the Number of state :: ";cin>>state;
cout<<"Enter the Number of input symbol :: ";cin>>symbol;
cout<<"Enter the states : "<<endl;
char st[state];
for(int i=0;i<state;i++)
{
cout<<"state : "<<i<<" : ";cin>>st[i];
}
cout<<"Enter the input symbol : "<<endl;
char sy[symbol];
for(int i=0;i<symbol;i++)
{
cout<<"symbol : "<<i<<" : ";cin>>sy[i];
}
char table[state][symbol];
cout<<"------Enter next move or state------ if no relation put -"<<endl;
for(int i=0;i<state;i++)
{
for(int j=0;j<symbol;j++)
{
cout<<"("<<st[i]<<", "<<sy[j]<<") = ";cin>>table[i][j];
}
}
char ststate,fnstate;
cout<<"Start state :: ";cin>>ststate;
cout<<"Final state :: ";cin>>fnstate;
cout<<"----------------------------------"<<endl;
int str_len=strlen(str);
int p,q;
int flag=1;
int j=0;
for(j=0;j<str_len-1;j++)
{
cout<<"input state"<<endl;
for(int i=0;i<state;i++)
{
if(st[i]==ststate)
{
p=i;
cout<<p<<endl;
break;
}
else
{
p=-1;
}
}
cout<<"input string"<<endl;
for(int i=0;i<state;i++)
{
if(sy[i]==str[j])
{
q=i;
// cout<<q<<endl;
break;
}
else
{
q=-1;
}
}
if(p!=-1 && q!=-1)
{
cout<<"table output :: "<<table[p][q]<<endl;
ststate=table[p][q];
}
}
cout<<endl;
cout<<"----------------------------------------------------------------------------------";
if((table[p][q]==fnstate) && (j==str_len-1))
{
cout<<"String is recognized"<<endl;
flag=0;
}
if(flag!=0)
{
cout<<"String is not recognized.";
}
cout<<endl;
return 0;
}
This part of the code:
while(string[i]!='\0')
{
if((s=check(string[i++],s))<0)
break;
for(i=0 ;i<nfinals ;i++)
{
uses and changes i in both loops. You need to use something else in one of them.

Error 10 error C2371: 'print_plant' : redefinition; different basic types [C]

I wrote the following code:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
//globals/consts:
#define NAME_SIZE 40
#define COLOR_LEN 20
#define HABITAT_LEN 12
#define LEN 4
//structs:
struct plant
{
char name[NAME_SIZE];
int BloomingAreas[3];//0-Not found in this area
/*
0=South
1=Center
2=North
*/
char color[COLOR_LEN];
int habitat[HABITAT_LEN];//0- Not available this month
int edible;
//0-Inedible
//1 Edible
//2 Unknown
int protected_plant;
//0-Unprotected
//1-Protected
//2-Unknown
};
int comp(struct plant p1,struct plant p2)
{
int i;
int falgHabitat=0;
int flag=0;
//BloomingAreas comper:
if(p1.BloomingAreas[0]!=0 && p2.BloomingAreas[0]!=0)
flag++;
else if(p1.BloomingAreas[1]!=0 && p2.BloomingAreas[1]!=0)
flag++;
else if(p1.BloomingAreas[2]!=0 && p2.BloomingAreas[2]!=0)
flag++;
//Color comper:
if(strcmp(p1.color,p2.color)==0)
{
flag++;
}
else if(strcmp(p2.color,"all")==0)
{
flag++;
}
//Habitat comper:
for(i=0;i<HABITAT_LEN;i++)
{
if(p1.habitat[i]==p2.habitat[i])
{
falgHabitat=1;
}
}
if(falgHabitat==1)
{
flag++;
}
//Edible comper:
if(p1.edible==p2.edible)
{
flag++;
}
//protected_plant:
if(p1.protected_plant==p2.protected_plant)
{
flag++;
}
}
void comparison(struct plant user,struct plant arr[])
{
int i;
int flag;
int temp=0;
for(i=0;i<LEN;i++)
{
flag=comp(user,arr[i]);
if(flag==1)//Are the same
{
printf("According to the statistics there is a chance that this is the plant you're looking for:\n");
print_plant(arr[i]);
}
else
{
temp++;
}
}
if(temp==0)
{
printf("No results\n");
}
getch();
}
void input(struct plant* user)
{
int i=0;
int tempInt;
char temp=NULL;
int flag=1;
//BloomingAreas input:
printf("\n");
printf("Please enter where you saw the plant:\n");
printf("Insert 1 choice in south\n");
printf("Insert 2 choice in central\n");
printf("Insert 3 choice in north\n");
printf("Insert ? if you do not remember\n");
printf("ENTER or any other key to end\n");
while(flag)//If you inserted a question mark or enter Stop the position input
{
temp=getch();
if(temp=='1')
user->BloomingAreas[0]=1;
else if(temp=='2')
user->BloomingAreas[1]=1;
else if(temp=='3')
user->BloomingAreas[2]=1;
else if(temp=='?')
{
user->BloomingAreas[0]=1;
user->BloomingAreas[1]=1;
user->BloomingAreas[2]=1;
flag=0;
}
else//Enter or any other key
flag=0;
}
//color input:
printf("\n");
(user->color)[0]=NULL;
(user->color)[1]=NULL;
printf("Please enter the color of the plant:\n");
printf("only lowercase letters\n");
printf("If you do not know insert ?\n");
scanf("%s", user->color);
if(user->color[0]=='?')
{
user->color[0]=NULL;
strcpy(user->color,"all");
}
//habitat input:
printf("\n");
printf("Please enter the numbers of the months you've seen the plant (ENTER to end)\n");
printf("Enter ? If you do not remember the numbers of the months\n");
printf("Enter %d to end input\n",HABITAT_LEN+1);
do{
scanf("%d",&tempInt);
if(tempInt=='?')
{
for(i=0;i<HABITAT_LEN;i++)
user->habitat[i]=1;
}
else if(tempInt>0 && tempInt<13)
{
user->habitat[tempInt-1]=1;
}
}while(tempInt!=(HABITAT_LEN+1));
//edible input:
printf("\n");
printf("Do you know for sure that the plant edible?\n");
printf("(Do not check this if you are not sure!!!)\n");
printf("Insert the number 1 if the plant is edible\n");
printf("Insert the number 2 if the plant is't edible\n");
printf("Insert the number 3 if you do not know\n");
temp=getch();
if(temp=='1')
user->edible=1;
else if(temp=='2')
user->edible=0;
else
user->edible=2;
//protected_plant input:
printf("\n");
printf("Do you know if this is a protected plant?\n");
printf("Insert 1 if it is\n");
printf("Insert 2 if it is not\n");
printf("Insert 3 if you do not know\n");
temp=getch();
if(temp=='1')
user->protected_plant=1;
else if(temp=='2')
user->protected_plant=0;
else
user->protected_plant=2;
}
void print_plant (struct plant p)
{
int i;
printf("name: %s\n",p.name);
printf("Blooming areas:\n");
if(p.BloomingAreas[0]!=0)
printf("south,");
if(p.BloomingAreas[1]!=0)
printf("central,");
if(p.BloomingAreas[2]!=0)
printf("North,");
printf("\d");
printf("\n");
printf("color: %s\n", p.color);
printf("habitat:");
for(i=0;i<HABITAT_LEN;i++)
{
if(p.habitat[i]!=0)
printf("%3d,",i+1);
}
printf("\d");
printf("\n");
if(p.edible==0)
{
printf("Edible: on\n");
}
else
{
printf("Edible: yes\n");
}
if(p.protected_plant==0)
{
printf("protected plant: no\n");
}
else
{
printf("protected plant: yes\n");
}
}
void putInStruct(struct plant arr[])
{
//plant number 1:
strcpy(arr[0].name,"Wild Marjoram");
arr[0].BloomingAreas[2]=1;
strcpy(arr[0].color,"white");
arr[0].habitat[3]=1;
arr[0].habitat[4]=1;
arr[0].habitat[5]=1;
arr[0].habitat[6]=1;
arr[0].habitat[7]=1;
arr[0].habitat[8]=1;
//not edible
//So no need to change anything
arr[0].protected_plant=1;
//plant number 2:
strcpy(arr[1].name,"Aleppo Jerusalem pine");
arr[1].BloomingAreas[2]=1;
strcpy(arr[1].color,"green");
arr[1].habitat[1]=1;
//not edible
//So no need to change anything
arr[1].protected_plant=1;
//plant number 3:
strcpy(arr[2].name,"Acacia strap flower");
arr[2].BloomingAreas[0]=1;
strcpy(arr[2].color,"red");
arr[2].habitat[1]=1;
//not edible
//So no need to change anything
//No protected plant so no need to change anything
//plant number 4:
strcpy(arr[4].name,"Common fig tree");
arr[4].BloomingAreas[0]=1;
arr[4].BloomingAreas[1]=1;
arr[4].BloomingAreas[2]=1;
strcpy(arr[3].color,"green");
arr[3].habitat[2]=1;
arr[3].habitat[3]=1;
arr[3].habitat[4]=1;
arr[3].habitat[5]=1;
arr[3].edible=1;
//No protected plant so no need to change anything
}
void zero(struct plant arr[])
{
int i,j;
for(i=0;i<LEN;i++)
{
for(j=0;j<NAME_SIZE;j++)
arr[i].name[j]=0;
for(j=0;j<3;j++)
arr[i].BloomingAreas[j]=0;
for(j=0;j<COLOR_LEN;j++)
arr[i].color[j]=0;
for(j=0;j<HABITAT_LEN;j++)
arr[i].habitat[j]=0;
arr[i].edible=0;
arr[i].protected_plant=0;
}
}
int main(void)
{
struct plant arr[LEN];
struct plant user;
zero(arr);
putInStruct(arr);
input(&user);
comparison(user,arr);
}
And it gave me the following error:
Error 10 error C2371: 'print_plant' : redefinition; different basic types d:\my documents\visual studio 2010\projects\rrr\rrr\dasd.c 205
What am I supposed to do?
You called the function before even declaring it. Move the declaration of print_plant above the code using it.

Resources