473,513 Members | 4,022 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Qsort reading from file sorting using array

47 New Member
The compiler is taking my code here
Expand|Select|Wrap|Line Numbers
  1. // stocks.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. //#include "stdafx.h"
  5.  
  6. #include <iostream>
  7. #include <fstream>
  8. #include <string>
  9.  
  10. using namespace std;
  11.  
  12. struct stock {
  13.     char company[256];
  14.     char symbol[4];
  15.     double price;
  16.     double change;
  17.     double old_price;
  18. };
  19.  
  20. // global data that is accessible by all of the functions
  21. const int fixed_array_size = 50; 
  22. stock stocks[fixed_array_size];
  23. int amount_actually_read;
  24.  
  25. // functions
  26. stock read_stock(ifstream &);
  27.  
  28. void print_all();
  29. void print_by_groups();
  30. void print_sorted_by_col();
  31. void print_one(stock);
  32.  
  33. int compare_by_price(const void *, const void *);
  34. int compare_by_symbol(const void *, const void *);
  35.  
  36. int main()
  37. {
  38.  
  39.  
  40.     ifstream data_file= ifstream("c:\\data.txt");
  41.     int i=0;
  42.  
  43.     while (i<fixed_array_size) {
  44.  
  45.         if (data_file.eof()) break;
  46.         stocks[i] = read_stock(data_file);
  47.         i++;
  48.  
  49.     }
  50.  
  51.     amount_actually_read = i - 1;
  52.  
  53.  
  54.  
  55.  
  56.  
  57.     print_all();
  58.  
  59.     cout << endl <<  "----------------------------------------------------------------------------" <<endl;
  60.  
  61.     qsort(stocks,amount_actually_read, sizeof(stock), compare_by_price);
  62.  
  63.     print_by_groups();
  64.  
  65.  
  66.     cout << endl <<  "----------------------------------------------------------------------------" <<endl;
  67.  
  68.     qsort(stocks,amount_actually_read, sizeof(stock), compare_by_symbol);
  69.  
  70.     print_all();
  71.  
  72.     cin >> *(new int);
  73.     return 0;
  74.  
  75.  
  76. }
  77.  
  78. stock read_stock(ifstream &input_file) {
  79.     stock result;
  80.  
  81.     input_file >> result.symbol;
  82.     input_file >> result.company;
  83.     input_file >>  result.price;
  84.     input_file >>  result.change;
  85.     //input_file >> endl;
  86.  
  87.     result.old_price =  result.price - result.change;
  88.  
  89.  
  90.  
  91.     return result;
  92. }
  93.  
  94.  
  95. void print_one(stock a) {
  96.  
  97.         cout.width(30); 
  98.         cout  << a.company;
  99.  
  100.         cout.width(7);
  101.         cout <<  a.symbol;
  102.  
  103.         cout.width(7);
  104.         cout << a.price;
  105.  
  106.         cout.width(8);
  107.         cout << a.change;
  108.  
  109.         cout.width(13);
  110.         cout << a.old_price <<  endl;
  111. }
  112. void print_all() {
  113.  
  114.     cout.setf(ios::adjustfield, ios::right);
  115.  
  116.     cout.width(30); 
  117.         cout  << "Company";
  118.  
  119.         cout.width(7);
  120.         cout <<  "Symbol";
  121.  
  122.  
  123.  
  124.         cout.width(7);
  125.         cout << "Price";
  126.  
  127.         cout.width(8);
  128.         cout << "Change";
  129.  
  130.         cout.width(13);
  131.         cout << "Old Price" <<  endl << endl << flush;
  132.  
  133.     for (int i = 0; i < amount_actually_read; i++) {
  134.  
  135.         print_one(stocks[i]);
  136.     }
  137. }
  138.  
  139. void print_by_groups() {
  140.  
  141.     double l = 0.00;
  142.     double r = 0.00;
  143.     char group_label = 'a';
  144.     int i =0,j,k=0;
  145.  
  146.     double delimeters[] = {25.0, 50.0, 100.0,1000.0};
  147.  
  148.     do {
  149.  
  150.         l = r;
  151.         r = delimeters[k];
  152.  
  153.         if (l!=0 & r!=1000) 
  154.             cout << "Stocks in range [" << l <<"," << r <<"]"<<endl;
  155.         else if (l==0)
  156.             cout << "Stocks with price under " << r << endl;
  157.         else if (r==1000)
  158.             cout << "Stockt with price above " << l << endl;
  159.  
  160.  
  161.  
  162.         while (stocks[i].price > l && stocks[i].price <= r && i<amount_actually_read) {
  163.  
  164.             print_one(stocks[i]);
  165.             i++;
  166.  
  167.         }
  168.  
  169.         cout << endl;
  170.  
  171.  
  172.  
  173.         k++;
  174.     } while(l!=1000 && i != amount_actually_read);
  175.  
  176.  
  177. }
  178.  
  179. int compare_by_price(const void* _a, const void* _b) {
  180.     const stock *a = (const stock *) _a;
  181.     const stock *b = (const stock *) _b;
  182.     if (a->price < b->price) return -1;
  183.     else if (a->price == b->price) return 0;
  184.     else return 1;
  185. }
  186.  
  187. int compare_by_symbol(const void* _a, const void* _b) {
  188.     const stock *a = (const stock *) _a;
  189.     const stock *b = (const stock *) _b;
  190.     if (string(a->symbol) < string(b->symbol)) return -1;
  191.     else if (string(a->symbol) == string(b->symbol)) return 0;
  192.     else return 1;
  193. }
  194.  
and doing this to it, marking it up like my professor would, what is GOING ON???

Expand|Select|Wrap|Line Numbers
  1.   *  function are allowed.  If there are multiple callbacks, they are
  2.      *  invoked in the order they were registered.
  3.     */
  4.     void
  5.     register_callback(event_callback __fn, int __index);
  6.  
  7.   protected:
  8.     //@{
  9.     /**
  10.      *  @if maint
  11.      *  ios_base data members (doc me)
  12.      *  @endif
  13.     */
  14.     streamsize        _M_precision;
  15.     streamsize        _M_width;
  16.     fmtflags        _M_flags;
  17.     iostate        _M_exception;
  18.     iostate        _M_streambuf_state;
  19.     //@}
  20.  
  21.     // 27.4.2.6  Members for callbacks
  22.     // 27.4.2.6  ios_base callbacks
  23.     struct _Callback_list
  24.     {
  25.       // Data Members
  26.       _Callback_list*        _M_next;
  27.       ios_base::event_callback    _M_fn;
  28.       int            _M_index;
  29.       _Atomic_word        _M_refcount;  // 0 means one reference.
  30.  
  31.       _Callback_list(ios_base::event_callback __fn, int __index,
  32.              _Callback_list* __cb)
  33.       : _M_next(__cb), _M_fn(__fn), _M_index(__index), _M_refcount(0) { }
  34.  
  35.       void
  36.       _M_add_reference() { __gnu_cxx::__atomic_add(&_M_refcount, 1); }
  37.  
  38.       // 0 => OK to delete.
  39.       int
  40.       _M_remove_reference() 
  41.       { return __gnu_cxx::__exchange_and_add(&_M_refcount, -1); }
  42.     };
  43.  
  44.      _Callback_list*    _M_callbacks;
  45.  
i think that's enough of an example as it is nearing a thousand lines of feedback for me to digest. what the???????? Anyone-
Jul 29 '07 #1
2 2872
JosAH
11,448 Recognized Expert MVP
The compiler is taking my code here

<snip>

and doing this to it, marking it up like my professor would, what is GOING ON???

<snip>

i think that's enough of an example as it is nearing a thousand lines of feedback for me to digest. what the???????? Anyone-
Which flags did you pass to your compiler? The output looks like you just
preprocessed your sources using the -E or -P flag.

kind regards,

Jos
Jul 29 '07 #2
joeschnell
47 New Member
If I did it was inadvertant, at least I know Dev didn't develop some more problems on me-I'm not fond of using VS as my only choice.
Thanks
Jul 29 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
2825
by: William Buch | last post by:
I have a strange problem. The code isn't written by me, but uses the qsort function in stdlib. ALWAYS, the fourth time through, the memory location of variable list (i.e. mem location = 41813698) becomes 11, then the program crashes. It is obviously qsort that may me overwritten the used memory location. The weird thing is that it is...
5
3848
by: Steve | last post by:
can someone tell me how qsort function in <stdlib.h> is used (qsort(..........))? the function has a buffer, two void * parameters and the a pointer to a compare function. Thanks.
13
2593
by: buda | last post by:
I had some spare time and decided to try to implement the standard library qsort. This is a pretty simpleminded attempt, but it seems to be working. I have to point out that efficiency is not at all an issue for me, and this is purely a toy, so to speak. I'm quite aware that this is not the quickest way to implement the quicksort algorithm,...
32
4497
by: John Smith | last post by:
I'm trying to figure out qsort(). I haven't seen any practical examples, only synopsis. In the code below, the array is not sorted. Can someone give me some help? #include <stdio.h> #include <stdlib.h> int compare(const void* a, const void* b); int main(void) {
16
2677
by: t_pantel | last post by:
I 've got the following structure: typedef struct GROUPED { short val ; short code; short group; short forecast_cd; short double_ind; short min;
23
6281
by: yatindran | last post by:
hai this is my 2d array. int a = { {5,2,20,1,30,10}, {23,15,7,9,11,3}, {40,50,34,24,14,4}, {9,10,11,12,13,14}, {31,4,18,8,27,17}, {44,32,13,19,41,19}, {1,2,3,4,5,6},
4
2786
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
18
2428
by: bsder | last post by:
Hi, Can anyone please tell me how to calculate the size of the following 4-dimensional array, and now to use qsort for sorting on this array? double sp = { 4.0, 5.0, 6.0 }; double spa = { { 4.0, 2.0 }, { 5.0, 8.0 }, { 6.0, 6.0 },
10
2207
by: gauss010 | last post by:
Suppose I have an object A of type char. Each A is a buffer containing a string, and I want to sort the M strings of A using the strcmp function. The description of the qsort function says that I must pass a pointer to the first object of the array to be sorted. This leads me to write the following: char A; int cmp(const void *a, const...
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7125
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5703
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5102
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4757
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3239
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.