I have searched the forum, read the faq, and can't find this anywhere.
I can keep the output window open when I hardwire the values in and then use getchar (), but when I use scanf to interactively input the values then hit enter the window closes. If I've missed this somewhere can someone please post a link
I am using devcpp-4.9.9.2
This is my program:
include <stdio.h>
main()
{
int a, b;
b = a + a;
printf("Input the value for a: \n");
scanf("%lf", &a);
"there are still characters in the que. in this case call fflush with stdin before the getchar and it should work."
Can some tell me what this means? Am I always going to have to use fflush(stdin); with scanf, or is there some other way to get the characters out of the que?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks for all the help. I had thought maybe I should use something other than %lf because the values were integers, but for some reason I thought my professor said %lf was all you could use in scanf. Must of heard him wrong.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have searched the forum, read the faq, and can't find this anywhere.
I can keep the output window open when I hardwire the values in and then use getchar (), but when I use scanf to interactively input the values then hit enter the window closes. If I've missed this somewhere can someone please post a link
I am using devcpp-4.9.9.2
This is my program:
include <stdio.h>
main()
{
int a, b;
b = a + a;
printf("Input the value for a: \n");
scanf("%lf", &a);
getchar ();
return 0;
}
This is my compile log:
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Documents and Settings\Owner\Desktop\Untitled.cpp" -o "C:\Documents and Settings\Owner\Desktop\Untitled.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
Execution terminated
Compilation successful
there are still characters in the que. in this case call fflush with stdin before the getchar and it should work.
snork
I'm sorry, I'm not quite sure what you mean when you say,
"call fflush with stdin before the getchar"
Can you show me an example with this added to my program?
Is stdin a preprocessor instruction?
fflush(stdin)
snork
I can input the value for a, but no matter what value I put in for a, b is always equal to 0.
Am I missing something?
This is my program:
include <stdio.h>
main()
{
int a, b;
/ When I put the value in like this b = 2 /
a = 1;
/* When I put the value in using scanf b = 0.
printf("Input the value for a: \n");
scanf("%lf", &a);
I've double checked and I'm pretty sure I've typed everything in correctly.
*/
b = a + a;
printf("b = %d, \n", b);
fflush(stdin);
getchar ();
return 0;
}
Compile Log:
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Documents and Settings\Owner\Desktop\Untitled4.cpp" -o "C:\Documents and Settings\Owner\Desktop\Untitled4.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
Execution terminated
Compilation successful
Output:
Input the value for a:
1
b = 0,
"there are still characters in the que. in this case call fflush with stdin before the getchar and it should work."
Can some tell me what this means? Am I always going to have to use fflush(stdin); with scanf, or is there some other way to get the characters out of the que?
scanf("%lf", &a); // %i or %d are acceptable for integers
Thanks for all the help. I had thought maybe I should use something other than %lf because the values were integers, but for some reason I thought my professor said %lf was all you could use in scanf. Must of heard him wrong.