Thread: If statement syntax?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Location
    St. Petersburg, FL
    Posts
    35

    Arrow If statement syntax?

    Hello, this is my first post, so I hope I don't come across negatively. I just started learning C programming language last week, so I am a complete newbie. I used to write basic scripts on mIRC about 8-10 years back, so i have a basic idea of what's going on. very basic...

    I started watching these University lectures here and the students were given an assignment to take input from the user and output how a viking would say that number. Well, I figured I would try the same thing and here it is. I am traveling in Vietnam right now, so I thought it would be a good idea to just use vietnamese instead of viking.

    The program is far from incomplete but I would like help on only one part for now. The If statement near the end is not working properly. When the program gets to that part, the variable is valid_entry is 1 and not 0, but it continues with the code as if it were 0. Thanks for your time and sorry i typed so much!

    Code:
    #include <stdio.h>
    
    
    
    int main(void) {
    
    	int number_input;                      //input from user.
    
    	int greater_than_ten = 0;          //avoids duplicate names.
    
    	int valid_entry = 0;                     //checks that the input is valid
    
    	int complete = 0;                       //will cause the program to loop
    
    	//char proceed_again = 'n';          //will allow to repeat translation.
    	
    
    	printf("Vietnamese Number Translator.\n\n");	
    
    	printf("Type a number from 1-100. ");
    
    	scanf("%d", &number_input);
    			
    
    	while ("complete == 0") {
    
    		
    
    		//check to make sure the number is between 1-100 
    
    		if (number_input < 101 && number_input > 0) {
    
    			valid_entry = 1;                 //skips the error
    
    			complete = 1;                   //completes the translation
    
    						
    
    			printf("\n");
    
    			printf("In Vietnamese, we say \"");
    
    	
    
    			//this section will calculate the numbers.
    
    			//it will group the 10's place with the one's.
    
    	
    
    	
    
    			//number 100
    
    			if (number_input == 100)
    
    				printf("mot tram\".\n");
    
    	
    
    			//for numbers 11-19
    
    			if (number_input >10 && number_input <20) {            
    
    				printf("muoi");     
    
    				number_input = number_input - 10;
    
    				}                                                             
    
    	
    
    			//for number 20    
    
    			if (number_input == 20) {
    
    				printf("hai muoi");
    
    				number_input = number_input - 10;
    
    				greater_than_ten = 1; 
    
    			}                                                
    
                                                                                        
    
    			//for numbers 21-30    
    
    			if (number_input >20 && number_input <31) {                                                
    
    				printf("hai muoi");                                                                      
    
    				number_input = number_input - 20;                                                           
    
    				greater_than_ten = 1; 
    
    			}                                                                                   
    
    	   
    
    			//for numbers 31-40                            
    
    			if (number_input >30 && number_input <41) {    
    
    				printf("ba muoi");                          
    
    				number_input = number_input - 30;
    
    				greater_than_ten = 1;            
    
    			}                                           
    
    	                                               
    
    			//for numbers 41-50                           
    
    			if (number_input >40 && number_input <51) {   
    
    				printf("bon muoi");                         
    
    				number_input = number_input - 40;
    
    				greater_than_ten = 1;           
    
    			}                                          
    
    	                                               
    
    			//for numbers 51-60                           
    
    			if (number_input >50 && number_input <61) {     
    
    				printf("lam muoi");                           
    
    				number_input = number_input - 50;
    
    				greater_than_ten = 1;             
    
    			}                                            
    
    	 
    
    			//for numbers 61-70                           
    
    			if (number_input >60 && number_input <71) {   
    
    				printf("sau muoi");                         
    
    				number_input = number_input - 60;
    
    				greater_than_ten = 1;           
    
    			}                                          
    
    	                                               
    
    			//for numbers 71-80                          
    
    			if (number_input >70 && number_input <81) {  
    
    				printf("bay muoi");                        
    
    				number_input = number_input - 70;
    
    				greater_than_ten = 1;          
    
    			}                                         
    
    	                                               
    
    			//for numbers 81-90                          
    
    			if (number_input >80 && number_input <91) {  
    
    				printf("tam muoi");
    
    				number_input = number_input - 80;
    
    				greater_than_ten = 1; 
    
    			}
    
    
    
    			//for numbers 91-99
    
    			if (number_input >90 && number_input <100) {
    
    				printf("chin muoi");
    
    				number_input = number_input - 90;
    
    				greater_than_ten = 1; 
    
    			}
    
    
    
    			//combines with previous section if needed.    
    
    			if (number_input == 1)
    
    				printf(" mot\".");
    
    
    
    			if (number_input == 2)
    
    				printf(" hai\".");
    
    
    
    			if (number_input == 3)
    
    				printf(" ba\".");
    
    
    
    			if (number_input == 4)
    
    				printf(" bon\".");
    
    
    
    			if (number_input == 5)
    
    				printf(" lam\".");
    
    
    
    			if (number_input == 6)
    
    				printf(" sau\".");
    
    
    
    			if (number_input == 7)
    
    				printf(" bay\".");
    
    
    
    			if (number_input == 8)
    
    				printf(" tam\".");
    
    
    
    			if (number_input == 9)
    
    				printf(" chin\".");
    
    
    
    			if (number_input == 10 && greater_than_ten == 0)  //to avoid * muoi muoi for 
    
    				printf("muoi\".");                                                  //numbers ending in 0.
    
        
    
    			if (number_input == 10 && greater_than_ten == 1)  //needed to place a "."" at the   
    
    				printf(".\"");                                                          //end of numbers ending in 0.
    
            
    
    			printf("\n");                                                                   //will print a new line 			
    
             
    
    		}  
    
    		
    
    		if ("valid_entry == 0") {
    
    			//added just to test the values
    
    			printf("v = %d c = %d n = %d", valid_entry, complete, number_input); 
    
    			//
    
    			printf("Sorry, please choose a number between 1-100. ");
    
    			scanf("%d", &number_input);
    
    		}	
    
    	}
    
          
    
    	return 0;
    
       
    
       }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    None of your other if statements have quotes -- why that one?

    (PS: Neither should your while statement.)

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    St. Petersburg, FL
    Posts
    35
    Quote Originally Posted by tabstop View Post
    None of your other if statements have quotes -- why that one?

    (PS: Neither should your while statement.)
    Such a fast response, thank you!

    Good point! At first i had
    Code:
     if (valid_entry = 0) {
    and i got an error suggesting the quotes. then later i changed it to ==

    It works now, thanks so much. Now I have to figure out how to avoid people entering a letter.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by kbat82 View Post
    Good point! At first i had
    Code:
     if (valid_entry = 0) {
    and i got an error suggesting the quotes. then later i changed it to ==
    No, you got an error suggesting parentheses, on the assumption you meant to assign.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    St. Petersburg, FL
    Posts
    35
    Quote Originally Posted by tabstop View Post
    No, you got an error suggesting parentheses, on the assumption you meant to assign.

    you're right! I don't know where my brain was. I didn't even have to check to make sure you were right. I guess my brain read parentheses and I understood quotes.

    Anyway, thanks again. One day this will all be easier for me.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So, if you're a mirc junkie, why choose C when there are other languages such as C++, C#, VB, Java, you-name-it-whatever?
    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.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    St. Petersburg, FL
    Posts
    35
    Quote Originally Posted by Elysia View Post
    So, if you're a mirc junkie, why choose C when there are other languages such as C++, C#, VB, Java, you-name-it-whatever?
    I thought it a good place to start. My goal is to be a well rounded programmer. I want to do it as a career someday. I've read that knowing c alone is better than knowing any other language. Even if that's not the case, It can't hurt. Also, if I figure out this is just a phase for me, I can use c for hobby projects. Maybe programming micro controllers. I have a lot to learn though.

    wasn't mIRC programmed in C? I can't remember.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It depends on what you want to do.
    If you're going to program micro controllers, C is probably your best choice.
    If you want to do C programming on software for computers, C++ is your best choice. And knowing C before C++ will only do more harm than good.
    And if you want more security or RAD, then a higher-level language may befit you. VB, Java or C#, among others.
    If you want to do web, C#, Java or some web-language such as PHP or Perl would probably be best.

    All are different (except for C and C++ which are quite similar), and have their own strengths and weaknesses.
    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.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    St. Petersburg, FL
    Posts
    35
    Quote Originally Posted by Elysia View Post
    It depends on what you want to do.
    If you're going to program micro controllers, C is probably your best choice.
    If you want to do C programming on software for computers, C++ is your best choice. And knowing C before C++ will only do more harm than good.
    And if you want more security or RAD, then a higher-level language may befit you. VB, Java or C#, among others.
    If you want to do web, C#, Java or some web-language such as PHP or Perl would probably be best.

    All are different (except for C and C++ which are quite similar), and have their own strengths and weaknesses.
    Interesting, thanks for the breakdown. Do you really think it will do more harm to learn C then C++? Would you suggest I skip C++ altogether? Or maybe learn it much later on? I thought about learning Java next. I don't know if web-languages interest me, but perhaps that will change in time. I would like to learn as many languages as possible, but that takes time I imagine.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by kbat82 View Post
    Do you really think it will do more harm to learn C then C++?
    If you plan to write computer software, then yes.
    EDIT: I meant if you only learn C, then yes, not if you learn both and use C++ to write computer software.

    Would you suggest I skip C++ altogether?
    Absolutely not.
    C++ is a modernization of C, and would very well be the language of choice for those who would use C, if it were not for lack of support for microcontrollers or so.
    Whether or not you learn C, I always will recommend learning C++. Chances are, if you like C, then you will also love C++.
    And if you would develop computer software, then all the more should you learn it. C hasn't really been designed for today's demanding computer software in mind. That's nowmore the job of C++ or other higher languages such as C#, Java, VB (though they are all no as close to the hardware as C++, or quite as fast).

    I thought about learning Java next.
    If portability is very important or you're making for the web, then I (IMO) would avoid Java. C++ should fit the void here quite well.

    I don't know if web-languages interest me, but perhaps that will change in time. I would like to learn as many languages as possible, but that takes time I imagine.
    That's absolutely fine, but be prepared for a hard journey, since every language has its pitfalls and good programming practices. For example, just because something is good practice in C doesn't mean it is in C++.
    That's the world we live in.
    Good luck!
    Last edited by Elysia; 09-18-2008 at 12:20 PM.
    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.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do you really think it will do more harm to learn C then C++?
    Personally, I disagree. If your intention is to learn C++, then learning C is just a detour that may actually make it more difficult to learn C++ without reverting to C techniques. In the long run, however, knowing both languages would make you a better programmer than knowing just one of them, since you would learn different things from using those languages.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    St. Petersburg, FL
    Posts
    35
    Quote Originally Posted by laserlight View Post
    Personally, I disagree. If your intention is to learn C++, then learning C is just a detour that may actually make it more difficult to learn C++ without reverting to C techniques. In the long run, however, knowing both languages would make you a better programmer than knowing just one of them, since you would learn different things from using those languages.
    I think we all agree here. I would like to learn C and C++ though. I want to be able to program micro controllers but not be limited to that. I think with this advice, i will learn C, then learn C++. I will just have to work extra to avoid being confused.

    What makes it difficult? Is the syntax similar but only slightly different?

    On a separate note, you're in Singapore?? I'm flying there tomorrow haha. A bunch of us are having a house warming party for my friend. He would have no problem if you came. bring a friend/boyfriend or whatever. Send me a message if you're interested. If not, maybe you would have time to tutor?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is the syntax similar but only slightly different?
    C++ is a larger language than C and inherits all of C's syntax (at least from the 1989/1990 version of the C standard). However, the language features that it adds leads to common idioms in C++ that you do not find in C, but when learning C++, you may be tempted to use idioms from C instead, thus slowing down your learning of C++ specific idioms.

    On a separate note, you're in Singapore??
    Yes indeed.

    I'm flying there tomorrow haha. A bunch of us are having a house warming party for my friend. He would have no problem if you came. bring a friend/boyfriend or whatever. Send me a message if you're interested.
    How long will you be staying in Singapore? University term break is next week, so I can spare time to meet up for say, lunch or dinner on Tuesday. Oh, and you have been fooled by my avatar and user title

    If not, maybe you would have time to tutor?
    Pick up a good book (there is a recommendation thread in this forum), use online tutorials, etc, then ask on this forum when you have a problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Sep 2008
    Location
    St. Petersburg, FL
    Posts
    35
    Quote Originally Posted by laserlight View Post
    C++ is a larger language than C and inherits all of C's syntax (at least from the 1989/1990 version of the C standard). However, the language features that it adds leads to common idioms in C++ that you do not find in C, but when learning C++, you may be tempted to use idioms from C instead, thus slowing down your learning of C++ specific idioms.
    Got it.


    How long will you be staying in Singapore? University term break is next week, so I can spare time to meet up for say, lunch or dinner on Tuesday. Oh, and you have been fooled by my avatar and user title
    Well, I will be here until the 23rd, then I will be flying to Thailand for 3 weeks, coming back to Singapore for 2, going home to Florida for 3 weeks, then moving to Singapore for a year.
    Tuesday sounds good, send me a message with your email or any contact info, or even just a place to meet. You can't still come to the party if youre not a girl haha. Even if you don't have a boyfriend haha. It's not a huge party, just a bunch of graphic designers and couchsurfers haha.


    Pick up a good book (there is a recommendation thread in this forum), use online tutorials, etc, then ask on this forum when you have a problem.
    Do you recommend a bookstore? I think there is a Borders,, but if you know a better/cheaper place, let me know.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do you recommend a bookstore? I think there is a Borders,, but if you know a better/cheaper place, let me know.
    I have bought programming books from Borders, Kinokuniya, MPH Bookstores, and uh, Amazon.com, and frankly, the price levels seem to vary, with Borders possibly being the most expensive out of the first three (or maybe it was just the books I bought). You might be able to learn the fundamentals of C programming within the loan period of your local library, so maybe that would be the cheapest option.

    By the way, I am taking the more personal part of our conversation to private messaging.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PlaySound
    By cangel in forum C++ Programming
    Replies: 16
    Last Post: 10-08-2009, 05:29 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Pointer syntax in if statement
    By fkheng in forum C Programming
    Replies: 5
    Last Post: 06-10-2003, 04:01 AM