First of all I want to thank the guys form this forum for there work and help that I found more then use full an different occasions.
Here is my c programing exercise.
Write a c program that shows all the products tahat have the price over 10.
Any help would be apreciated
This is how I thought I could make the program

#include<stdio.h>
int nr_products;//initialization
int nr_prices;
char products;
int price;
int main()
{
	

nr_products=nr_prices;
if(nr_products=2)
printf("No products");
scanf("%d",&nr_products);
printf("Products:");//enter the products
scanf("%c",products);
printf("Price:");//enter price
scanf("%d",&price);
	if(price>10)//condition over 10
	{
		printf("Products that have the price over 10 are",products);//products over 10

	}	
else printf("Eroare");//not found nay products over 10
getch();
}

The program uses nr_products and nr_prices, without having given them values! :(
These variables were declared BUT NOT initialized to any value.

if statements need TWO == in them, instead of just one =.

You need a for loop (best) to control how many times a price needs to be entered.

products should be a string, instead of just one char.

Lots of work to do on this program.

The program uses nr_products and nr_prices, without having given them values! :(
These variables were declared BUT NOT initialized to any value.

if statements need TWO == in them, instead of just one =.

You need a for loop (best) to control how many times a price needs to be entered.

products should be a string, instead of just one char.

Lots of work to do on this program.

Can you show me who to do it
I'm still a newbie to C

I tried to take your advice and use a for instead of if but I don't think taht worked very well
Thank you for the ideea

#include<stdio.h>
int nr_products;//initialization
int nr_prices;
char products;
int price;
int main()
{
	




printf("No products");
scanf("%d",&nr_products);
printf("Products:");//enter the products
scanf("%c",products);

printf("Price:");//enter price
scanf("%d",&price);
for(nr_products=nr_prices;nr_products<=2;nr_products==nr_prices)//use for to enter nr_produts
	if(price>10)//condition over 10
	{
		printf("Products that have the price over 10 are",products);//products over 10

	}	
else printf("Eroare");//not found nay products over 10
getch();
}
#include<stdio.h>


int main()
{
  int nr_products, nr_prices, price, over10=0;
  char product[40]={'\0'};

  printf("Enter the number of products");
  scanf("%d",&nr_products);
  getchar();

  printf("Product:");//enter the product
  fgets(product, sizeof(product), stdin);

  printf("Price:");//enter price
  scanf("%d",&price);
  getchar();
  for(i=0; i<nr_products;i++) 
  { 
    if(price>10)//condition over 10
    {
      printf("\nThis product has a price over 10: %s",product);//product over 10
      over10++;
    }	
  }
  if(over10==0) 
  { 
    printf("\nNo product cost over 10."); 
  }
  else {
    if(over10==1)
      ch='';
    else
      ch='s';
    printf("%d product%c cost over 10.", over10, ch);
  }
  return 0;
}

Not a complete solution to what you need perhaps, but a good start. I have not tested/debugged any of the above.

#include<stdio.h>


int main()
{
  int nr_products, nr_prices, price, over10=0;
  char product[40]={'\0'};

  printf("Enter the number of products");
  scanf("%d",&nr_products);
  getchar();

  printf("Product:");//enter the product
  fgets(product, sizeof(product), stdin);

  printf("Price:");//enter price
  scanf("%d",&price);
  getchar();
  for(i=0; i<nr_products;i++) 
  { 
    if(price>10)//condition over 10
    {
      printf("\nThis product has a price over 10: %s",product);//product over 10
      over10++;
    }	
  }
  if(over10==0) 
  { 
    printf("\nNo product cost over 10."); 
  }
  else {
    if(over10==1)
      ch='';
    else
      ch='s';
    printf("%d product%c cost over 10.", over10, ch);
  }
  return 0;
}

Not a complete solution to what you need perhaps, but a good start. I have not tested/debugged any of the above.

Thank you very much for the help

#include<stdio.h>


int main()
{
  int nr_products, nr_prices, price, over10=0;
  char product[40]={'\0'};

  printf("Enter the number of products");
  scanf("%d",&nr_products);
  getchar();

  printf("Product:");//enter the product
  fgets(product, sizeof(product), stdin);

  printf("Price:");//enter price
  scanf("%d",&price);
  getchar();
  for(i=0; i<nr_products;i++) 
  { 
    if(price>10)//condition over 10
    {
      printf("\nThis product has a price over 10: %s",product);//product over 10
      over10++;
    }	
  }
  if(over10==0) 
  { 
    printf("\nNo product cost over 10."); 
  }
  else {
    if(over10==1)
      ch='';
    else
      ch='s';
    printf("%d product%c cost over 10.", over10, ch);
  }
  return 0;
}

Not a complete solution to what you need perhaps, but a good start. I have not tested/debugged any of the above.

I get an error on ch it is an line 33 can you explian to me what is ch because the c compiler dosen't understand what type it is.
Thank you

'ch' is not defined in the code. But, it's a character. It'd look like this:

#include<stdio.h>
 
 
int main()
{
  int nr_products, nr_prices, price, over10=0;
  char product[40]={'\0'};
  char ch = '';
 
  printf("Enter the number of products");
  scanf("%d",&nr_products);
  getchar();
 
  printf("Product:");//enter the product
  fgets(product, sizeof(product), stdin);
 
  printf("Price:");//enter price
  scanf("%d",&price);
  getchar();
  for(i=0; i<nr_products;i++) 
  { 
    if(price>10)//condition over 10
    {
      printf("\nThis product has a price over 10: %s",product);//product over 10
      over10++;
    }	
  }
  if(over10==0) 
  { 
    printf("\nNo product cost over 10."); 
  }
  else {
    if(over10==1)
      ch='';
    else
      ch='s';
    printf("%d product%c cost over 10.", over10, ch);
  }
  return 0;
}

'ch' is not defined in the code. But, it's a character. It'd look like this:

#include<stdio.h>
 
 
int main()
{
  int nr_products, nr_prices, price, over10=0;
  char product[40]={'\0'};
  char ch = '';
 
  printf("Enter the number of products");
  scanf("%d",&nr_products);
  getchar();

  printf("Product:");//enter the product
  fgets(product, sizeof(product), stdin);
 
  printf("Price:");//enter price
  scanf("%d",&price);
  getchar();
  for(i=0; i<nr_products;i++) 
  { 
    if(price>10)//condition over 10
    {
      printf("\nThis product has a price over 10: %s",product);//product over 10
      over10++;
    }	
  }
  if(over10==0) 
  { 
    printf("\nNo product cost over 10."); 
  }
  else {
    if(over10==1)
      ch='';
    else
      ch='s';
    printf("%d product%c cost over 10.", over10, ch);
  }
  return 0;
}

I understood that ch is declared as a char but the compilers does not understand that, can you help me to compile this program with no errors thanks in advance

Edited 6 Years Ago by dany12: n/a

Compiled this, changed the empty character constants to null (\0) chars. Also, i was not defined.

#include<stdio.h>


int main()
{
  int nr_products, nr_prices, price, over10=0, i;
  char product[40]={'\0'};
  char ch = '\0';

  printf("Enter the number of products\n");
  scanf("%d",&nr_products);
  getchar();

  printf("Product:");//enter the product
  fgets(product, sizeof(product), stdin);

  printf("Price:");//enter price
  scanf("%d",&price);
  getchar();
  for(i=0; i<nr_products;i++)
  {
    if(price>10)//condition over 10
    {
      printf("\nThis product has a price over 10: %s\n",product);//product over 10
      over10++;
    }
  }
  if(over10==0)
  {
    printf("\nNo product cost over 10.\n");
  }
  else {
    if(over10==1)
      ch='\0';
    else
      ch='s';
    printf("%d product%c cost over 10.\n", over10, ch);
  }
  return 0;
}

Compiled this, changed the empty character constants to null (\0) chars. Also, i was not defined.

#include<stdio.h>


int main()
{
  int nr_products, nr_prices, price, over10=0, i;
  char product[40]={'\0'};
  char ch = '\0';

  printf("Enter the number of products\n");
  scanf("%d",&nr_products);
  getchar();

  printf("Product:");//enter the product
  fgets(product, sizeof(product), stdin);

  printf("Price:");//enter price
  scanf("%d",&price);
  getchar();
  for(i=0; i<nr_products;i++)
  {
    if(price>10)//condition over 10
    {
      printf("\nThis product has a price over 10: %s\n",product);//product over 10
      over10++;
    }
  }
  if(over10==0)
  {
    printf("\nNo product cost over 10.\n");
  }
  else {
    if(over10==1)
      ch='\0';
    else
      ch='s';
    printf("%d product%c cost over 10.\n", over10, ch);
  }
  return 0;
}

how can I rate you?

Thanks guys for the help I hope that I can help you sometime like you helped me.

This question has already been answered. Start a new discussion instead.