Why scanf not working for double and float? - c

CODE 1 (above)
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int a;
long long int b;
char c;
float d;
double e;
scanf("%i %lli %ch %f %d", &a, &b , &c, &d, &e);
printf("%i\n%lli\n%ch\n%f\n%d", a,b,c,d,e);
return 0;
}
CODE 2 (BELOW)
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
// Complete the code.
int a;
long long int b;
char c;
float d;
double e;
scanf("%d %lld %c %f %lf",&a,&b,&c,&d,&e);
printf("%d\n%lld\n%c\n%f\n%lf",a,b,c,d,e);
return 0;
}
why the code(top one) is not asking for input afterward float (it is not asking for the values of float and double, while the code below is correct and taking all inputs

The format string for a char is %c, in the top code you put %ch which is incorrect, causing the scanf function to return after this.

Related

Change two variables with each other using functions and pointers

I have to change the values of two variables with each other using the following function:
I made this code
#include <stdio.h>
#include <stdlib.h>
void change_double(double *d1, double *d2);
int main()
{
double *a, *b;
printf("Write two variables of the type double.\n");
scanf("%d", &a);
scanf("%d", &b);
printf("Normal variables: %d %d\n", a, b);
change_double(&a, &b);
printf("Changed variables: %d %d\n", a, b);
return 0;
}
void change_double(double *d1, double *d2)
{
double aux;
aux=*d1;
*d1=*d2;
*d2=aux;
}
When I run this programm, I generally always get this result(example):
Normal variables: 321 123
Changed Variables: 82 321
Number 82 appears for some reason I don't know.
Thank you
Changed Code
#include <stdio.h>
#include <stdlib.h>
void swap_double(double *d1, double *d2);
int main()
{
double a, b;
printf("Write two variables of the type double.\n");
scanf("%lf", &a);
scanf("%lf", &b);
printf("Normal variables: %lf %lf\n", a, b);
swap_double(&a, &b);
printf("Changed variables: %lf %lf\n", a, b);
return 0;
}
void swap_double(double *d1, double *d2)
{
double aux;
aux=*d1;
*d1=*d2;
*d2=aux;
}

How to get pass by reference to work in a function properly in C

The function with the pass by value works like it ought to, I believe, as it prints out the value 0 whenever I enter two numbers. However the adderRef (pass by reference function) doesn't work. All it prints out is "the pass by reference of c is" and that's it. There is no value or anything. I just wanted to inquire whether there was something wrong with my syntax or something....
Okay guys sorry about the question being vague and for my errors. It's my first time asking on stackoverflow and I should have been more mindful. I'm aware of my error and why I made it. I got muddled in class when my teacher was altering the code a bit and I copied it down incorrectly/ Thanks everyone for your help. Yes i was indeed quite dumb .
#include <stdio.h>
#include <stdlib.h>
void adderval(int a, int b);
void adderRef(int *, int *);
int main()
{
int a, b, c = 0;
printf("enter two numbers.\n");
scanf("%d %d", &a, &b);
adderval(a, b);
printf("the pass by value of c is %d \n", c);
adderRef(&a, &b);
printf("the pass by reference of c is \n", c );
return 0;
}
void adderRef(int *a, int *b )
{
int c;
c = *a + *b;
}
void adderval(int a, int b )
{
int c;
c = a + b;
}
corrected as below.
#include <stdio.h>
#include <stdlib.h>
void adderval(int a,int b,int c);
void adderRef(int a,int b,int *c );
int main()
{
int a,b,c=0;
printf("enter two numbers.\n");
scanf("%d %d",&a,&b);
adderval(a,b,c);
printf("the pass by value of c is %d \n",c);
adderRef(a,b,&c);
printf("the pass by reference of c is %d \n",c );
return 0;
}
void adderRef (int a, int b, int *c )
{
*c = a + b;
}
void adderval (int a , int b,int c )
{
c=a+b;
}

C language: scanf function producing different results with float and double types?

**Code A returns the correct conversion: 6.55957.**
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float convert(float currencyA)
{
float currencyB = 0;
currencyB = 6.55957 * currencyA;
return currencyB;
}
int main(int argc, const char *argv[])
{
float amount = 0;
printf("How much\n");
scanf("%f", &amount);
printf("You get %f in currencyB", convert(amount));
return 0;
}
**Code B returns an incorrect conversion: 0.051247.**
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double convert(double currencyA)
{
double currencyB = 0;
currencyB = 6.55957 * currencyA;
return currencyB;
}
int main(int argc, const char *argv[])
{
double amount = 0;
printf("How much\n");
scanf("%f", &amount);
printf("You get %f in currencyB", convert(amount));
return 0;
}
If I remove printf and scanf, and assign 1 as the value to the "amount" variable the result is correct.
I suspect scanf is causing the error. If so, why would that be?
Thank you for reading and feel free to ask for any additional info you require.
The correct format specifier for double is %lf, not %f.

Weird results using simple math operations

i have the following struct:
typedef struct number
{
int x;
int y;
int z;
unsigned long int final;
}number;
my code is the following:
number* numbers;
numbers= (number*)malloc(sizeof(number));
scanf("%d %d %d", &numbers->x, &numbers->y, &numbers->z);
printf("input: %d,%d,%d\n",numbers->x, &numbers->y, &numbers->z);
numbers->final=(numbers->x)*4000 + (numbers->y)*50 + (numbers->z);
printf("final: %d",numbers->final);
but the output is wrong. for example here is a run:
12 12 12
input: 12,12,12
final: -28640
i cant figure out the problem. the highest number that number->final can get is 90,000 (i make sure of it as i gives the input)... i seems like there is overlap? please help.
Your problem is the pointer. I am assuming you initialised the struct as follows.
numbers *numbers;
However if you use it in the main where you declare it don't use a pointer. There are also a few errors in your printf call, you are printing the memory address of y and z instead of the value like you did for the x value.
Use something like this.
#include <stdio.h>
typedef struct number
{
int x;
int y;
int z;
unsigned int final;
} number;
int main()
{
number numbers;
scanf("%d %d %d", &numbers.x, &numbers.y, &numbers.z);
printf("input: %d,%d,%d\n",numbers.x, numbers.y, numbers.z);
numbers.final=(numbers.x)*4000 + (numbers.y)*50 + (numbers.z);
printf("final: %d\n",numbers.final);
return 0;
}
Right and if you used malloc it looks like this.
#include <stdio.h>
#include <stdlib.h>
typedef struct number
{
int x;
int y;
int z;
unsigned int final;
} number;
int main()
{
number *numbers = malloc(1 * sizeof(number));
scanf("%d %d %d", &numbers->x, &numbers->y, &numbers->z);
printf("input: %d,%d,%d\n",numbers->x, numbers->y, numbers->z);
numbers->final=(numbers->x)*4000 + (numbers->y)*50 + (numbers->z);
printf("final: %d\n",numbers->final);
free(numbers);
return 0;
}
Running example here
The reason for your wrong answer is because you have kept the datatype as int which has max value of 32767 change it to unsigned long int as your ans calculates to 2400612

C programming: reading from file

I have customer data such as customer no, the coordinates of location etc. And there are 25 customers in the text file.
Here's my code. This gives me an output of zeros when I print it.
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define customerCount 25
struct customerData
{
int customerNo;
double xCoordinate;
double yCoordinate;
double demand;
double readyTime;
double dueTime;
double serviceTime;
};
int main()
{
int i;
struct customerData allSubscriber[customerCount];
FILE *dosya;
dosya = fopen("c:\\solomon_c101.txt", "r");
for(i=1; i<=customerCount; i++)
{
fscanf(dosya, "%d %f %f %f %f %f %f", &allSubscriber[i].customerNo, &allSubscriber[i].xCoordinate, &allSubscriber[i].yCoordinate, &allSubscriber[i].demand, &allSubscriber[i].readyTime, &allSubscriber[i].dueTime, &allSubscriber[i].serviceTime);
}
fclose(dosya);
for(i=1; i<=customerCount; i++)
{
printf("%f\n", &allSubscriber[i].xCoordinate);
}
getch();
return 0;
}
Use this:
printf("%f\n", allSubscriber[i].xCoordinate);
instead of
printf("%f\n", &allSubscriber[i].xCoordinate);
As another user pointed out,use %lf as the format specifier for double in fscanf().In printf() it gets promoted to double.
Don't use conio.h and getch().Those are not standard C.
use the %lf to read a double in scanf
printf("%f\n", &allSubscriber[i].xCoordinate); //remove &

Resources