Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I'm currently writing a program that sort of functions like an order invoice. The user inputs the price of the items and the number of items he wants.

Example
1.42 - Price
2.25
30.21
0 200 -Item Number and Quatity
1 400
2 100

So my problem is how do I differentiate between the Price inputs and Item Number, Quantity input?

What I have tried:

I have written

Objective-C
#include <stdio.h>

int main(void)
{
	
	int item ;
	float value;
	float price[300];
	for (item = 0;( Missing Condition to exit);item++)
	{
		scanf("%f",&value);	
		price[item]=value;
		      
			
	
	}	
	
	
}	


Which works for inputting the item price.
I do not know how to transition to Item Number and Quantity inputs.
Posted
Updated 27-Oct-17 5:38am
Comments
Rick York 27-Oct-17 11:35am    
It appears to me the differences are a price has a decimal point in the string and an item number has a space in the string. That would be one place to start.
Rick York 27-Oct-17 13:25pm    
Try this :

gets( buffer ); // obtain a string from the user
if( strchr( buffer, ' ' ) )
{
// space detected - read item and quantity
}
else
{
// no space - must be a price
}

Assuming your input is without remarks, i.e.
1.42
2.25
30.21
0 200
1 400
2 100

You could read the whole line, the test if it contains two integers (no. and quantity) or just a float (price). That is
C
#include <stdio.h>

#define ITEMS 300
int main(void)
{
  float price[ITEMS];
  int items = 0;

  int item_no;
  int item_qty;

  char line[80];

  while ( fgets(line, sizeof(line), stdin) && items < ITEMS )
  {
    if ( sscanf(line, "%d %d", &item_no, &item_qty) == 2)
    {
      if  (item_no < items)
        printf("item n. %d, item quatity %d, price = %f\n", item_no, item_qty, price[item_no] * item_qty);
    }
    else
    {
      if ( sscanf(line, "%f", &price[items]) == 1)
        ++items;
    }
  }
  return 0;
}
 
Share this answer
 
Think about how you would do this in a shop.
You: I would like to buy some widgets.
Shopkeeper: Certainly madam, which size?
You: the big ones.
Shopkeeper: And how many would you like?
... etc.


So you need to do something similar.
Print a message asking for some information.
Read the user's response.
Check that what they have entered is valid.
Repeat the above for each item of information.
Do the various calculations.
Print the results.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900