Me and my 2.8+ million lines of code

This is a discussion on Me and my 2.8+ million lines of code within the C++ Programming forums, part of the General Programming Boards category; You can't. char[256] is a pointer to a char, and if you did a switch, you would be checking the ...

  1. #16
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    You can't.
    char[256] is a pointer to a char, and if you did a switch, you would be checking the address of a char (the 1st element of an array of chars). So input would be interpreted as something like 23847981 (the address of the first char), instead of whatever the user typed in. You could do:
    Code:
    switch(*input)
    {
    }
    But that would only check the first letter of input, so you still have to use strcmp(input, "asdf") to check the input of a string.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #17
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    i guess im asking for how exactly strcmp would look in that. All I can do with strcmp is

    Code:
    if (!strcmpi("n", command) || !strcmpi("no", command))
    and i have no idea how it would fit into a switch statement
    AIM: MarderIII

  3. #18
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hmm, not sure what you're asking, but it would be easier to just check if the first letter is 'n', since that's pretty similar to what you're doing:
    Code:
    if(command[0] == 'n')
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #19
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    easy money....

    Switch case statement are like if statements, except easier to handle multiple stats with one char, int, flaot, etc.

    like:

    Code:
    int choice;
    cin >> choice;
    switch (choice)
    {
       case '1' : cout << "you choose 1\n";
       break;
       case '2' : cout << "You choose 2\n";
       break;
       defualt : cout << "please press a real button\n";
       break;
    }
    opposed to:

    Code:
    if (choice == '1')
    {
     cout << "You choose 1\n";
    }
    
    else if (choice == '2')
    {
     cout << "You choose 2\n");
    }
    
    else 
    {
      cout << "please press a real key\n";
    }

    See? Hope that helps.
    This war, like the next war, is a war to end war.

  5. #20
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    Sorry, didn't see the previous coding here...
    This war, like the next war, is a war to end war.

  6. #21
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    By the way:
    Code:
    int choice;
    cin >> choice;
    switch (choice)
    {
       case 1 : cout << "you choose 1\n";
       break;
       case 2 : cout << "You choose 2\n";
       break;
       defualt : cout << "please press a real button\n";
       break;
    }
    The quotes would screw up the result, 'cuz then it would check for the ascii values of '1', '2', while the input stored in choice is in integer form.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #22
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    if i dont have the ' ' around it it get errors.
    This war, like the next war, is a war to end war.

  8. #23
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Then something must be screwy with your compiler... putting ' ' around the letter only changes the letter into an ascii value, which is an int anyways. Besides, what error are you getting?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #24
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    But what If im not entering an integer? Then

    case 1 doesnt mean anything
    AIM: MarderIII

  10. #25
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Code:
    char input;
    cin >> input;
    
    switch(input)
    {
          case 'n':
                      cout << "You pressed n";
                      break;
          case 'p':
                     cout << "You pressed p";
                     break;
          default:
                     cout << "You didn't press n or p";
    }
    switch statements can only deal with ints or chars, no arrays or floats.
    Last edited by HybridM; 05-14-2003 at 03:47 AM.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  11. #26
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    oh - someone should write a tutorial that explains that, thanks
    AIM: MarderIII

  12. #27
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    You should buy a reference book, they're invaluable resources.

  13. #28
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    Get Primer C++, its one of the BEST out there.
    This war, like the next war, is a war to end war.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

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