Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C - switch statement
04-07-2013, 12:45 AM, (This post was last modified: 04-07-2013, 01:11 AM by Cybercrookz.)
Post: #1
Sad  C - switch statement
Code:
#include <stdio.h>
//Global Variable
int i;
int num1, num2, result = 0;
int action;

displayMsg()
{    
    printf("\t\t*********ARITHEMATIC OPERATIONS*********\n\n");
    printf("\n\t1. Addition\n");
    printf("\t2. Subtraction\n");
    printf("\t3. Multiplication\n");
    printf("\t4. Division\n");
    printf("\t5. EXIT\n\n");
    printf("\t\t*****************************************\n\n");

    printf("Enter first number: ");
    scanf_s("%i", &num1);

    printf("\nEnter second number: ");
    scanf_s("%i", &num2);

    printf("\nChoose an action (1,2,3,4,5): ");
    scanf_s("%i", &action);
}

addNum(int x)
{
    printf("\n\tAddition Calculations\n\n");
        printf("\n%i + %i = %i\n",num1,num2,(num1+num2));

    return;
}

subtractNum(int x)
{
    printf("\n\tSubtraction Calculations\n\n");
        printf("\n%i - %i = %i\n",num1,num2,(num1-num2));
        if (num2 < 0)
            printf("\n\tInvalid Input\n");

    return;
}

multiplyNum(int x)
{
    printf("\n\tMultiplication Calculations\n\n");
        printf("\n%i x %i = %i\n",num1,num2,(num1*num2));

    return;
}

divideNum(int x)
{
    printf("\n\tDivision Calculations\n\n");
        printf("\n%i / %i = %i\n",num1,num2,(num1/num2));

    return;
}

displayExitMsg()
{    
    printf("\n\n\t\t\tYou choosed EXIT\n");
    printf("\t\t--------HAVE A NICE DAY-------\n\n");
}

int main();
{
    do
    {
        displayMsg();
        switch
        {
        case 1: { addNum;
            break; }
        case 2: { subtractNum;
            break; }
        case 3: { multiplyNum;
            break; }
        case 4: { divideNum;
            break; }
    }while (action < 5);

    if (action = 5);
        displayExitMsg()

    
    return 0;
}
$GetCurrentSignature();

[Image: dcc46f8439.jpg]

$GetCurrentSignature();





Troll 2
Reply
04-07-2013, 12:54 AM,
Post: #2
RE: C - switch statement
try buang void, bila call function. apsal ada void??? Smile
̿ ̿ ̿̿'̿̿\̵͇̿̿\=(•̪●)=/̵͇̿̿/'̿̿ ̿ ̿ ̿ - انا کڤيتݢولوڠ
http://fatah.afraid.org/
[Image: 763440762.png]
Reply
04-07-2013, 01:11 AM,
Post: #3
RE: C - switch statement
abam fatah. cer tngk coding baru tu, stil ada error, code updated OK
$GetCurrentSignature();

[Image: dcc46f8439.jpg]

$GetCurrentSignature();





Troll 2
Reply
04-07-2013, 02:01 AM,
Post: #4
RE: C - switch statement
to much mistake.. you should check and understand the basic C statement.
Reply
04-07-2013, 02:09 AM,
Post: #5
RE: C - switch statement
(04-07-2013, 12:45 AM)Cybercrookz Wrote:
Code:
#include <stdio.h>
//Global Variable
int i;
int num1, num2, result = 0;
int action;

displayMsg()
{    
    printf("\t\t*********ARITHEMATIC OPERATIONS*********\n\n");
    printf("\n\t1. Addition\n");
    printf("\t2. Subtraction\n");
    printf("\t3. Multiplication\n");
    printf("\t4. Division\n");
    printf("\t5. EXIT\n\n");
    printf("\t\t*****************************************\n\n");

    printf("Enter first number: ");
    scanf_s("%i", &num1);

    printf("\nEnter second number: ");
    scanf_s("%i", &num2);

    printf("\nChoose an action (1,2,3,4,5): ");
    scanf_s("%i", &action);
}

// addNum(int x) <- tak payah letak "int x" sbb variable dah set global
addNum()
{
    printf("\n\tAddition Calculations\n\n");
        printf("\n%i + %i = %i\n",num1,num2,(num1+num2));

    return;
}

// subtractNum(int x) <- tak payah letak "int x" sbb variable dah set global
subtractNum()
{
    printf("\n\tSubtraction Calculations\n\n");
        printf("\n%i - %i = %i\n",num1,num2,(num1-num2));
        if (num2 < 0)
            printf("\n\tInvalid Input\n");

    return;
}

// multiplyNum(int x) <- tak payah letak "int x" sbb variable dah set global
multiplyNum()
{
    printf("\n\tMultiplication Calculations\n\n");
        printf("\n%i x %i = %i\n",num1,num2,(num1*num2));

    return;
}

// divideNum(int x) <- tak payah letak "int x" sbb variable num1, num2 dah set global
divideNum()
{
    printf("\n\tDivision Calculations\n\n");
        printf("\n%i / %i = %i\n",num1,num2,(num1/num2));

    return;
}

displayExitMsg()
{    
    printf("\n\n\t\t\tYou choosed EXIT\n");
    printf("\t\t--------HAVE A NICE DAY-------\n\n");
}

// int main(); <- delete ";" anggap main tu mcm nak declare function jugak
int main()
{
    do
    {
        displayMsg();
/*        switch
        {
        case 1: { addNum;
            break; }
        case 2: { subtractNum;
            break; }
        case 3: { multiplyNum;
            break; }
        case 4: { divideNum;
            break; }
    }while (action < 5);
*/

        switch (action)
        {
            case 1: addNum();
                    break;
            case 2: subtractNum();
                    break;
            case 3: multiplyNum();
                    break;
            case 4: divideNum();
                    break;
            default: break;
        }
    } while (action < 5);

    //if (action = 5); <- delete ";" letak kat ujung displayExitMsg()
    if (action = 5)
        displayExitMsg();

    
    return 0;
}
Reply
04-07-2013, 02:35 AM,
Post: #6
RE: C - switch statement
switch(<expression>) {

}
puas sudah menanam ubi, nenas juga dibeli orang, puas sudah aku berbakti, pengganas juga dipandang orang

Quote:A true warrior need blood on his sword; A true pirate never afraid to sail the red sea.
Reply
04-07-2013, 08:55 AM, (This post was last modified: 04-07-2013, 08:59 AM by syahmiazhar.)
Post: #7
RE: C - switch statement
Bismillah...

1. Elakkan guna global variable

2. Indent kod dengan betul

3. Masalah kod:

function declare macam ni subtractNum(int x) tapi kod pada switch panggilnya macam ni substractNum;... cara ni salah, nak panggil function mestilah ada parentheses/bracket, dan kalau ada parameter kena letak parameter sekali... contoh: subsctractNum(12345);. 12345 = parameter bagi function.

Lagi satu, function yang tak ada return value declarekan macam ni void substractNum(int x) {...}. Jangan biarkan kosong, letak void atau return value yang sesuai.

Oleh sebab int x tak dipakai, jadi buang je jadi void substractNum() {...}

---

switch, macam if mesti ada expression. switch (action) {...}

case dalam switch tak perlu buat kod block baru melainkan kita nak declare variable.. block/compound statement ialah kod-kod atau pernyataan yang dikumpulkan bersama dalam bracket { }. Jadi, buang block pada case dan tukar macamni:
Code:
switch( action )
{
case 0:
   addNum();
   break;
case 1:
...
}

Comparison atau assignment? Dalam c, operator assignment ialah sama dengan '='. Assignment ialah memasukkan sesuatu nilai dalam variable, manakala operator comparison/perbandingan ialah ==,!=,>, dan lain2...

if (action = 5) ..., dalam kod ni, 5 dimasukkan dalam variable action, kemudian dia compare, if (5), disebabkan 5 nilai positif dan bukan kosong, jadi expression ni sentiasa benar/true... Tukar jadi if (action == 5).

Semicolon ";" ialah pemisah antara satu kod dengan kod yang lain. Kalau dalam bahasa visual basic atau python, tekan ENTER mungkin kod dah terpisah, tapi dalam C/PHP, semicolon merupakan pemisah kod. Jadi kalau pernyataan if diikuti dengan semicolon, .if (..); pernyataan selepas tu dah terpisah, takde kaitan lagi dengan if...

buang semicolon:
Code:
if (...) do_something();
atau lebih cantik dan selamat, letak block
Code:
if (...) {
   do_something();
}

Contoh kod dalam satu baris, dipisahkan dengan semicolon:
Code:
int main() { int x, y; x = 5; y = 10; printf("x + y = %d\n", x + y); return 0; }
Reply
04-07-2013, 10:33 AM, (This post was last modified: 04-07-2013, 10:34 AM by Cybercrookz.)
Post: #8
RE: C - switch statement
awesomely solved Thumbs Up special thanks 2 all tbdians.. all of ur feedback were totally worth it ^^

*NOTE*
fatah
fliptopbox
syahmiazhar

explanation dia org mmg clear Thumbs Up
$GetCurrentSignature();

[Image: dcc46f8439.jpg]

$GetCurrentSignature();





Troll 2
Reply
04-07-2013, 10:49 AM,
Post: #9
RE: C - switch statement
greetz to syahmiazhar atas kerajinan membuat brief documentation.

#kalau boleh push ke wiki tbd sekali ka
̿ ̿ ̿̿'̿̿\̵͇̿̿\=(•̪●)=/̵͇̿̿/'̿̿ ̿ ̿ ̿ - انا کڤيتݢولوڠ
http://fatah.afraid.org/
[Image: 763440762.png]
Reply
04-07-2013, 09:32 PM,
Post: #10
RE: C - switch statement
explaination syahmiazhar terang dan nyata, aku pun dapat ilmu dari explainaton tu.
Reply
05-07-2013, 11:39 PM,
Post: #11
RE: C - switch statement
(04-07-2013, 12:45 AM)Cybercrookz Wrote:
Code:
int main();
{
    do
    {
        displayMsg();
        switch
        {
        case 1: { addNum;
            break; }
        case 2: { subtractNum;
            break; }
        case 3: { multiplyNum;
            break; }
        case 4: { divideNum;
            break; }
    }while (action < 5);
}
saje nak bagi pendapat,

kalau action < 5, program ni masih boleh loop kalau input tu -1, -2, -3(tapi sebaliknya kalau input >= 5 program ni akan terminate = undefined behaviour)

Fikirlah: kenapa bila input >= 5, program tu terminate tetapi kalau input < 1 program xterminate? OK

so, while(action >= 1 && action <= 4) akan enhance integriti program tu sebab kita enforce range checking(memastikan bahawa user input tidak melebihi atau kurang daripada apa yg kita dah set) dlm code tu boss

(04-07-2013, 08:55 AM)syahmiazhar Wrote:
Code:
if (...) do_something();
atau lebih cantik dan selamat, letak block
Code:
if (...) {
   do_something();
}
[/code]
Addon: Example #2 lebih selamat kerana Example #1 cuma leh letak satu statement je, kalo ada statement selepas tu, statement tu tak termasuk dlm condition if

Contoh():
Code:
int condition = 0;

if(condition == 1)
    printf("%s \n", "[b]Dalam[/b] if statement");
    printf("%s \n", "[i]kat luar[/i] if statement");

Sebab condition bukan -1, so biasanya kita akan anggap statement dlm if statement tu takkan execute, tapi

Code:
printf("%s \n", "[i]kat luar[/i] if statement");

akan diexecute sebab statement tu bukan dalam block if statement

konklusi: guna braces "{}" dalam if else statement kalau statement kat dalam if else statement tu lebih dari satu statement

p/s: just my 2 cents
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  [Solved] Loop dalam switch structure. ibnumalik 10 3,208 05-04-2012, 10:21 AM
Last Post: ibnumalik
  [help]c++ function execute if else statement output pron3t 10 1,109 16-03-2012, 12:34 PM
Last Post: pron3t
  c++ switch payment [basic coding] yonyusuff 1 521 20-08-2011, 06:38 PM
Last Post: LongJohnSilver
  Help - C simple statement th3phantom 12 1,486 26-06-2011, 11:09 PM
Last Post: JejakaPemalu

Forum Jump: