Small problem with command line

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Small problem with command line

 
0
  #1
Mar 28th, 2006
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. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,813
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Small problem with command line

 
0
  #2
Mar 28th, 2006
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)
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: Small problem with command line

 
0
  #3
Mar 28th, 2006
:cry: Thanks you saved me :cry:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC