--EDIT--
The problem with the third snippet has been resolved and thus removed from the thread! It wasn't the syntax error identified here (but thanks a bunch for catching that, that was kind of embarassing...), so leaving it in here would just confuse readers. However, I still do not know how I would go about removing the newline buffer from scanf without the if statement below (please read my background). Changed portion at the bottom of first post in dark green.
Hello, all!
I have incredibly recently begun my C education, as I am preparing myself for my first computer science class next quarter, and some minor familiarity with C programming is necessary for the class. Given that, my programs and problems are very simple.
My problem is this: scanf seems to cause my programs to skip over certain parts of themselves.
either a) scanf(); will skip a part of itself, or b) scanf(); will skip a getchar(); statement later.
Here is a comparison with two smooth programs, I am having trouble with the third, and an instance of skipping getchar(); appears in the second.
(sorry, indentation removed in posting)
My first program (who would have thought) was Hello, World! As it only uses printf, it does not skip lines. It runs smoothly. The syntax is as follows:
------------------------------------------------------------------------
Code:
#include <stdio.h> /* Includes printf definition */
int
main(void)
{
/* Print Hello, world! */
printf("Hello, world!\n");
getchar(); /*pauses the program so output can be read*/
return(0);
}
-------------------------------------------------------------
The getchar(); statement at the end of the program suspends it so the user can view output. This program is completely functional.
The next program I used calculated the diameter, circumference, and area of a circle, given a radius from the user. The syntax of the program is as follows:
Code:
int
main(void)
{
double RAD, /* input - radius */
DIAM, /* output - diameter */
CIRCUM, /* output - circumference */
AREA; /* output - area */
/* Obtain the radius of the circle */
printf("Please enter the radius of the circle.\n");
scanf("%lf", &RAD);
/* Given the radius, calculate the diameter, circumference, and area, respectively */
DIAM = 2 * RAD;
CIRCUM = 2 * PI * RAD;
AREA = PI * RAD * RAD;
/* Display the diameter, circumference, and radius, respectively */
printf("The diameter of the circle is %f units.\n", DIAM);
printf("The circumference of the circle is %f units.\n", CIRCUM);
printf("The area of the circle is approximately %f units.\n", AREA);
getchar(); /* Delay the program so the user can view the data */
getchar(); /* one getchar did not delay the program, two did */
return(0);
}
------------------------------------------------------
If I used only one getchar();, the program would close immediately. At first I didn't care, but I know this is important and is going to come back to hurt me. Are there other ways to pause a program so a user can view output?
I use Windows XP and am compiling in Dev-C++ 4.9.9.2. My programs execute in the command prompt.
Thanks for your time!