HELP! C program doesn't run properly for several reasons.

Vicboy

Ultra Cool Member
Reaction score
44
http://codepad.org/UZnNZCaq

The program contains prices for 5 products in 5 categories. The product's name and price are listed in variables. (integers and strings)

Once you select a category using getch(), it'll give you a list of products of that selected category. It will ask you for a price range, so you type the budget.

Entering the integer will cause the program to start listing down products of that category if the price is below the budget.

You'll notice that my program tries to achieve two other things:

- Making the processing part of the code short.
- Making products/category names easily replaceable.

The link also provides reasons that may have relations to the programs I'm getting but I don't understand it.

Two things I notice when I run the bugged up program:

- Integers for prices are treated as short int.
- Strings are not handled correctly.

I really need as much help as possible. :(
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
Seams you don't have the conio.h library so instead of getch() use getchar() and instead of clrscr() use system("cls").

JASS:
First of all arrays in C are 0-based aka
	int arr[5];
	arr[0] = 0; // valid
	arr[1] = 1; // valid
	arr[5] = 5; // array out of bounds error (although C will let you do this)
So valid indecies in arrays are from 0 to n - 1 (0 to 4 for our arr example)


You can't initialize char arrays after you've diclared them:

that's wrong:
    char type[5][5]; // declare type
    type[0] = "abcd"; // error

You should use char* type[5];
	type[0] = "Gaming Consoles";
	type[1] = "Phones";
	...
	type[4] = "Earphones";

So if we follow the above syntax rules your product array should look like:
char* product[5][5]; // declare it here and initizlied it below
product[0][0] = "Nintendo DS";
product[0][1] = "Sony PSP-3000";
...
product[0][4] = "Sony PS3"; // Note: last product of for Gaming Consoles is at [0][4]!

product[1][0] = "Samsung Galaxy S 4G";
...
product[1][4] = "RIM BlackBerry Bold 9700";

And for the rest of the categories and their products will be the same

So in order to print the last product from the Phones categorie we do:
printf("Category: %s Last product:%s\n", type[1], product[1][4]);

This will print:

Category: Phones Last product: RIM BlackBerry Bold 9700
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top