Thread: Problem with If - Switches

  1. #16
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    Quote Originally Posted by Mario F. View Post
    Actually a quite plausible explanation given the circumstances. Most probably you got your file changed by some other means and didn't even realized it. Possibly you used an older version of the source file when you moved it onto cygwin, or something like that.

    What is guaranteed is that version would never compile on gcc. Ever.
    I'm really not sure what to tell you. Obviously i am not a C++ expert, but I know enough about computers to tell the difference between when a program tells me i completed an operation successfully and when it tells me it failed.

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by adamdavis View Post
    Again, sorry to blow your mind with my freak occurrence.
    My mind is not blown; I am merely attempting out of dull dreary habit to teach you something which you don't particularly seem to want to know. I admit to mild curiosity as to whether you just ignored the errors and used the old executables or changed the input file with or without realizing it and just aren't owning up to it, but I can live without knowing that.

    Anyway, I'll let you ask a question about your debug problem before going on about that.

  3. #18
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    Quote Originally Posted by tabstop View Post
    My mind is not blown; I am merely attempting out of dull dreary habit to teach you something which you don't particularly seem to want to know. I admit to mild curiosity as to whether you just ignored the errors and used the old executables or changed the input file with or without realizing it and just aren't owning up to it, but I can live without knowing that.

    Anyway, I'll let you ask a question about your debug problem before going on about that.
    Shrug. We're going to have to go with hallucinations, it seems. As far as I know, if there is an error with compiling, Dev_C++ would clue me in on this and the .exe would not be compiled (you made mention of getting some sort of temp.exe? this however was not the case for me). I am certainly not too proud to admit to making mistakes at this point in the game, and if that were the case I would gladly admit it. I am simply recounting the situation as I experienced it and really do find it a tad distressing for one reason or another that apparently that this situation is without a doubt impossible. Perhaps someone happened that I didn't even realize? I don't know. Either way, I apologize for my ignorance if this is the case, and assure you I am being wholeheartedly sincere about the situation.

    As far as my debug problem goes, you know what it is, you can help if you'd like, I will of course try to figure it out on my own. Posting on a code forum was not close to my first step in figuring out this problem originally, and I only did so because the situation i was in left me scratching my head.

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by adamdavis View Post
    Shrug. We're going to have to go with hallucinations, it seems. As far as I know, if there is an error with compiling, Dev_C++ would clue me in on this and the .exe would not be compiled.
    That seems like a pretty silly assumption. If you open up a word doc, make some changes, and then exit Word without saving, your old document is still there. Same deal here -- you tried to change your .exe, it failed, so your old .exe is still there. (That's under the assumption that you had a version without this line in it.)
    Quote Originally Posted by adamdavis View Post
    As far as my debug problem goes, you know what it is, you can help if you'd like, I will of course try to figure it out on my own. Posting on a code forum was not close to my first step in figuring out this problem originally, and I only did so because the situation i was in left me scratching my head.
    Are you printing the chars from the program? From gdb? When are you printing?

  5. #20
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    Quote Originally Posted by tabstop View Post
    That seems like a pretty silly assumption. If you open up a word doc, make some changes, and then exit Word without saving, your old document is still there. Same deal here -- you tried to change your .exe, it failed, so your old .exe is still there. (That's under the assumption that you had a version without this line in it.)
    this is the thing, i didn't have an earlier version where i had the correct else if statement. But we should stop talking about this, yes? Beating a dead horse, and probably making myself look even more incompetent.



    Are you printing the chars from the program? From gdb? When are you printing?
    I am running Eclipse in Debug mode, tracing through the steps individually. Here's a screen shot
    and pasted below is the updated version of the code;

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
    
    cout << "Enter a Number Between 10-99: ";
    char tens, ones;
    cin >> tens >> ones;
    
    if (tens == 1)
    switch (ones) {
        case '0' : cout << "Ten"; break;
        case '1' : cout << "Eleven"; break;
    	case '2' : cout << "Twelve"; break;
    	case '3' : cout << "Thirteen"; break;
    	case '4' : cout << "Fourteen"; break;
    	case '5' : cout << "Fifteen"; break;
    	case '6' : cout << "Sixteen"; break;
    	case '7' : cout << "Seventeen"; break;
    	case '8' : cout << "Eighteen"; break;
    	case '9' : cout << "Nineteen"; break;
    }
    
    else if (tens >=2){
    switch (tens) {
    	case '2' : cout << "Twenty"; break;
    	case '3' : cout << "Thirty"; break;
    	case '4' : cout << "Fourty"; break;
    	case '5' : cout << "Fifty"; break;
    	case '6' : cout << "Sixty"; break;
    	case '7' : cout << "Seventy"; break;
    	case '8' : cout << "Eighty"; break;
    	case '9' : cout << "Nintey"; break;
    	default: cout << "Number not within allowed range" << endl;
    }
    switch (ones) {
        case '0' : cout << " "; break;
        case '1' : cout << " one"; break;
        case '2' : cout << " two"; break;
    	case '3' : cout << " three"; break;
    	case '4' : cout << " four"; break;
    	case '5' : cout << " five"; break;
    	case '6' : cout << " six"; break;
    	case '7' : cout << " seven"; break;
    	case '8' : cout << " eight"; break;
    	case '9' : cout << " nine"; break;
    	default: cout << "Number not within allowed range" << endl;
    }
    }
    }
    Thank you again for taking the time to help.

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Alright. So we should have tens == '1' for sure. Now my mind is blown: 27 is ESC, of course, and [ means that we're looking at good old ANSI terminal escape codes, which seems both outrageous and distressing Do you have a setting for that in Eclipse? (You've gotten farther along in using Eclipse for C++ then most people I meet so perhaps this is some quirk of Eclipse I've not come across.) I've not used Cygwin in a while either, but I don't remember it introducing escape sequences when reading characters.

  7. #22
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    Quote Originally Posted by tabstop View Post
    Alright. So we should have tens == '1' for sure. Now my mind is blown: 27 is ESC, of course, and [ means that we're looking at good old ANSI terminal escape codes, which seems both outrageous and distressing Do you have a setting for that in Eclipse? (You've gotten farther along in using Eclipse for C++ then most people I meet so perhaps this is some quirk of Eclipse I've not come across.) I've not used Cygwin in a while either, but I don't remember it introducing escape sequences when reading characters.
    I don't believe I have a setting for that in Eclipse [I have not fooled around much with the settings in Eclipse at all, and am thinking I may try a clean install of it because I don't like that I am getting escape chars when I am clearly entering valid numeric characters] .

    I believe it is worth mentioning that in my earlier post i mentioned that I was receiving these values [ 27 and ']' ]when I entered 16 as my input, but after running another debug session 16 was input correctly [1 in the tens, 6 in the ones], but Eclipse still blew right by switches under if (tens==1) and i was not receiving the correct output.

    edit: I do however receive correct outputs when my input is within the 20-99 range.

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you do need tens=='1' and not tens==1.

  9. #24
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    Quote Originally Posted by tabstop View Post
    So you do need tens=='1' and not tens==1.


    I would give you a hug right now if I could.
    I considered that the code might not be right because I was thinking of the values in ones and tens as ints, but since they are of type char they're not really numbers, right?

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by adamdavis View Post


    I would give you a hug right now if I could.
    I considered that the code might not be right because I was thinking of the values in ones and tens as ints, but since they are of type char they're not really numbers, right?
    Oh, they're numbers, just not the numbers you're thinking of (unless you're thinking of ASCII). '1' is 49, for instance.

  11. #26
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    Quote Originally Posted by tabstop View Post
    Oh, they're numbers, just not the numbers you're thinking of (unless you're thinking of ASCII). '1' is 49, for instance.
    Definitely not thinking of ASCII So just to make sure I am on the proper page here; Would it be incorrect to even try to do a condition like else if (tens >=2)?? Or can C++ still take this sort of condition into consideration despite the fact that the variable is a char and not an int.

  12. #27
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can do tens >= 2 -- that would just check whether the character tens had an ASCII value greater than or equal to two.

  13. #28
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    Quote Originally Posted by tabstop View Post
    You can do tens >= 2 -- that would just check whether the character tens had an ASCII value greater than or equal to two.
    Ok. Thanks again for your help, it's greatly appreciated. In the future I hope I will be able to come to these conclusions on my own.

  14. #29
    Registered User
    Join Date
    Apr 2008
    Location
    Haddock, GA, United States
    Posts
    13
    I'm not really sure what you'd like to do, but this code may help you.

    Code:
    // Computer Science - Lab 2b
    // Outputing Text Format of Numbers 10-99
    // Adam Davis - 09/06/09
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    char* number[10] = {
        "zero",
        "one",
        "two",
        "three",
        "four",
        "five",
        "six",
        "seven",
        "eight",
        "nine"
    };
    
    int main(void)
    {
    
        char command[10] = {0};
        int i;
    
        scanf("%9s", command);
        if( isdigit(command[0]) && (command[1] == '\0') ) printf("%s\n", number[command[0] - '0']);
            else for (i = 0; i < 10; i++)
        if(strncmp(command, number[i], strlen(number[i])) == 0) printf("%d\n", i);
    
        return 0;
    }

  15. #30
    Registered User
    Join Date
    Apr 2008
    Location
    Haddock, GA, United States
    Posts
    13
    P.S.
    And you can customize for how many numbers you would like, of course.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM