944,028 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 992
  • C RSS
Mar 28th, 2006
0

Small problem with command line

Expand Post »
The program sorts numbers in ascending order. If the user enters a "-r" in the command line, the program sorts the numbers in descending order.

The problem I'm having is that I can't get the **** program to read the if/else condition correctly. Maybe something wrong with my syntax? :evil:

  1. #include <stdio.h>
  2.  
  3. #define SIZE 100
  4. #define FLAG "-r"
  5.  
  6. void sort_asc(int *);
  7. void sort_des(int *);
  8. void prn_list(int *, char *);
  9.  
  10. int i, j, temp;
  11. int main(int argc, char *argv[])
  12. {
  13. int list[] = {90, 70, 50, 60, 30, 100, 80};
  14.  
  15. if (argv[1] == FLAG)
  16. sort_des(list);
  17. else
  18. sort_asc(list);
  19.  
  20. return 0;
  21. }
  22.  
  23. void sort_asc(int *list)
  24. {
  25. char state[] = {"ascending"};
  26.  
  27. for (i = 0; i < 7; i++)
  28. for (j = i + 1; j < 7; j++)
  29. if (list[i] < list[j])
  30. {
  31. temp = list[i];
  32. list[i] = list[j];
  33. list[j] = temp;
  34. }
  35.  
  36. prn_list(list, state);
  37.  
  38. }
  39.  
  40. void sort_des(int *list)
  41. {
  42. char state[] = {"descending"};
  43.  
  44. for (i = 0; i < 7; i++)
  45. for (j = i + 1; j < 7; j++)
  46. if (list[i] > list[j])
  47. {
  48. temp = list[i];
  49. list[i] = list[j];
  50. list[j] = temp;
  51. }
  52.  
  53. prn_list(list, state);
  54. }
  55.  
  56. void prn_list(int *list, char *state)
  57. {
  58. int cnt;
  59.  
  60. printf("Your list in %s order:\n", state);
  61.  
  62. for (cnt = 0; cnt < 7; cnt++)
  63. printf("%d\n", list[cnt]);
  64. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
Mar 28th, 2006
0

Re: Small problem with command line

You compare the contents of two strings with the strcmp function in the <string.h> header. What you're actually doing with the == operator is comparing two memory addresses. Since the two strings are extremely unlikely to be located at the same address, your comparison will fail and the sort will always be in ascending order.

Include <string.h> and change this:
  1. if (argv[1] == FLAG)
to this:
  1. if (strcmp(argv[1], FLAG) == 0)
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 28th, 2006
0

Re: Small problem with command line

:cry: Thanks you saved me :cry:
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC