Thread: Transpose information

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    16

    Transpose information

    this is another question from my review... i need change an if.. else to a switch case and a while loop to a for keeping the program logic.

    this is the code givin

    Code:
    do{
      choice = menu();
        if(choice == 1){
    	addtogether(one, two);
         }else 
            if(choice == 2){
    	  multiply(one, two);
    	 }else 
              if(choice == 3){
    	    listall(one, two);
    	   }
    }while(choice != 4);
    this is what i have ...

    Code:
    int choice(one,two,choice){switch(choice){
     case 1:addtogether(one,two);break;
     case 2:multiply(one,two);break;
     case 3:listall(one,two);break;}
    would i have to incorporate a for loop? and if so how?

    I'm just not really confident with my programming at this point so I'm a bit worried

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Your switch statement is good but why did you make up a choice function in the second version?
    Don't quote me on that... ...seriously

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by fr0zen View Post
    this is another question from my review... i need change an if.. else to a switch case and a while loop to a for keeping the program logic.

    this is the code givin

    Code:
    do{
      choice = menu();
        if(choice == 1){
    	addtogether(one, two);
         }else 
            if(choice == 2){
    	  multiply(one, two);
    	 }else 
              if(choice == 3){
    	    listall(one, two);
    	   }
    }while(choice != 4);
    this is what i have ...

    Code:
    int choice(one,two,choice){switch(choice){
     case 1:addtogether(one,two);break;
     case 2:multiply(one,two);break;
     case 3:listall(one,two);break;}
    would i have to incorporate a for loop? and if so how?

    I'm just not really confident with my programming at this point so I'm a bit worried
    What happened to choice = menu() ? And I would not start a switch statement on the same line of code as choice = menu(), either. Follow the pattern they gave you!

    The do loop might be changed to something like this:
    Code:
    for(choice = 0; choice != 4; ;)  { /*don't forget 3rd semi-colon! */
       choice = menu();
        /* your switch statement here */
    
    }
    Last, but not least, I would not advise putting a closing curly brace at the end of a line of code. That is not a format that they are likely to want to see.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > for(choice = 0; choice != 4; ; ) { /*don't forget 3rd semi-colon! */
    When's the last time you wrote a for loop, Adak? There is no third semi-colon.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indeed, a 3rd one will give a compile error.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-01-2009, 07:54 PM
  2. Struct/parse returning wrong information
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 12-04-2007, 01:39 PM
  3. Assignment Help !! (Student information system)
    By ashb in forum C++ Programming
    Replies: 6
    Last Post: 03-12-2005, 05:32 AM
  4. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM
  5. Special Allegro Information
    By TechWins in forum Game Programming
    Replies: 12
    Last Post: 08-20-2002, 11:35 PM