Help C programming with Xcode

Discussion in 'Mac Programming' started by crazyjorgito, Sep 27, 2012.

  1. crazyjorgito, Sep 27, 2012
    Last edited by a moderator: Sep 27, 2012

    crazyjorgito macrumors newbie

    Joined:
    Sep 27, 2012
    #1
    Hello, I have a MacBook Pro 2011(i5). Right now I am learning to program in C with a book and i have a problem with the function getchar().
    In the program below (if everything is correct) it seems as if option=getchar() is skipped when i get to execute...the program is the one below. Thank you very much, I hope you guys help me out!
    Code:
    main()
    {
    	int speed, distance;
    	char option;
    	float time;
    
    	printf("This program calculates how much time it takes to go at a certain speed over a distance \n");
    	printf("Introduce distance: \n");
    	scanf("%d", &distance);
    	printf("Introduce velocity: \n");
    	scanf("%d", &speed);
    	time=distance/speed;
    	printf("The answer is %f seconds \n", time);
    	printf("Want to try again (y/n)?\n");
    	option=getchar();
    	
    	
    	if (option=='y') 
    	{
    	
    		printf("Introduce distance: \n");
    		scanf("%d", &distance);
    		printf("Introduce speed: \n");
    		scanf("%d", &speed);
    		time=distance/speed;
    		printf("The answer is %f seconds \n", time);
    	}
    	else 
    	{
    		printf("Thanks.");
    	}
    
    	
    	
    }
    
     
  2. rdo macrumors newbie

    Joined:
    May 24, 2011
    #2
    probably need to flush the io buffer...
    you can use fflush() for that... right before the getchar() line.
     
  3. crazyjorgito thread starter macrumors newbie

    Joined:
    Sep 27, 2012
    #3
    Thanks but i don't understand why (i'm new to programming) because normally getchar() works for me, therefore i think it must be something else..

    Anyway, thank you very much!
     
  4. rdo macrumors newbie

    Joined:
    May 24, 2011
    #4
  5. crazyjorgito thread starter macrumors newbie

    Joined:
    Sep 27, 2012
    #5
    Thank you very much, i will take a look a it now!!

    Thanks!!!
     
  6. crazyjorgito thread starter macrumors newbie

    Joined:
    Sep 27, 2012
    #6
    I have been looking at the link you sent me but I still dont know how to solve this problem in a simple way...
    I will keep reading but hopefully someone from the forum can help us out.

    Thanks.
     
  7. chown33, Sep 27, 2012
    Last edited: Sep 27, 2012

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    This was the answer at Stack Overflow:
    Your scanf only ate the number but not the trailing newline. Putting a newline or white space after the %d will then give you the opposite problem, reading too far.

    This is why people don't like scanf.

    I would suggest reading an actual line (use fgets(3)) and then using sscanf() to scan the string.
    Please explain how that isn't a simple enough solution.

    There are other solutions, but they may not be simpler or as reliable. You should start by reading reference docs. For example, you could call scanf() to read a string instead of calling getChar(), reading into a string variable. Then check the first char for 'y' or 'n'. You'll have to read the docs for scanf(), and make sure to limit the length of what's being read. Otherwise, if someone enters too long a string, such as "yesindeed", you'd overflow a buffer.

    You can also tell scanf() to skip trailing whitespace, which is what the first sentence of the Stack Overflow reply is suggesting. If you don't understand why that might work, or what happens if you do it, read the scanf() reference doc then try it.


    Finally, when you're learning from a book, it's a good idea to post the complete title, author, and edition, so we know what you're reading.

    Many books have online websites. Have you tried the website for your book?
     
  8. crazyjorgito thread starter macrumors newbie

    Joined:
    Sep 27, 2012
    #8
    Hello, thanks again. Yes, I did read that but although y erased the "\n" and executed the program, it still did not work.
    I'm spanish, so excuse me if i don't understand 100% of the words that you guys write in the posts (sorry for that).
    Anyway, as I said before, I understand that if I put a "\n" before scanf it will read my data plus the "\n" (right)?
    Therefore if I erase "\n" the program should work...but it doesn't. So what is my program missing? Could you edit it and post it to see what you mean?

    I am learning from a Herbert Schildt book. The english version is called "Teach yourself C".

    Thanks again and sorry for that..!
     
  9. macuser1232 macrumors 6502a

    macuser1232

    Joined:
    Jan 20, 2012
    #9
    I also have a 2011 MBP i5 and am programming C in Xcode! :)
     
  10. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #10
    Post your code.

    If you have several versions of it, post each version (or at least the actual changed code of each version) using CODE tags, and then describe what happened in each case.


    At this point, you should probably a simple test program. First, it's a good idea to learn how to do this. Writing test programs isn't just a learning tool, it's a testing tool. Second, by isolating only the one thing you're testing (or learning about), you can focus on the one thing of importance. Third, shorter programs are easier to post, and easier for other people to try.

    Do this: write a program that only tests the Y/N input. It should prompt for Y/N, read the input, then print one line that identifies whether it got a Yes or a No answer. It doesn't need to loop. It doesn't need to read any other data. It just needs to read a Y/N input and print the outcome. Then post the code, for every version you have a question about, and describe how each one behaves. If you're systematic, I don't see it needing more than 3 versions.

    If that seems like a lot of effort when I could just give you the answer, you should get used to doing it. Reading docs, writing tests, re-reading docs, trying something else -- that's how real programmers do things. So learning how to go through that process (the fancy name is "successive refinement" or "stepwise refinement") is an important part of learning how to write real programs.
     
  11. robvas macrumors 68020

    Joined:
    Mar 29, 2009
    Location:
    USA
    #11
    Schildt would not be on my list of books I would suggest learning from!

    Have you thought about jumping into Objective-C with Cocoa to make Mac apps, or using something like Python or Ruby to learn?

    C is so cumbersome when doing simple things like getting keyboard input - I hate to see new programmers become frustrated and give up or lose interest.
     
  12. macuser1232 macrumors 6502a

    macuser1232

    Joined:
    Jan 20, 2012
    #12
    Start with Python! Python is an outstanding language. In fact, my friend and I meet tomorrow to continue working on a Python programming project.
     
  13. cookiesnfooty macrumors 6502

    cookiesnfooty

    Joined:
    Jul 1, 2009
    Location:
    Harrogate
    #13
    Can you elaborate? What makes Python outstanding?
     
  14. macuser1232 macrumors 6502a

    macuser1232

    Joined:
    Jan 20, 2012
    #14
    It is a very easy to learn language that can also be related with higher level langauges. It's very efficient and is active. When I say active I mean that it is still being improved. For example, Python 3.2 was released just last year.
     

Share This Page