How can this if else statement be simplified?

This is a discussion on How can this if else statement be simplified? within the C Programming forums, part of the General Programming Boards category; I've got 25 options the user can take from 5 different choices. I've tried using the "switch" function, but the ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    27

    How can this if else statement be simplified?

    I've got 25 options the user can take from 5 different choices.

    I've tried using the "switch" function, but the "case" test one conditional at a time. I've tried looping, but I couldn't get my head round it.

    Well, here is the "replace all" thing that I done in MS word to get it like this, phew!

    I would really appreciate it if anyone can show me hints or suggestions to how this can be simplified:

    Code:
    	if ((chosenfile1==1)&&(chosenfile2==1)) statisticsof2textfilescompared ("The Sun","The Sun",".\\textfiles\\thesun.txt",".\\textfiles\\thesun.txt");
    	else
    	if ((chosenfile1==1)&&(chosenfile2==2)) statisticsof2textfilescompared ("The Sun","The Times",".\\textfiles\\thesun.txt",".\\textfiles\\thetimes.txt");
    	else
    	if ((chosenfile1==1)&&(chosenfile2==3)) statisticsof2textfilescompared ("The Sun","French Article",".\\textfiles\\thesun.txt",".\\textfiles\\frenchnewspaper.txt");
    	else
    	if ((chosenfile1==1)&&(chosenfile2==4)) statisticsof2textfilescompared ("The Sun","Romeo and Julliet",".\\textfiles\\thesun.txt",".\\textfiles\\shakespeare.txt");
    	else
    	if ((chosenfile1==1)&&(chosenfile2==5)) statisticsof2textfilescompared ("The Sun","Harry Potter",".\\textfiles\\thesun.txt",".\\textfiles\\childrensbook.txt"); 
    	else
    	if ((chosenfile1==2)&&(chosenfile2==1)) statisticsof2textfilescompared ("The Times","The Sun",".\\textfiles\\thetimes.txt",".\\textfiles\\thesun.txt"); 
    	else
    	if ((chosenfile1==2)&&(chosenfile2==2)) statisticsof2textfilescompared ("The Times","The Times",".\\textfiles\\thetimes.txt",".\\textfiles\\thetimes.txt"); 
    	else 
    	if ((chosenfile1==2)&&(chosenfile2==3)) statisticsof2textfilescompared ("The Times","French Article",".\\textfiles\\thetimes.txt",".\\textfiles\\frenchnewspaper.txt"); 
    	else 
    	if ((chosenfile1==2)&&(chosenfile2==4)) statisticsof2textfilescompared ("The Times","Romeo and Julliet",".\\textfiles\\thetimes.txt",".\\textfiles\\shakespeare.txt"); 
    	else 
    	if ((chosenfile1==2)&&(chosenfile2==5)) statisticsof2textfilescompared ("The Times","Harry Potter",".\\textfiles\\thetimes.txt",".\\textfiles\\childrensbook.txt"); 
    	else 
    	if ((chosenfile1==3)&&(chosenfile2==1)) statisticsof2textfilescompared ("French Article","The Sun",".\\textfiles\\frenchnewspaper.txt",".\\textfiles\\thesun.txt"); 
    	else 
    	if ((chosenfile1==3)&&(chosenfile2==2)) statisticsof2textfilescompared ("French Article","The Times",".\\textfiles\\frenchnewspaper.txt",".\\textfiles\\thetimes.txt"); 
    	else 
    	if ((chosenfile1==3)&&(chosenfile2==3)) statisticsof2textfilescompared ("French Article","French Article",".\\textfiles\\frenchnewspaper.txt",".\\textfiles\\frenchnewspaper.txt"); 
    	else 
    	if ((chosenfile1==3)&&(chosenfile2==4)) statisticsof2textfilescompared ("French Article","Romeo and Julliet",".\\textfiles\\frenchnewspaper.txt",".\\textfiles\\shakespeare.txt"); 
    	else 
    	if ((chosenfile1==3)&&(chosenfile2==5)) statisticsof2textfilescompared ("French Article","Harry Potter",".\\textfiles\\frenchnewspaper.txt",".\\textfiles\\childrensbook.txt"); 
    	else 
    	if ((chosenfile1==4)&&(chosenfile2==1)) statisticsof2textfilescompared ("Romeo and Julliet","The Sun",".\\textfiles\\shakespeare.txt",".\\textfiles\\thesun.txt"); 
    	else 
    	if ((chosenfile1==4)&&(chosenfile2==2)) statisticsof2textfilescompared ("Romeo and Julliet","The Times",".\\textfiles\\shakespeare.txt",".\\textfiles\\thetimes.txt"); 
    	else 
    	if ((chosenfile1==4)&&(chosenfile2==3)) statisticsof2textfilescompared ("Romeo and Julliet","French Article",".\\textfiles\\shakespeare.txt",".\\textfiles\\frenchnewspaper.txt"); 
    	else 
    	if ((chosenfile1==4)&&(chosenfile2==4)) statisticsof2textfilescompared ("Romeo and Julliet","Romeo and Julliet",".\\textfiles\\shakespeare.txt",".\\textfiles\\shakespeare.txt"); 
    	else 
    	if ((chosenfile1==4)&&(chosenfile2==5)) statisticsof2textfilescompared ("Romeo and Julliet","Harry Potter",".\\textfiles\\shakespeare.txt",".\\textfiles\\childrensbook.txt"); 
    	else 
    	if ((chosenfile1==5)&&(chosenfile2==1)) statisticsof2textfilescompared ("Harry Potter","The Sun",".\\textfiles\\childrensbook.txt",".\\textfiles\\thesun.txt"); 
    	else 
    	if ((chosenfile1==5)&&(chosenfile2==2)) statisticsof2textfilescompared ("Harry Potter","The Times",".\\textfiles\\childrensbook.txt",".\\textfiles\\thetimes.txt"); 
    	else 
    	if ((chosenfile1==5)&&(chosenfile2==3)) statisticsof2textfilescompared ("Harry Potter","French Article",".\\textfiles\\childrensbook.txt",".\\textfiles\\frenchnewspaper.txt"); 
    	else 
    	if ((chosenfile1==5)&&(chosenfile2==4)) statisticsof2textfilescompared ("Harry Potter","Romeo and Julliet",".\\textfiles\\childrensbook.txt",".\\textfiles\\shakespeare.txt"); 
    	else 
    	if ((chosenfile1==5)&&(chosenfile2==5)) statisticsof2textfilescompared ("Harry Potter","Harry Potter",".\\textfiles\\childrensbook.txt",".\\textfiles\\childrensbook.txt"); 
    	else 
    	printf("Invalid menu option, please try again");
    Aiiiiiiiiiiiiiii.

    There must be some way of making this simpler, there must bee....

    Thanks in advance,
    CookieMonster

  2. #2
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    char *cstring[4] = { "Harry Potter", ... };

    statisticsof2textfilescompared(cstring[chosenfile1], cstring[chosenfile2]);
    .sect signature

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    Use arrays of names.
    e.g. {"The Sun", "The Times"}

    Now you can access according to the chosen file
    e.g. func(names[chosenfile1-1], names[chosenfile2-1]);

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    27
    Whoa! Just like that!

    Thank you!

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    27
    This must be a very trivial question (sorry!) but what is the difference between doing

    Code:
    char *selectedname1[5]={"The Sun","Th...."}
    and this

    Code:
    char selectedname1[5]={"The Sun","Th...."}
    I understand that in the first code the array is being dereferenced, but what difference does this make compared to the second one? Am I not asking in the second code for the strings to be put into the array?

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The first one creates an array of pointers that point to an array of characters.
    The second one tries to store multiple arrays of characters into a single dimensional array.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    27
    I'll take that as a given rule. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 06:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 03:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21