1

Hey I am trying to create an invoice program that is supposed to accept the item name, item price and quantity and store them in a list or array. When I run the program however I get errors. Can someone please help?? I'm noob to c and cannot see how I went wrong, the concept is very simple..

Here is the program:

#include <string.h>
#include <conio.h>

int main()
  {
   char item_name[255];
   float item_price=0;
   float quantity;
   int choice;
   int k;

   for (k=0;choice != 2;k++)
    {
       printf ("Enter item name: ");
       scanf ("%s", item_name[k]);
       printf ("\n");
       printf ("Enter item price: ");
       scanf ("%f", &item_price[k]);
       printf ("\n");
       printf ("Enter item quantity: ");
       scanf ("%f", &quantity[k]);
       printf ("\n\n");
       printf ("Enter another item? Enter '1' for yes and '2' for no: ");
       scanf ("%d", &choice);
    }

  }

these are the errors:

sample.c: In function ‘main’: sample.c:15:8: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=] scanf ("%s", item_name[k]); ^ sample.c:18:32: error: subscripted value is neither array nor pointer nor vector scanf ("%f", &item_price[k]); ^ sample.c:21:30: error: subscripted value is neither array nor pointer nor vector scanf ("%f", &quantity[k]); ^ sample.c:25:5: error: expected ‘;’ before ‘}’ token } ^ sample.c:8:10: warning: variable ‘quantity’ set but not used [-Wunused-but-set-variable] float quantity; ^ sample.c:7:10: warning: variable ‘item_price’ set but not used [-Wunused-but-set-variable] float item_price=0; ^

6
  • 3
    Please add the complete errors that you get to the question post (not as comments). Commented Feb 10, 2015 at 21:11
  • ...well -- it's pretty obvious that we're only allocating space for one item name, price and quantity, then trying to store several of them (in memory that doesn't exist). Commented Feb 10, 2015 at 21:13
  • quantity and item_price aren't arrays so you can't use [] on them. Commented Feb 10, 2015 at 21:13
  • ...so, for the OP: Can you control how many items there are? If so, you can allocate arrays large enough for all of them; if not, you'll need to dynamically allocate memory, either by allocating new linked-list entries (which any first-year computer science class in C will show you how to do), or by using realloc() or kin to resize an array. Commented Feb 10, 2015 at 21:15
  • could you comment the correction of the program above? Commented Feb 10, 2015 at 21:25

1 Answer 1

1
char item_name[255];

is a single string - not an array of strings.

item_name[k]

is a char - not a char*

So you need to make item_name a 2D array of strings

char item_name[100][255];

and then use

scanf ("%s", item_name[k]);

The next problem is item_price. It also needs to be an array:

float item_price[100];

And same story with float quantity

float quantity[100];

And then your are missing a ";" at the end of the last scanf

Finally you need to add

if (choice == 2) break;

to get out of the loop.

The proposed code allows for 100 items so you should all so add:

if (k > 99) break;

as the first line in the for-loop

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.