Problem with If - Switches

This is a discussion on Problem with If - Switches within the C++ Programming forums, part of the General Programming Boards category; This is a two part question; I am attempting to write a code which will output a number in its ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    21

    Problem with If - Switches

    This is a two part question;

    I am attempting to write a code which will output a number in its txt form, for example if the input in 10 it outputs ten, if the input is 22 it outputs twenty-two.

    Now, yesterday it appeared I had this project complete. I could execute the code and it behaved exactly like I wanted it to. However, since installing cygwin the code doesnt appear to be behaving in the same way. When i try to compile it now explains to me that i need a parenthesis after the else if statement containing a condition, and even once I Include this, else if (tens >=2), the program no longer outputs correctly for numbers above 19.

    In conclusion, my questions are, Why is the compiler functioning so differently now, is this a good or a bad thing, and of course, i am curious as to what exactly is faulty about the code. Any hints or advice will be more than appreciated;

    Code:
    // Computer Science - Lab 2b
    // Outputing Text Format of Numbers 10-99
    // Adam Davis - 09/06/09
    
    #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  {
    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;
    }
    }
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    if (tens = 1) isn't what you want.
    Code:
    else if {
    is so amazingly illegal. else if what?? You need a condition to test with your if statement.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    Ok. I Figured this was the case. Im wondering how the hell was i able to compile it before without having Dev-C++ tell me that I was a total idiot.

    edit: also as i stated in the OP i had since modified the code to be - else if (tens >=2) , but since you are telling me that i dont want if tens = 1, i imagine i dont want this to be my else if statement either.
    Last edited by adamdavis; 09-07-2009 at 08:35 PM.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    else if {
    This is not the proper syntax. Remove 'if'. You only include it when you want to add a condition to the else statement

    Code:
    if (studying) {
    
    }
    else if (programming) {
    
    }
    else {
    
    }
    Incidentally, what compiler are you using under cygwin? "else if {" should not compile. The syntax for any if statement is: if ( expression ) statement
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    Quote Originally Posted by Mario F. View Post
    This is not the proper syntax. Remove 'if'. You only include it when you want to add a condition to the else statement

    ....

    Incidentally, what compiler are you using under cygwin? "else if {" should not compile. The syntax for any if statement is: if ( expression ) statement
    It compiled pre-Cygwi and worked without error. Now that I have successfully installed and am using cygwin the errors in my previous code are being noticed.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    Quote Originally Posted by adamdavis View Post
    edit: also as i stated in the OP i had since modified the code to be - else if (tens >=2) , but since you are telling me that i dont want if tens = 1, i imagine i dont want this to be my else if statement either.
    What tabstop means is that if (tens = 1) is actually assigning the value 1 to the variable tens. This is not what you want. You want to test its value. So,

    Code:
    if (tens == 1)
    Then you repeat changing the condition on the remaining else statements.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I am not aware of any compiler that has ever allowed such a thing. Tell us what it is, so that we may burn it in effigy.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    Quote Originally Posted by tabstop View Post
    I am not aware of any compiler that has ever allowed such a thing. Tell us what it is, so that we may burn it in effigy.
    i had not installed a compiler before my recent installation of Cygwin...so whatever black magic dev-C++ was using to compile my source was responsible.

    edit: also, thank you for the helpful responses. it is much appreciated.
    Last edited by adamdavis; 09-07-2009 at 09:23 PM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you didn't have a compiler, then you got the message "unable to find compiler" (or something similar), and I doubt that you would have considered that a success. Dev-C++ does come with the gcc compiler, and I will bet you any amount of money you like that gcc would never successfully compile the code you posted. Whether you were just not noticing the error messages or what I don't know.

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    Quote Originally Posted by tabstop View Post
    If you didn't have a compiler, then you got the message "unable to find compiler" (or something similar), and I doubt that you would have considered that a success. Dev-C++ does come with the gcc compiler, and I will bet you any amount of money you like that gcc would never successfully compile the code you posted. Whether you were just not noticing the error messages or what I don't know.
    Well there's no way to prove it now, short of me creating the same exact conditions on my laptop. Though I highly doubt that I'm incorrect in my assessment of the situation, since the alternative is that I experienced an insanely vivid hallucination in which my code worked.

    edit: Sorry if the situation is hard for you to swallow? But the code was compiled as an .exe before and I was able to execute said .exe w/ proper results in regards to what was expected from the program.
    Last edited by adamdavis; 09-07-2009 at 09:51 PM.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    See, that's the thing -- I have the same exact conditions on my laptop -- Dev-C++ and the code you posted, and all you get is
    Code:
    error: expected `(' before '{' token
    What you changed in the file I couldn't say.

    (Edit: Although I do have a temp.exe file -- from the previous file I had downloaded from here, that did compile. Just because you have a .exe file doesn't mean it's any good.)
    Last edited by tabstop; 09-07-2009 at 09:59 PM.

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    Quote Originally Posted by tabstop View Post
    See, that's the thing -- I have the same exact conditions on my laptop -- Dev-C++ and the code you posted, and all you get is
    Code:
    error: expected `(' before '{' token
    What you changed in the file I couldn't say.
    Since you're not getting the same results I received then i believe it's fair to say that you don't have the same exact conditions that I had earlier. Again, I apologize if you're having a hard time believing this, perhaps it was just some freak thing, but if you'd like I'll just say that I must be developing daytime hallucinations. Either way, this whole digression is a bit of a derail, yes?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by adamdavis View Post
    Since you're not getting the same results I received then i believe it's fair to say that you don't have the same exact conditions that I had earlier. Again, I apologize if you're having a hard time believing this, perhaps it was just some freak thing, but if you'd like I'll just say that I must be developing daytime hallucinations. Either way, this whole digression is a bit of a derail, yes?
    Until this same issue bites you again when you do lab 3, of course. But that's probably a whole week away, so we've got time.

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    Quote Originally Posted by adamdavis View Post
    the alternative is that I experienced an insanely vivid hallucination in which my code worked
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    Registered User
    Join Date
    Sep 2009
    Posts
    21
    the issue shouldn't bite me again as my compiler seems to be behaving correctly now though I'm sure I will run into future problems, as I am learning this language and don't possess savant like skills with it.

    Right now I'm attempting to figure out why when i type in 19 as my input my debugger is telling me the values of tens = 27 and ones = ]. The joys of learning.

    Again, sorry to blow your mind with my freak occurrence.

Page 1 of 3 123 LastLast
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, 10: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

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