Total Posts:3|Showing Posts:1-3
Jump to topic:

scanf function screws up my C program

F-16_Fighting_Falcon
Posts: 18,324
Add as Friend
Challenge to a Debate
Send a Message
3/11/2013 2:50:01 AM
Posted: 3 years ago
For whatever reason, when I use a scanf function, my program never stops compiling so I have to terminate it manually. This is the most basic step which I isolated. The actual program is a lot longer and myfunction actually is supposed to do stuff but this was my test run and scanf isn't working for whatever reason. Any idea what is wrong with my code?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

void myfunction();

int main() {
int input = 0;

printf("Enter a number:");
scanf ("%d", &number);

if (input == 1) {
myfunction();
}

else {
printf("Invalid Selection.");
}
return 0;
}

void myfunction() {

printf("We are in myfunction");
}
AlwaysMoreThanYou
Posts: 2,900
Add as Friend
Challenge to a Debate
Send a Message
3/11/2013 8:03:42 AM
Posted: 3 years ago
At 3/11/2013 2:50:01 AM, F-16_Fighting_Falcon wrote:
For whatever reason, when I use a scanf function, my program never stops compiling so I have to terminate it manually. This is the most basic step which I isolated. The actual program is a lot longer and myfunction actually is supposed to do stuff but this was my test run and scanf isn't working for whatever reason. Any idea what is wrong with my code?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

void myfunction();

int main() {
int input = 0;

printf("Enter a number:");
scanf ("%d", &number);

Just as a comment, there's a random space between scanf and ("%d", &number);

I would think the problem is that you declare 'input', then proceed to use 'number' in scanf, but never again.


if (input == 1) {
myfunction();
}

else {
printf("Invalid Selection.");
}
return 0;
}


void myfunction() {

printf("We are in myfunction");
}
'When the Spirit of truth comes, he will guide you into all the truth; for he will not speak on his own authority, but whatever he hears he will speak, and he will declare to you the things that are to come.' - John 16:13