scanf prevents program from running - c

So I wrote this program using coderunner,
#include <stdio.h>
int main()
{
int num1, num2;
scanf("%d%d", &num1, &num2);
if (num1 > num2)
printf("The min is:%d\n ", num2);
else
printf("The min is:%d\n ", num1);
return 0;
}
The problem is that the program wont run. It keeps showing this and then it stops after a while:
Removing the scanf fixed the issue, I've tried other programs using scanf and it was fine. Any ideas?

How do you expect scanf() to interpret e.g. 123 or 1232 as two integers? Chances are all digits you enter are "eaten" by the first %d, and then scanf() waits for more for the second.
You must use some separation, or some non-numeric character between them:
scanf("%d/%d", &num1, &num2);
This tells scanf() to expect a slash between the two numbers. You could just use whitespace (without any in the format string, as pointed out in comments) too of course.
Also, you should check the return value before relying on the numbers:
if(scanf("%d %d", &num1, &num2) == 2)
{
}

Related

When I add two integers it says invalid although supposedly it's for characters and symbols. Am I missing something?

In my calculator I tried firstly to make one operation functioning to have integers be displayed properly and when someone inputted a character it would say invalid.
When I input two integers it say's invalid. Not the actual sum of it.
#include <stdio.h>
#include <conio.h>
int main(){
char op;
int num1, num2;
int result;
printf("Enter (+, -, /, *): ");
scanf("%c", &op);
printf("Enter Two Integers: \n");
scanf("%d %d", &num1, &num2);
switch (op){
case '+':
result = num1+num2;
if(!(num1 == '+' && num2 == '+')){
printf("Invalid");
}
else{
printf("Sum: %d ", result);
}
break;
case '-':
result = num1-num2;
printf("Difference: %d ", result);
break;
case '/':
result = num1/num2;
printf("Quotient: %d ", result);
break;
case '*':
result = num1*num2;
printf("Product: %d ", result);
break;
default:
break;
}
getch();
return 0;
}
I expected that with that new line of condition it will make characters and symbols print "Invalid"
if(!(num1 == '+' && num2 == '+'))
This doesn't make any sense for several reasons. First of all De Morgan's laws and boolean algebra is often considered a prerequisite before studying any form of programming. By applying De Morgan/common sense, then we can tell that the opposite to "if num1 is + and num2 is +" is "if num1 isn't + OR num2 isn't +". That is:
if(num1 != '+' || num2 != '+') or if you will if(!(num1 == '+' || num2 == '+')).
With that logic flaw out of the way, this is the wrong solution to the the actual problem anyway. You simply want to prevent the user from entering a character - any character not just '+' - when expecting a number. The easiest way of doing that is to check the result of scanf - it returns a number corresponding to the number of arguments successfully read. For example:
int result;
do
{
result = scanf("%d %d", &num1, &num2);
if(result != 2)
{
printf("You must enter two numbers!\n");
}
}
while(result != 2);
Another option is to read the input as a string with fgets and then parse that string afterwards.
As a side note, please note that <conio.h> has been obsolete for well over 20 years and if someone taught you to use it, you need a more updated source of learning. The future for freshly graduated MS DOS programmers isn't very promising...
Be sure to check the return value of scanf to ensure it read the number of values you expected.
num1 and num2 are int variables and you are reading into them using the %d specifier. If you type in + or other symbols, scanf("%d %d", &num1, &num2) will return 0. A simple demonstration:
$ cat > testing.c
#include <stdio.h>
int main(void) {
int op;
int result = scanf("%d", &op);
printf("%d\n", result);
}
$ gcc testing.c
$ ./a.out
+
0
$ ./a.out
67
1
The odds that num1 and num2 uninitialized both contain the value '+' are not impossible but highly unlikely, so the most likely outcome is that "Invalid." is printed.
As noted in comments, 43 is the ASCII code for '+' so if these specific numbers are input, both num1 and num2 will test as equal to 43 and their sum (86) will be printed.

problem creating a code to print maximum between 3 numbers

