Program suddenly stops working.
Hello, I'm fairly new to C, and after taking several examples from the book I decided to write my own. Yet I kept running into several problems with it, so i decided I'd just continue reading, and maybe I'd find the answer there. So I put in another example involving the switch statement, and ran into the same problem.
The problem is, every time I run the program, it will bring up the little mini window that says "___ has stopped working" or something along those lines. But I think I've linked it to having to get a value from a variable, then using that same variable within the parentheses of a printf() function and using it in an arithmetic equation with another variable. The examples of code that I encountered this problem with were the following:
Code:
#include <stdio.h>
int main(void)
{
float f, ff;
char c;
{
printf("Type number, operator, number\n");
scanf("%f %c %f", f, c, f);
switch (c)
{
case '+':
printf(" = %f", f + ff);
case '-':
printf(" = %f", f - ff);
case '*':
printf(" = %f", f * ff);
case '/':
printf(" = %f", f / ff);
default:
printf("Unknown operator");
}
printf("\n\n");
}
return 0;
}
This is the one from the book, only I had to add the heaer file, the void and the int around the main function, and return 0;. This is taken almost exactly from the book, but I used different names for variables, and indented differently. But I already know that doesn't matter.
But the examples that I did narrowing it down to the conclusin that it only happened when it has <stdio.h>, printf, and variable arithmetic within it.
Code:
#include <stdio.h>
main();
{
printf("%d\n" 2 + 2);
return 0;
}
Code:
#include <stdio.h>
main();
{
int i = 2;
printf("%d\n" i + i);
return 0;
}
Code:
#include <stdio.h>
main();
{
int i;
scanf("%d" i);
printf("%d\n" i + i);
return 0;
}
Those three were something similar, I'm not sure what they were exactly, but they were similar to that. I didn't test them, other than the last one, and when I compiled it, it gave me that same error, where it said that the program was not responding, and it needed to close. If it matters, I am using the Digital Mars compiler, the free version, and I'm running it from the command line.
tl;dr: I have a problem where when I run a program that has a scanf(), and a variable within the printf() function's parentheses that I use with another variable in an arithmetic equation, it gives me the small window saying that the program is not responding andneeds to close. I am using the Digital Mars C compiler, and I'm running the free version from the Command Line. Any advice as to how I can fix this?