| | |
Small problem with command line
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Solved Threads: 0
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:
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:
C Syntax (Toggle Plain Text)
#include <stdio.h> #define SIZE 100 #define FLAG "-r" void sort_asc(int *); void sort_des(int *); void prn_list(int *, char *); int i, j, temp; int main(int argc, char *argv[]) { int list[] = {90, 70, 50, 60, 30, 100, 80}; if (argv[1] == FLAG) sort_des(list); else sort_asc(list); return 0; } void sort_asc(int *list) { char state[] = {"ascending"}; for (i = 0; i < 7; i++) for (j = i + 1; j < 7; j++) if (list[i] < list[j]) { temp = list[i]; list[i] = list[j]; list[j] = temp; } prn_list(list, state); } void sort_des(int *list) { char state[] = {"descending"}; for (i = 0; i < 7; i++) for (j = i + 1; j < 7; j++) if (list[i] > list[j]) { temp = list[i]; list[i] = list[j]; list[j] = temp; } prn_list(list, state); } void prn_list(int *list, char *state) { int cnt; printf("Your list in %s order:\n", state); for (cnt = 0; cnt < 7; cnt++) printf("%d\n", list[cnt]); }
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:
to this:
Include <string.h> and change this:
C Syntax (Toggle Plain Text)
if (argv[1] == FLAG)
C Syntax (Toggle Plain Text)
if (strcmp(argv[1], FLAG) == 0)
New members chased away this month: 3
![]() |
Similar Threads
- SQL Plus command line: Arrow keys not giving previous commands back (Oracle)
- Creating a Command Line Parser (C++)
- About console command line switches.... (Windows Software)
- command line arguments problem. (Shell Scripting)
- Command-line Arguments. (C++)
- Using WGET command line util problem (Windows NT / 2000 / XP)
- Using command line parameters (C++)
Other Threads in the C Forum
- Previous Thread: i can't seem to make my search function work.... help!...
- Next Thread: can someone help me how to declare constructors as private?
| Thread Tools | Search this Thread |
Tag cloud for C
#include * ansi array arrays asterisks binarysearch calculate centimeter changingto char cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv fflush file fork forloop framework getlasterror givemetehcodez grade graphics gtkwinlinux hacking hardware histogram inches include incrementoperators input iso kernel keyboard km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mqqueue number opendocumentformat opensource overwrite owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential socket socketprograming spoonfeeding standard string strings structures systemcall testing threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






