C Programming in Xcode, scanf doesn't read int - c

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!
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 =)

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;

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

Related

When i run a program on eclipse, the console shows nothing until I type in something, then takes that as the first input (for scanf for example) [duplicate]

This question already has an answer here:
printf not print on the console in eclipse?
(1 answer)
Closed 12 months ago.
So I just installed eclipse on windows after having too many problems with my old installation on a vm running Ubuntu.
All problems gone except for what I wrote in the title. Scanf text never comes up first, no matter which program I'm running. It shows text when I have entered all that I can enter. Let me give you an example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int number1;
int number2;
printf ("Type in a number \n");
scanf ("%d", &number1);
printf ("Type in a number \n");
scanf ("%d", &number2);
int number3 = number1 * number2;
printf ("%d", number3);
return 0;
}
Console looks like this:
2
3
Type in a number
Type in a number
6
2 and 3 I had to type in first, then the last three lines showed up. That's not how it's supposed to be is it?
Thanks for any help!
What seems to work is writing
setbuf(stdout, NULL);
into every main of every program. It does fix the problem, yet there's maybe another fix that's not so annoying as doing that everytime. It's not bad, but I don't get why it simply worked on another instance of eclipse but here not.

scanf seems to break my code (C programming) [duplicate]

This question already has answers here:
C/C++ printf() before scanf() issue
(2 answers)
Closed 1 year ago.
I'm programming a text-based adventure game for my C Programming class, but for whatever reason, the code seems to break whenever I try to use "scanf" to get a player's input. Everything will print onto the console perfectly fine, but then when I include the line "scanf("%d", playerInput), nothing will print out and the program will run endlessly. Anyone know why this is the case? Here's my code:
#include <stdbool.h>
#include <stdio.h>
int main()
{
printf("~ Doctor Crowley: Master of Death ~\n"); //Titlecard
//Introductory narration =================================================================================================
printf("\n- Narrator -\n");
printf("You are Doctor Jonathan Crowley, an archeologist and wizard from the Arcane University in Aeternum.\n");
printf("You awaken in your classroom on the first floor of the University. Last you remember, you were in your office\n");
printf("on the sixth floor of the University.\n");
// Player Actions (1) ========================================================================================================================
printf("\n1. Stand up\n");
printf("2. Look around\n");
printf("-------------------------------------------\n");
int playerInput; //player input variable
printf("--> ");
scanf("%d", &playerInput);
return 0;
}
First of all make sure to write '&' before any variable in scanf. If you don't write that, it will crash and force you to end the program. Then as you write in your code it just input number and will put that in variable playerInput, and nothing more. You didn't print anything after scanf, so nothing will happen after that. For example you can write printf("%d",playerInput); after that and it will print the number that is in playerInput.

scanf always prints entered number

Just started learning C and I have a problem with the scanf function. Every time I enter a number in the console, it will be printed right under the input. The program still works, but it is a little bit annoying.
(I am using CLion from JetBrains)
int main()
{
int x;
printf("Number: ");
scanf("%d", &x);
printf("Your number is %d!", x);
}
This is the output:
Number:15
15
Your number is 15!
Process finished with exit code 0
It is an issue in clion (Why is CLion printing back inputs from standard input?). Currently unresolved. This problem exist for C and C++.
This bug resides for four years. I definitely advice you to change your compiler if you are not bound this for a particular reason.

fgets() breaks string into 2 lines

I am an absolute beginner to programming and I am starting with the C language. I am currently using the Beginning Programming with C for Dummies book by Dan Gookin.
When doing an exercise with fgets() the following occurred.
This is my code
#include <stdio.h>
int main()
{
char name[10];
printf("Who are you? ");
fgets(name,10,stdin);
printf("Glad to meet you, %s.\n",name);
return(0);
}
The expected result should be a name with a full stop at the end and what is happening is that the full stop carries over to the next line like shown below.
I am using the code blocks IDE on Ubuntu
You typed Miguel and then pressed ENTER. name now holds Miguel\n, in which \n stands for the ENTER you just pressed, since ENTER counts as a character too. If you want to remove it, you'll find the answer here.
You can use strtok(name, "\n")

'C for Dummies' getchar() function not working

I'm currently in the very early stages of learning C programming and am working my way through "Beginning Programming with C for Dummies" using Code::Blocks.
The first activity in Chapter 7, Fetching characters with getchar(), asks us to copy the code exactly as it's presented in the book; see below:
#include <stdio.h>
int main()
{
int c;
printf("I'm waiting for a character: ");
c = getchar();
printf("I waited for the '%c' character.\n", c);
return (0);
}
The output I get is:
I'm waiting for a character:
However according to the book, the output that I should be seeing is the character's ASCII code value. It then asks that I change the %c placeholder to %d to display the value, but still I get the same outcome as before. I could probably recite the code with my eyes closed I've checked it through that may times; I simply cannot see where I'm going wrong.
Am I right in thinking that the getchar() function isn't being recognized? Or that the code isn't being read after the first printf statement? Any guidance is welcome as I don't want to move on until I've understood the problem.
Please enter any key, then 2nd printf will show the result. getchar() is expecting input from user, controller reach at 2nd line & waiting for input.

Resources