Menu

a quick question!

beho G.
2008-12-03
2012-09-26
1 2 > >> (Page 1 of 2)
  • beho G.

    beho G. - 2008-12-03

    I have a quick question for you, I checked the Please read...,
    I am doing a program that uses a data from a file called beer.dat, where should I save the beer.dat in the dev-C++ directory?!

    Thanks

     
    • beho G.

      beho G. - 2008-12-08

      Clifford, I like your way of explaining, It finally works, Thanks so much for your time.
      and Thank you too Wayne.

       
    • cpns

      cpns - 2008-12-03

      You shouldn't save anything "in the dev-C++ directory", for the same reason you would not save you document in Office's installation folder!

      You can put your file wherever you like, so long as when you open it you specify its full path. By default the current-working-folder for an application is wherever you launched it from. If you launched the program from the IDE, that will be the project folder. So I guess the simple answer is to put it into the project folder, however that may or may not be appropriate in all circumstances.

      Clifford

       
    • beho G.

      beho G. - 2008-12-06

      Thanks Clifford,
      I created a new folder and put the cpp file and the doc file, but it didn't work!!

      That's what I have,
      FILE *infile = fopen("beer.dat", "r"); //open file

      so I put the beer.dat in the same folder, but it didn't work?

       
      • Wayne Keen

        Wayne Keen - 2008-12-06

        How didn't it work? Didn't compile? Didn't run? Gave an error message? (What did it say). Or did your computer simply try to start global thermo-nuclear was as a result?

        How did you run your executable? Where was it?

        Wayne

         
    • beho G.

      beho G. - 2008-12-06

      No Wayne, It didn't start global thermo-nuclear.

      it compiles but, when it tries to open, it just display the error message , that I assigned if the file didn't open.
      anyway here's my program, you make a file called beer.dat , w some numbers (just to check it)

      include<stdio.h>

      include<stdlib.h>

      //print menu
      char menu( );
      //search inventory by beer ID.
      //Return index of beer if found. -1 if beer not found
      int search(int beerID[20], int searchID, int size);
      //display the record that the user searched for
      void displayBeer(int beerID[20], int beerQTY[20], float beerPrice[20], int recordNumber);
      //sort inventory by price
      void sort_array(int beerID[20], int beerQTY[20], float beerPrice[20], int size);
      //order beer
      void orderBeer(int beerID[20], int beerQTY[20], float beerPrice[20], int recordNumber, int quantity);

      int main(void)
      {
      FILE *infile = fopen("beer.dat", "r"); //open file
      int id[20]; //array to hold id
      int qty[20]; //array to hold quantity on hand
      float price[20]; //array to hold price
      int numberOfRecords; //the first line of the file is number of records in file
      int index = 0; //used to step through arrays
      char choice = ' '; //hold the users input choice
      int beerID; //hold ID when user searches for beer
      int recordNumber; //hold the record number of beer user searched for
      int orderQty; //order quantity
      int count;
      char trash;
      if(infile == NULL) //make sure file is open, if not print error and exit
      {
      printf("Could not open file");
      exit(1);
      }
      //get the number of records in file
      fscanf(infile, "%i",&numberOfRecords);
      //get data while not at end of file
      while(!feof(infile))
      {
      fscanf(infile, "%i", &id[index]);
      fscanf(infile, "%i", &qty[index]);
      fscanf ( infile, "%f", &price[index]);
      index++;
      }
      sort_array(id, qty, price, numberOfRecords);
      choice = menu( );
      //run
      while(choice != 'q')
      {
      switch(choice)
      {
      case 's':
      printf("Enter the 4 digit ID number:\n");
      scanf("%d", &beerID);
      scanf("%c", &trash);
      recordNumber = search(id, beerID, numberOfRecords);
      if(recordNumber == -1)//beer not found
      {
      printf("Sorry. We are out of %d\n", beerID);
      }
      else //beer on hand
      {
      displayBeer(id, qty, price, recordNumber);
      }
      break;
      case 'v':
      printf("ID\tQTY\tPRICE\n");
      for(count =0; count < numberOfRecords; count++)
      {
      printf("%d\t", id[count]);
      printf("%d\t", qty[count]);
      printf("%.2f\n", price[count]);
      }//end for
      break;
      case 'o':
      printf("To oder enter the 4 digit ID number:\n");
      scanf("%d", &beerID);
      printf("Enter the quantity you wish to order:\n");
      scanf("%d", &orderQty);
      scanf("%c", &trash);
      recordNumber = search(id, beerID, numberOfRecords);
      if(recordNumber == -1)//beer not found
      {
      printf("Sorry. We are out of %d\n", beerID);
      }
      else //beer on hand
      {
      orderBeer(id, qty, price, recordNumber, orderQty);
      }
      break;
      case 'q':
      printf("Good bye\n");
      default:
      printf("Bad choice.\n");
      break;
      }//end switch
      choice = menu( );

      }//end while

      return 0;
      }
      //***********
      char menu( )
      {

      char trash;//remove newline
      char input;
      printf("\n\n\nWelcome to the beer inventory system.\n");
      printf("Enter (S)earch for a beer.\n");
      printf("Enter (V)iew inventory.\n");
      printf("Enter (O)rder beer.\n");
      printf("Enter (Q)uit.\n");
      //input = getchar();
      scanf("%c", &input);
      scanf("%c", &trash);
      return input;

      }//end menu
      //********
      int search(int beerID[20], int searchID, int size)
      {
      int count;
      int index = -1; //return -1 if beer not found
      for(count = 0; count < size; count++)
      {
      if(beerID[count] == searchID)
      {
      index = count;
      }
      }//end for
      return index;
      }//end search
      //
      *******
      void displayBeer(int beerID[20], int beerQTY[20], float beerPrice[20], int recordNumber)
      {
      printf("ID\tQTY\tPRICE\n");
      printf("%d\t", beerID[recordNumber]);
      printf("%d\t", beerQTY[recordNumber]);
      printf("%.2f\n", beerPrice[recordNumber]);
      }//end display
      //**********
      void sort_array(int beerID[20], int beerQTY[20], float beerPrice[20], int size)
      {
      float temp = 0.0;
      int intTemp;
      int count = 0;
      int index = 0;
      for(count = 0; count < (size - 1); count++)
      {
      for(index = 0; index < (size - 1); index++)
      {
      if(beerPrice[index] > beerPrice[index +1])
      {
      //swap price
      temp = beerPrice[index +1];
      beerPrice[index +1] = beerPrice[index];
      beerPrice[index] = temp;
      //swap id
      intTemp = beerID[index +1];
      beerID[index +1] = beerID[index];
      beerID[index] = intTemp;
      //swap qty
      intTemp = beerQTY[index +1];
      beerQTY[index +1] = beerQTY[index];
      beerQTY[index] = intTemp;
      }
      }
      }
      }//end sort
      //
      ********
      *
      void orderBeer(int beerID[20], int beerQTY[20], float beerPrice[20], int recordNumber, int quantity)
      {
      //make sure we have qty on hand
      if(beerQTY[recordNumber] < quantity)//not enough on hand
      {
      printf("Sorry we don't have %d of %d on hand.\n", quantity, beerID[recordNumber]);
      }
      else//place order
      {
      beerQTY[recordNumber] -= quantity;
      printf("Your order is:\n");
      printf("ID\tQTY\tPRICE\n");
      printf("%d\t", beerID[recordNumber]);
      printf("%d\t", quantity);
      printf("%.2f\n", beerPrice[recordNumber]);
      }
      }//end order

       
    • cpns

      cpns - 2008-12-06

      It has nothing to do with where your .cpp file is. I did not say that. When you run your program from the IDE, the cwd will be the project folder, which may or may not be the same folder as the .cpp.

      If you dod not use a project, and simply directly compiled the .cpp file, I don't know what the cwd will be. I would never not use teh project facility.

      The simple test is to not use the IDE at all to run your code, and simply open a console, cd to the folder where the .exe was created, and run it from there (with the file present).

      I wonder why you would not simply pass the file as a command line argument, then you could specify exactly where it is.

      If you were to post your Compile Log, we would have sufficient information about your project to determine more precisely how you should proceed.

      Clifford

       
    • beho G.

      beho G. - 2008-12-07

      Look, I tried to compile it as usual, and saved the DOS file in the same folder as the beer.dat didn't work,

      I opened new project---> console---> then saved it in a new folder, pasted my code, in the project, and saved everything, including the exe file in the same folder, didn't work!!

      Clifford, can you give me some directions?

      or try it, may be something wrong in opening the file ( first time to try this fopen thing )

      just create a new beer.dat and paste the following inside.

      10
      1235 42 88.56
      1918 3 111.11
      7002 22 203.99
      8619 55 167.22
      5693 30 123.45
      2415 11 99.98
      5419 23 101.12
      6795 6 188.67
      5190 20 145.62
      2504 14 124.49

      , I hope it works,
      Thanks Clifford for your time

       
    • Wayne Keen

      Wayne Keen - 2008-12-07

      I compiled and ran your code here. No trouble finding the file, and it seemed to load it OK.

      I compiled it directly under MinGW/MSYS (I don't have Dev on this machine, though I should).

      Wayne

       
    • Wayne Keen

      Wayne Keen - 2008-12-07

      Note that some of your prompts imply that they are looking for capital letters when they really are not.

       
    • cpns

      cpns - 2008-12-07

      > Clifford, can you give me some directions?

      Unfortunately I no longer waste my time installing and using Dev-C++, so I cannot really give step-by-step instructions.

      One thing you might try is adding the following immediatly after the printf("Could not open file");

      system( "cd" ) ;
      perror( "Error: " ) ;

      this will display the cwd and show you where the program is looking, and siaplay a string representation of the errno value, which will tell us why teh fopen failed.

      Also you might do as I suggested and specify the the full path for the file (even if just as an experiment to discount other problems).

      Finally I asked for the Compile Log as an aid to diagnosing your problem. You spent the first 53 words of your last post describing exactly what simply copy & pasting the log would have told us - and the log is more accurate.

      Clifford

       
    • beho G.

      beho G. - 2008-12-07

      Here's the compiler log Clifford, Sorry, I hid it by mistake.
      I tried the full path, but I got some errors in the directory.
      Thanks Wayne, for letting me know that the program works. I actually had a library that check the cases, capital or lower case, but I took it off, I was thinking about a do-while loop, to make sure that the user will enter a lower case letter.

      Compiler: Default compiler
      Executing g++.exe...
      g++.exe "C:\Users\Beho86\Desktop\beer1.cpp" -o "C:\Users\Beho86\Desktop\beer1.exe" -g3 -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3
      Execution terminated
      Compilation successful

      I have another question, I didn't want to open a new post, in the order function, I made it like one type of beer per purchase, how can I ask the user, before displaying the receipt, if he is done or return back to the menu to buy another type , like any amount of beers of any kind.
      the last line was wrong, it should be ( printf("%.2f\n", beerPrice[recordNumber]* quantity); )

      Thanks Clifford and Wayne for your time. ( would you recommend using another compiler?!!)

       
    • Wayne Keen

      Wayne Keen - 2008-12-07

      g++.exe "C:\Users\Beho86\Desktop\beer1.cpp"

      This tells me your code is resident on your desktop. This is a very bad idea.

      Wayne

       
    • beho G.

      beho G. - 2008-12-07

      I put them in a folder called test, here's the compiler log,

      Compiler: Default compiler
      Executing g++.exe...
      g++.exe "C:\Users\Beho86\Desktop\test\beer.cpp" -o "C:\Users\Beho86\Desktop\test\beer.exe" -g3 -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3
      Execution terminated
      Compilation successful

       
    • cpns

      cpns - 2008-12-07

      > I tried the full path, but I got some errors in the directory.

      Ok, so fix them! This is an important part of the investigation, why would you give up? And why would you not explain what the errors were so we could help? You should have:

      FILE *infile = fopen("C:\Users\Beho86\Desktop\beer1.dat", "r"); //open file

      Note you need \ because \ on its own indicates a character escape sequence. '\' is the escape sequence for '\'. Alternatively GCC will allow Unix stule delimiters:

      FILE *infile = fopen("C:/Users/Beho86/Desktop/test/beer1.dat", "r"); //open file

      I note that you are compiling your code as C++ rather than C, although it appears to be valid C code. That is not necessarily a bad thing, but it might be if you did not intend or realise that you were doing that.

      I note also that you are not using thw Project tool, but rather building the code directly. Also not necessarily a problem, but I would not recommend it if you want to ensure that your code builds correctly and as intended every time.

      When I made suggestions such as about displaying the cwd and errno strings, I kind of expected feedback on that too.

      Clifford

       
      • cpns

        cpns - 2008-12-07

        Sorry, that should have been "beer.dat" in both cases.

         
    • beho G.

      beho G. - 2008-12-07

      I didn't gave up, that's why I am still here.
      look, new folder, c source, not cpp. here's the compiler log

      Compiler: Default compiler
      Executing gcc.exe...
      gcc.exe "C:\Users\Beho86\Desktop\test\beer.c" -o "C:\Users\Beho86\Desktop\test\beer.exe" -g3 -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3
      Execution terminated
      Compilation successful

      Then, I tried
      FILE *infile = fopen("C:\Users\Beho86\Desktop\test2\beer.dat", "r");
      and here's my compiler log:

      Compiler: Default compiler
      Building Makefile: "C:\Users\Beho86\Desktop\test2\Makefile.win"
      Executing make...
      make.exe -f "C:\Users\Beho86\Desktop\test2\Makefile.win" all
      make.exe: Nothing to be done for `all'.

      Execution terminated
      Compilation successful

      So, What do you think???

      Then, I tried new-->project--->console application

      and here's my compiler log:
      Compiler: Default compiler
      Building Makefile: "C:\Users\Beho86\Desktop\test2\Makefile.win"
      Executing make...
      make.exe -f "C:\Users\Beho86\Desktop\test2\Makefile.win" all
      gcc.exe -c main.c -o main.o -I"C:/Dev-Cpp/include"

      gcc.exe main.o -o "beer_distribution.exe" -L"C:/Dev-Cpp/lib"

      Execution terminated
      Compilation successful

      I tried to make the path [

      and here's my compiler log:
      Compiler: Default compiler
      Building Makefile: "C:\Users\Beho86\Desktop\test2\Makefile.win"
      Executing make...
      make.exe -f "C:\Users\Beho86\Desktop\test2\Makefile.win" all
      gcc.exe -c main.c -o main.o -I"C:/Dev-Cpp/include"

      gcc.exe main.o -o "beer_distribution.exe" -L"C:/Dev-Cpp/lib"

      Execution terminated
      Compilation successful

       
    • Wayne Keen

      Wayne Keen - 2008-12-08

      Get your code OFF the desktop.

      Create a directory called something like

      c:\mycstuff

      I don't know why you thought putting it in a directory on your desktop was the right thing to do. That is just plan dumb. Its still on your destop - it can still give you path issues!

      Wayne

       
    • beho G.

      beho G. - 2008-12-08

      Okay wayne, I did what you said,
      I created a folder called test, saved the beer.dat and the beer.c, in the same folder
      using FILE *infile = fopen("beer.dat", "r");
      Compiler: Default compiler
      Executing gcc.exe...
      gcc.exe "C:\test\beer.c" -o "C:\test\beer.exe" -g3 -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3
      Execution terminated
      Compilation successful

      Used FILE *infile = fopen("C://test//beer.dat", "r");

      Compiler: Default compiler
      Executing gcc.exe...
      gcc.exe "C:\test\beer.c" -o "C:\test\beer.exe" -g3 -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3
      Execution terminated
      Compilation successful

      So now it's not a dumb idea, and it didn't work too.
      what compiler are you using?

       
    • beho G.

      beho G. - 2008-12-08

      Hey Wayne,
      I just noticed something, I unhidden the extension, I saw that file that the teacher sent to us, called beer.dat.rtf
      I edited it in the program, I get a black dos screen but nothing in it.

       
    • Wayne Keen

      Wayne Keen - 2008-12-08

      "So now it's not a dumb idea"

      It is now, and it will always be a dumb idea. It is asking for path trouble, even when you get away with it.

      I am using GCC.

      "I created a folder called test, saved the beer.dat and the beer.c, in the same folder"

      Ddi you verify that your executable is written to that directory?

      Wayne

       
    • beho G.

      beho G. - 2008-12-08

      Yes I did Wayne.

       
    • Wayne Keen

      Wayne Keen - 2008-12-08

      "I edited it in the program, I get a black dos screen but nothing in it."

      Are you saying that you changed the filename, and now your program runs, but you are not getting your prompts?

      Wayne

       
    • beho G.

      beho G. - 2008-12-08

      YES,

       
      • Wayne Keen

        Wayne Keen - 2008-12-08

        OK, this can happen sometimes.

        When I compiled your code earlier, to make sure that the prompts displayed, after each printf I added:

        fflush(stdout);

        to make sure that my prints didn't get cached.

        This is more of a problem when you are running with MSYS.

        Wayne

         
1 2 > >> (Page 1 of 2)

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.