Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

any type of suggestion/correction will do, and i can't seem to get my functions to work or my conditional statements.

int buyitem(int x, int y)//* function used to buy an item at any store*//
{                        
int bought;
bought=x-y;
return bought;       //*returns the value of the difference bet wen x and y*//
}
void Tycoons(int *gil,int *onhand,int *day,int *debt)//*function used as a shop, it       does not return anything*//
{
 int i, g, on, d, de, count, N, p1, p2, p3, p4, p5, p6, p7, p8, option;
 d=*day;
 g=*gil;
 de=*debt;
 on=*onhand;
 srand(time(NULL));
 p1=rand()%1201+500;
 p2=rand()%2101+1500;
 p3=rand()%7001+5000;
 p4=rand()%5501+3500;
 p5=rand()%12001+8000;
 p6=rand()%30001+15000;
 p7=rand()%70001+40000;
 p8=rand()%65001+35000;
 system("cls");
 printf("************************\n");
 printf("Tycoon Meteor's Minerals\n");
 printf("************************\n");
 printf("\n");
 printf("Shopkeeper: Welcome! How may I be of service?\n");
 printf("\n");
 printf("Item                   On hand                 Price\n");
 printf("\n");
 printf("Phoenix Down              %d                    %dG\n", on, p1);
 printf("Elixir Essence            %d                    %dG                 Day   #%d\n",on, p2, d);
 printf("Platinum Ingot            %d                    %dG                 Gil:%d\n",   on, p3, g);
 printf("Golden Materia            %d                    %dG                 Debt:%d\n", on, p4, de);
 printf("Scarlette                 %d                    %dG\n",on, p5);
 printf("Adamintite                %d                    %dG\n",on, p6);
 printf("Dark Matter               %d                    %dG\n",on, p7);
 printf("Trapezohedron             %d                    %dG\n",on, p8);
 printf("\n");
 printf("[1]Buy  [2]Sell  [3]eave\n");
 printf("\n");
 printf("Option: ", option);
 scanf("%d", &option);
 if(option == 1)
 {
 system("cls");
 printf("************************\n");
 printf("Tycoon Meteor's Minerals\n");
 printf("************************\n");
 printf("\n");
 printf("Shopkeeper: Buying an item? Which one?\n");
 printf("\n");
 printf("Item                       On hand                Price\n");
 printf("\n");
 printf("[1]Phoenix Down              %d                    %dG\n", on, p1);
 printf("[2]Elixir Essence            %d                    %dG            Day# %d\n",on, p2, d);
 printf("[3]Platinum Ingot            %d                    %dG            Gil:%d\n", on, p3, g);
 printf("[4]Golden Materia            %d                    %dG            Debt:%d\n", on, p4, de);
 printf("[5]Scarlette                 %d                    %dG\n",on, p5);
 printf("[6]Adamintite                %d                    %dG\n",on, p6);
 printf("[7]Dark Matter               %d                    %dG\n",on, p7);
 printf("[8]Trapezohedron             %d                    %dG\n",on, p8);
 printf("\n");
 printf("[1]Buy  [2]Sell  [3]Leave\n");
 printf("\n");
 printf("Option: ", option);
 scanf("%d", &option);
 if(option == 1 || option == 2 || option == 3 || option == 4|| option == 5 || option == 6 || option == 7 || option ==8)
 {
           printf("How Many?: ", N);
           scanf("%d", &N);
           if(N>0 && g>p1)
           {
                      buyitem(g,p1);
                      Tycoons(gil,onhand,day,debt);
           }

 }
 }
 else if(option == 3)
 {
        main();
        d++;
     }
 else 
 {
      printf("wrong option");
      Tycoons(gil,onhand,day,debt);
 }

}

int main()
{
 int d, g, de, Choice, on, p1, p2, p3, p4, p5, p6, p7, p8;
 char quit;
 d = 1;
 g=20000;
 de=50000;
 char option;
 on= 0;
 srand(time(NULL));
 system("cls");
 printf("\n");
 printf("Gilgamesh: Where should my travels take me to?\n");
 printf("\n");
 printf("[1]Tycoon Meteor's Minerals                                        Day #%d\n", d);
 printf("[2]Pulsian Restoratives                                            Gil: %d\n", g);
 printf("[3]Archadian Luxuries                                              Debt: %d\n", de);
 printf("[4]Cid's Magical Escapades\n");
 printf("[5]Gaian Gratitudes\n");
 printf("[6]Riches and Minerals of Spira                                    [Q]uit\n", quit);
 printf("[7]Go see the Merchant of The Rift\n");
 printf("\n");
 printf("Your Choice: ", Choice);
 scanf("%d", &Choice);
 switch(Choice)
 {
               case 1:
                    Tycoons(&g,&on,&d,&de);
                    break;
               case 'q':
                            printf("Thank you for playing!");
                            exit(0);
                            break;
 }
 getch();
 }
share|improve this question
Why are you passing ints by pointer? – Alex Chamberlain Aug 12 '12 at 11:49
3  
I realize that you were sent here from StackOverflow; however our faq do not permit questions about code that isn't working. Please get your code to work, then come back if you want to find out how to make it better. – codesparkle Aug 12 '12 at 11:49
Also, it could be interesting to have a little background of what this code do and other useful information such as the context (scholar or business) and if this is only a part of a bigger apps. – Jean-François Côté Aug 12 '12 at 13:31
See also CR14652. – Jonathan Leffler Aug 14 '12 at 14:38
Welcome to Code Review. Please note that the preferred way of saying 'thanks' around here is by up-voting good questions and helpful answers (once you have enough reputation to do so), and by accepting the most helpful answer to any question you ask (which also gives you a small boost to your reputation). Please see the FAQ and especially How do I ask questions here? – Jonathan Leffler Aug 14 '12 at 14:42

