3

I'm practicing C programming language before jump to objective-c, so I'm using the last version of XCode 4.6.3 (I believe this is the last version). I want to read an input, a numeric input (age), and show the dog age of a person. Here is my code

#include <stdio.h>
int main()
{
    int age;
    printf("How old are you? \n");
    scanf("%d",&age);
    age = age *7;
    printf(\nIn dog years you are %d years old",age);
    return 0;
}

so i enter my age and it doesn't show the result, sry for the newb question but I've already asked 4 people from work and nothing :( Thanks!

debugging

The Answer! I don't know why this happens, but I solved it (kind of fun hahahaha). I'm using apple keyboard and the NumLock ENter key doesn't work for debugg o.O . When I use the main Enter key, it works! Thanks everyone =)

5
  • 2
    Hi. "I'm practicing C programming language before jump to objective-c" - that's cool! It is the right order in which to learn those languages. Please consider getting used to some coding and formatting standards so that your code is easier to read. Also note that this question is not related to the IDE being used, rather to the language and the usage of a library function. Commented Sep 13, 2013 at 15:33
  • @KevinDTimm it shows nothing, I used some break points and debug, the error is on "scanf". It seems to don't get the input. I type the input,25, and hit enter, then the program exits. Commented Sep 13, 2013 at 16:38
  • @H2CO3 Hello, thnx for helping, I'll read the formatting standards! Commented Sep 13, 2013 at 16:40
  • 1
    see the answer from @dasblinkenblight, also - when you single step the code (since you're in the debugger) does it never return from scanf? Commented Sep 13, 2013 at 16:40
  • @KevinDTimm no, it never return from scanf =/ Commented Sep 13, 2013 at 17:01

2 Answers 2

4

This often happens because the program exits before the output buffer gets a chance to empty itself onto the console. Adding \n to the end of printf's format line should fix this problem:

printf("\nIn dog years you are %d years old\n",age);

Printing \n to an output stream which is connected to console "flushes" the output unless you change this setting in your program.

Note: C provides a shorter way of multiplying by 7: instead of

age = age * 7;

you can write

age *= 7;
Sign up to request clarification or add additional context in comments.

11 Comments

You bring up a good point about the output stream, but that isn't his issue here. I guess you can your answer is too good for his problem...?
@MohammadS. I don't think the missing quote is more than a typo the OP introduced while typing up the question, because the program wouldn't have compiled without it.
If he's new to programming, he may not know about compiling then running. Or he may not know that hes getting compile time errors. We'll only really know when he gives us more info.
@MohammadS. - unlikely, since he's running in the debugger
@dasblinkenlight I don't know why this happens, but I solved it (kind of fun hahahaha). I'm using apple keyboard and the NumLock ENter key doesn't work for debugg o.O . When I use the main Enter key, it works! Thanks everyone =)
|
1

I think you're missing a quote here:

printf(\nIn dog years you are %d years old",age);

Change it to:

printf("\nIn dog years you are %d years old",age);

I don't know how this is even compiling for you. You are probably getting compile time errors. Remember to compile your code first, then run. When it runs, remember to enter your input in the console, then hit the ENTER key.

I just tried this and it works for me: http://ideone.com/NFU2Ry

1 Comment

sry, I miss typed. I'm using the quote! Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.