I'm new to structures, our instructor haven't discussed it yet...
Im trying to make a program that will ask the user 5 "fruits".
Ex.
The user input:

Mango
Apple
Mango
Grapes
Grapes

Output:

Fruits entered: 2 Mango
1 Apple
2 Grapes

I've trying to see some tutorials but i can't find anything like this...
Can anyone help me? I would appreciate it...

#include <stdio.h>
#include <string.h>

struct fruit{
     char diet[20];
     int number;
     
     }name1,name2;

int main () 
{

        
        
                printf("Enter a fruit: ");
                scanf("%s",&name1.diet);
                
                printf("Enter a fruit: ");
                scanf("%s",&name2.diet);
                //.....
                
        printf("Fruits entered: %s",name2.diet);
        
        getchar();
        getchar();
        return 0;

}

I don't think you really need structs for this. If you've covered arrays, strings, and string functions like strcmp and strcpy you should be all set. Rather than keeping a count with the item, keep a count in an int array (match up the indexes with those of your strings),

Our next topic is about structures...
Im trying to make a program like that just to know more about struct...

1) Use consistent formatting. See this
2) You should know loops by now, use them.
3) I see no counters -- important is you want to count things
4) I see no comparisons -- also important for obvious (I hope) reasons.

If you don't know about 3&4, you're not ready for this program. Come up with something else to test out structures. Like a simple
Create a structure with 3 or 4 integer elements.
Input 3 or 4 integers into the structure elements.
Add/sub/mult different combinations of the elements and output answers.

Just an update:

#include <stdio.h>
#include <string.h>


struct food{
       char *fruit[15];
       int *number;
       }type[3];
       
int main()
{
    
    int i;
    int counter;
    
    
    for(i=0;i<3;i++)
    {
    
        printf("Enter a fruit: ");
        scanf("%s",type[i].fruit);
    
        printf("Enter a number: ");
        scanf("%d",&type[i].number);
   
        if(*type[i].fruit==*type[i+1].fruit)
        {
        counter = *type[i].number + counter + *type[i+1].number;
        }
    }
    
    for(i=0;i<3;i++)
    {
                    
    printf("\nFruit %s\n no. %d",type[i].fruit,type[i].number);
    
    }
    
    getchar();
    getchar();
}

Still i don't know how to add the number of fruits which have the same name...:(

Your formatting looks much better.

In addition to structures, you are also trying to learn counters, string comparisons, and somewhat difficult logic.

If you want to learn structures, use what you already know. Stop trying to learn it all at once. You should be able to program my suggestion in a short time because you already know input, output, and simple math.

#include <stdio.h>
#include <string.h>

struct food{
       char fruit[10];
       int number;
       }f[5];

main()
{
      int temp;
      int i, j;
      int k=0;
      
      /* Inputs */
      for(i = 0;i<5;i++)
      {
              printf("Enter fruit: ");
              scanf("%s",f[i].fruit);
              
              printf("Enter number of fruit(s): ");
              scanf("%d",&f[i].number);
      }
      
      /* Str Compare */
      
      for(i=0;i<5;i++)
      {
           
           for(j=1;j<5;j++)
           {   
               if(i!=j)
               {
                       if(strcmp(f[i].fruit ,f[j].fruit)==0)
                       {  
                          temp+= f[i].number+f[j].number;
     
                          
                       } 
               }
           }

       }    
      
      /* Output */ 
      printf("\nFruits\tNo.");
      for(i=0;i<5;i++)
      {
              
              printf("\n%s\t%d",f[i].fruit,f[i].number);
      }             
      getchar();
      getchar();
      
      return 0;
}

I still can't this problem....

I don't want to output the name twice when the user inputs.

Fruit : Mango
Number: 2

Fruit: Grapes
Number: 5

Fruit : Mango
Number : 1
Above code output:            
[B]
Fruits    No.                     
Mango     2                              
Grapes    5                        
Mango     3[/B]

 Desired output:
[B]
Fruits    No.
Mango     3
Grapes    5  [/B]

Why did you calculate temp? You just threw it away.

You might be better off getting the input into temp variables.

After reading one fruit, look through the array and
-- if the fruit is not there, put the values into the array
-- if the fruit is there, add the number to the the fruit number.
Then get the next fruit.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.