I wrote a program which takes three numbers from the user and prints out the maximum number, but when I run the program it is not taking the numbers from the user correctly
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1, num2, num3;
printf ("PROGRAM TO FIND THE BIGGEST NUMBER\n");
printf ("\n");
printf ("enter first number : ");
scanf ("%d ",&num1);
printf ("enter second number : ");
scanf ("%d ",&num2);
printf ("enter third number : ");
scanf ("%d ",&num3);
printf("%d - %d - %d \n",num1,num2,num3);
if (num1>num2 && num1>num3){
printf ("the biggest number is %d",num1);
}
else if (num2>num1 && num2>num3){
printf ("the biggest number is %d",num2);
}
else if (num3>num1 && num3>num2){
printf ("the biggest number is %d",num3);
}
return 0;
}
Remove the space after the %d in all the scanf calls. The space will match any character including the newline. The scanf will keep reading until the match fails. By removing the space the match fails when the newline character is entered and the code continues.

How to read floating-point numbers by pairs in C?

Write a program that requests two floating-point numbers and prints the value of their difference divided by their product. Have the program loop through pairs of input values until the user enters nonnumeric input.
Use a function to return the value of the calculation.
I've successfully completed this exercise without using function but can't get it right using function. The program itself runs but doesn't return any value in fact it crashes.
Please any help would be appreciated.
Here is my program:
#include <stdio.h>
#include <string.h>
double result (double x, double y);
int main(void)
{
double num1, num2, res;
printf("This while calculate difference of two numbers by their product.\n");
printf("Enter first number followed by second number\n");
while (scanf("%lf %lf", &num1, &num2 ==2))
{
res= result(num1, num2);
printf("the result is equal to %.3g\n", res);
printf("Enter next set of numbers or q to quit\n");
}
return 0;
}
double result(double x, double y)
{
double output;
output = (y-x)/(x*y);
return output;
}
while (scanf("%lf %lf", &num1, &num2 ==2))
was meant to be:
while (scanf("%lf %lf", &num1, &num2) ==2)
Try changing
while (scanf("%lf %lf", &num1, &num2 ==2))
to
while (scanf("%lf %lf", &num1, &num2) ==2)

An extra input appears

For example when I input 2 for num1 and 3 for num2, I expect to get 8 for the output as soon as I enter the second number. However, the program expects me to input one more integer, and I just input a random number like 242 and it still outputs 8, which means that it does not affect the result. So my question is why is there the third input?
Thank you for your help!
#include "stdafx.h"
int Power (int num1, int num2);
int main ()
{
int a, b;
puts ("Enter two numbers, a and b:\n");
scanf ("%i\n", &a);
scanf ("%i\n", &b);
printf ("%i\n", Power(a, b));
return 0;
}
int Power (int num1, int num2)
{
int sum=1;
for (int i=1; i<=num2; i++){
sum= sum*num1;
}
return sum;
}
Get rid of the newlines: \n, in your scanf format strings, or just use a single scanf, e.g.:
scanf("%i%i", &a, &b);
Or:
scanf ("%i", &a);
scanf ("%i", &b);
Your scanf() doesn't need the "\n".
scanf ("%i", &a);
scanf ("%i", &b);
You should remove the '\n' from your format string in your calls to scanf.

Switch small program does not let me see result

I am learning the switch statement of C. This is my small program and it runs and does the calculation but doesn't let me see the result of the operation. The black window shows up so that I input the numbers and the operator and then for a fraction of a second shows the result and disappears. Any help is appreciated.
#include <stdio.h>
int main(int argc, char *argv[])
{
int num1, num2, ans=0;
char ch, name;
printf("Enter a value: ");
scanf("%d", &num1);
printf("Enter a second value: ");
scanf("%d", &num2);
printf("Input * To multiply\
+ To add\
- To subtract: ");
scanf(" %c", &ch);
switch(ch)
{
case'*':
ans=num1 * num2;
printf("%d times %i equals: %i",num1,num2,ans);
break;
case'+':
ans=num1+num2;
printf("%i plus %i equals: %d",num1,num2,ans);
break;
case'-':
ans=num1-num2;
printf("%d minus %d equals: %d",num1,num2,ans);
break;
default:
printf("Range numbers");
}
getchar();
return ch;
}
Probably due to output buffering. Add newlines (\n) last in your formatting strings.
As a newbie, you should end all your printf format string with an escaped newline \n, i.e. printf("%i plus %i equals %d\n", num1, num2, ans); (or you should call fflush(stdout); just after the end of the switch before the getch and before all your scanf).

Resources