closed as off topic by svick, Trevor Pilley, Winston Ewert Nov 9 '12 at 16:38

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

The most immediate observation is:

  • When you see code like:

    int ... p1, p2, p3, p4, p5, p6, p7, p8, ...;
    

    you should immediately think 'array'.

  • When you see code like:

    p1=rand()%1201+500;
    p2=rand()%2101+1500;
    p3=rand()%7001+5000;
    p4=rand()%5501+3500;
    p5=rand()%12001+8000;
    p6=rand()%30001+15000;
    p7=rand()%70001+40000;
    p8=rand()%65001+35000;
    

    you should immediately be thinking 'array of structures'.

  • You should also worry about those modulo operations. The range of values returned by rand() is denoted RAND_MAX and might be as small as 32767, in which case your modulo operations for p7 and p8 are not doing anything. Actually, you should revisit the random number generation altogether. You should probably be using something like (rand() / (double) RAND_MAX) * max_value to get a value between 0 and the maximum value (such as 7001).

  • When you see code like:

     printf("Item                   On hand                 Price\n");
     printf("\n");
     printf("Phoenix Down              %d                    %dG\n", on, p1);
     printf("Elixir Essence            %d                    %dG                 Day   #%d\n",on, p2, d);
     printf("Platinum Ingot            %d                    %dG                 Gil:%d\n",   on, p3, g);
     printf("Golden Materia            %d                    %dG                 Debt:%d\n", on, p4, de);
     printf("Scarlette                 %d                    %dG\n",on, p5);
     printf("Adamintite                %d                    %dG\n",on, p6);
     printf("Dark Matter               %d                    %dG\n",on, p7);
     printf("Trapezohedron             %d                    %dG\n",on, p8);
     printf("\n");
     printf("[1]Buy  [2]Sell  [3]eave\n");
     printf("\n");
     printf("Option: ", option);
     scanf("%d", &option);
     if(option == 1)
     {
     system("cls");
     printf("************************\n");
     printf("Tycoon Meteor's Minerals\n");
     printf("************************\n");
     printf("\n");
     printf("Shopkeeper: Buying an item? Which one?\n");
     printf("\n");
     printf("Item                       On hand                Price\n");
     printf("\n");
     printf("[1]Phoenix Down              %d                    %dG\n", on, p1);
     printf("[2]Elixir Essence            %d                    %dG            Day# %d\n",on, p2, d);
     printf("[3]Platinum Ingot            %d                    %dG            Gil:%d\n", on, p3, g);
     printf("[4]Golden Materia            %d                    %dG            Debt:%d\n", on, p4, de);
     printf("[5]Scarlette                 %d                    %dG\n",on, p5);
     printf("[6]Adamintite                %d                    %dG\n",on, p6);
     printf("[7]Dark Matter               %d                    %dG\n",on, p7);
     printf("[8]Trapezohedron             %d                    %dG\n",on, p8);
     printf("\n");
    

    with its vast block of repeated material, you should be thinking 'subroutine'. Actually, probably several subroutines. The repetition is a maintenance nightmare. Yes, handling the special cases for items 2, 3, and 4 complicates life a little. Yes, deciding whether to output the [1] before the item name complicates it a little. You should probably be using %6d or something similar for the 'quantity on hand', and quite likely for the price too.

  • It is a little surprising that you have the same number on of each item available. Are you sure that shouldn't be calculated (generated at random) too?

  • Of course, the functions will need arguments to define what to print, etc. But you really need to use some basic structures.


In the code:

printf("Your Choice: ", Choice);
scanf("%d", &Choice);
switch (Choice)
{
case 1:
    Tycoons(&g,&on,&d,&de);
    break;
case 'q':
    printf("Thank you for playing!");
    exit(0);
    break;
}
  • Your printf() format doesn't need the argument Choice.
  • You scan for an integer.
  • You do not check that scanf() was successful.
  • Your switch requires your users to know that the code for 'q' is 113.
  • Your switch needs a default clause to catch the 'any other response'.

We could get into a philosophical debate about the merits or otherwise of scanf(). Suffice to say that it is hard to use correctly and harder still to recover from errors when using scanf(). If you're going to be reading single characters without the user having to hit return after each choice, then you should probably not be using scanf(). If you're going to want answers with a newline at the end of each, you'd probably do better reading the input with fgets() and then using sscanf() to parse the data that was read. Of course, ultimately, you'll need a function that handles the input—that handles a prompt, recognizes when the data is bad, reprompts, handles EOF (exit game?), etc.

Choice = prompt("Your choice: ", "12345678q");

The function is given the prompt string and a list of valid options (here, 8 digits for 8 emporia plus 'q' for quit). The value in Choice is probably the character code, so the cases in the switch become (note the single quotes):

case '1':
    Tycoons(&g,&on,&d,&de);
    break;

but your input code is reading a character, not a number (so "%d" is not the correct format) and returns the number corresponding to the character entered.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.