An extra input appears - c

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.

Related

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.

c - char variable proceeds without taking input

I have written a program to take input of two numbers and either add or subtract the numbers depending the operation specified.
Here is my code :
#include <stdio.h>
#include <stdlib.h>
int main()
{
float i, j, k;
char a;
printf("This is a program to add or subs two number.\n");
printf("Enter the first number : ");
scanf("%f", &i);
printf("Enter the second number : ");
scanf("%f", &j);
printf("Give your choice(+ or -): ");
scanf("%c", &a);
switch(a){
case '+' :
k = i + j;
printf("Sum = %f\n", k);
break;
case '-' :
k = i - j;
printf("Difference = %f\n", k);
break;
default:
printf("Cannot do this operation\n");
}
return 0;
}
This program takes input for the two numbers but skips input for operation and runs the default case. Please help!
(I am using gcc compiler).
The %c conversion specifier won't automatically skip any leading whitespace, so if there's a stray newline in the input stream (from a previous entry, for example) the scanf call will consume it immediately.
One way around the problem is to put a blank space before the conversion specifier in the format string:
scanf(" %c", &a);
The blank in the format string tells scanf to skip leading whitespace, and the first non-whitespace character will be read with the %c conversion specifier.
-Aditya

Not getting the expected result from function call return value

I'm doing one of the beginner C-programming exercises, building a simple calculator that shows the menu, takes in the user's choice of operation, takes in 2 operands and shows the result. Here's what I have so far:
#include<stdio.h>
int main(){
int add (int a,int b) {return a+b;}
int substract (int a,int b) {return a-b;}
int multiply (int a,int b) {return a*b;}
double divide (int a,int b) {return a/b;}
int modulo (int a,int b) {return a%b;}
int num1, num2, choice;
printf ("======MENU=======\n");
printf ("1. Add\n");
printf ("2. Substract\n");
printf ("3. Multiply\n");
printf ("4. Divide\n");
printf ("5. Modulo\n");
printf ("Please, select your operation and press enter:\n");
scanf ("%d", &choice);
switch (choice){
case 1:
printf ("Please, enter the two operands separated by space and press enter:\n");
scanf ("%d %d", &num1, &num2);
add (num1, num2);
printf ("%d\n", add);
break;
case 2:
printf ("Please, enter the two operands separated by space and press enter:\n");
scanf ("%d %d", &num1, &num2);
substract (num1, num2);
printf ("%d\n", substract);
break;
case 3:
printf ("Please, enter the two operands separated by space and press enter:\n");
scanf ("%d %d", &num1, &num2);
multiply (num1, num2);
printf ("%d\n", multiply);
break;
case 4:
printf ("Please, enter the two operands separated by space and press enter:\n");
scanf ("%d %d", &num1, &num2);
divide (num1, num2);
printf ("%lf\n", divide);
break;
case 5:
printf ("Please, enter the two operands separated by space and press enter:\n");
scanf ("%d %d", &num1, &num2);
modulo (num1, num2);
printf ("%d\n", modulo);
default:
printf ("Invalid entry\n");
break;
};
scanf ("%d");
return 0;
}
My issues with this code are:
The program doesn't return the correct values. The addition option shows numbers over 4000000. Other operations do the same.
The program should scan for an integer to be entered once the operation is complete and then end. However, it crashes once the integer has been entered.
TL;DR -- printf ("%d\n", add); does not print the returned value of the previous add() function call, it tries to print the function address itself. Now, in case of using wrong argument to a format specifier, undefined behavior is the end product.
To elaborate, the main problem is the approach. As you have written
add (num1, num2);
printf ("%d\n", add);
this will not add num1 and num2 and print the result of the addition magically. You need to either
collect the return value of the function in a variable and print that variable later.
int res = -1;
res = add (num1, num2);
printf ("%d\n", res);
Otherwise, you have to call the add() function itself as the argument to the format specifier in the format string in printf(), like
printf ("%d\n", add (num1, num2););
Same goes for the other function calls in all the cases, too.
Also, as mentioned by Mr. Bathseba in the previous comment, though you're using the return type as double in your division function, the actual division involves the operands of type int, which makes the division to be performed as integer division, then the result will get promoted to double, which probably you don't want. You have to enforce the floating point division.
And finally, regarding usage of nested function, it's a bad idea as this is not standard C. Please see this answer for more info.
You are printing function addresses:
modulo (num1, num2);
printf ("%d\n", modulo);
You must replace by:
int var = modulo(num1, num2);
printf ("%d\n", var);

c program ignores all scanfs after wrong variable type input

#include <stdio.h>
#include <stdlib.h>
int main(void) {
int a, b, checka, checkb;
printf ("enter a: ");
checka = scanf ("%d", &a);
printf ("enter b: ");
checkb = scanf ("%d", &b);
printf ("checka = %d\n", checka);
printf ("checkb = %d", checkb);
return EXIT_SUCCESS;
}
I was having this problem in a larger program but I wrote a quick test to see if I could fix it, which I can't.
Basically when anything other than an integer is entered for a scanf, the program just instantly skips and ignores every other scanf and just prints the rest of the program it sees, meaning I can't make checks with a while loop, or I just get an infinite loop as the scanf in the loop to fix the variable just gets skipped.
Obviously if integers are entered this particular program will just return 1 for the last two printfs, which is expected.
What am I doing wrong?
Thanks!
There is a reason why we should check scanf for errors, try this:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int a, b, checka, checkb;
printf ("enter a: ");
if((scanf ("%d", &a)) == 1){
checka = a;
}else{
printf("Error(1)!");
exit(EXIT_FAILURE);
}
printf ("enter b: ");
if((scanf ("%d", &b)) == 1){
checkb = b;
}else{
printf("Error(2)!");
exit(EXIT_FAILURE);
}
printf ("checka = %d\n", checka);
printf ("checkb = %d", checkb);
return EXIT_SUCCESS;
}

3rd function won't run scanf

I have a simple program which receives input from 3 different functions, 2 return ints, 1 returns a char, but the third function doesn't scanf for some reason it skips that step entirely.
#include <stdio.h>
int get_height();
int get_length();
char get_symbol();
void draw_rectangle(int h, int l, char s);
int main () {
int h, l;
char s;
h = get_height();
l = get_length();
s = get_symbol();
draw_rectangle(h, l, s);
return 0;
}
int get_height() {
int i;
printf ("Please enter the height of the rectangle: ");
scanf ("%d", &i);
return i;
}
int get_length() {
int i;
printf ("Please enter the length of the rectangle: ");
scanf ("%d", &i);
return i;
}
char get_symbol() {
char i;
printf ("Please enter the symbol for the rectangle: ");
scanf ("%c", &i);
return i;
}
void draw_rectangle(int h, int l, char s) {
printf ("%d %d %c", h, l, s);
}
When I run this, i can scan for height and length but it prints the prompt to scan for the char but then skips the user input and prints the value for h and l but no s. What am i missing here?
Previous scanf leaves a newline in the input buffer which is consumed by:
scanf ("%c", &i);
Just change it to:
scanf (" %c", &i);
Notice the space. This will tell scanf to ignore all whitespaces.
You need to consume the newline characters that are being left by the previous scanf() calls. A simple way would be:
scanf ("%d", &i);
getchar();
in each function where it's used. Or with just the one call:
scanf(" %c", &i);
the space will tell it to skip the previous tabs, newlines, or spaces.
stdin is buffered and when the user enters a digit you get a newline char too. The scanf taking the character is therefore picking up one of those left over newlines
You can also use
fflush(stdin);
scanf("%c",&i);
It will resolve your problem. As fflush clears the buffer before reading the character

Resources