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 &
Related
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.
I am attempting to create a text file using C that will contain a table of values in Fahrenheit and their Celsius conversion.
I am able to use fprintf properly outside of the for loop but when I put it inside it does not print anything to the file. The code compiles properly but when I try to execute it completes but with exit code "-1073741819"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
FILE *filePointerThree;
double myArray[100];
filePointerThree = fopen("myFileFive.txt", "w");
for(int i=0; i<=100; i++)
{
myArray[i] = (i-32)/1.8;
}
for(int j=0; j<=100; j+=5)
{
fprintf(filePointerThree, "%d degrees F \t %5.2lf degrees C\n", j, myArray[j]);
}
fclose(filePointerThree);
}
Your array needs to be larger to hold 101 values (0 through 100):
double myArray[101];
Upon further review, the code can be simplified to not require an array, as follows. A return 0; at the end of main() will ensure an exit code of 0. Minor: the math.h and stdlib.h includes are not required as fopen() and friends are defined in stdio.h.
#include <stdio.h>
int main()
{
FILE *filePointerThree;
filePointerThree = fopen("myFileFive.txt", "w");
for(int j=0; j<=100; j+=5)
{
fprintf(filePointerThree, "%d degrees F \t %5.2lf degrees C\n", j, (j-32)/1.8);
}
fclose(filePointerThree);
return 0;
}
While printing a number, I am trying to print its sign before the number. Is there a way to do it without the actually if...else case mentioned in the comment section of the code below.
I have tried getting the sign of the number. But I don't know how to print just the sign.
#include<stdio.h>
#include<complex.h>
void main(){
double complex s = 3.14 + 5.14*I;
printf("\ns is: %f + %f i", creal(s), cimag(s));
double complex S = conj(s);
printf("\nConjugate of s is: %f + %f i", creal(S), cimag(S));
}
/*
printf("\nConjugate of s is: %f ", creal(S))
if cimag(S) > 0
printf("+ %f i", cimag(S))
else
printf("- %f i", abs(cimag(S)))
*/
If S = 3.14 - 5.14*I, without the if...else condition, I'm expecting to get an output something like this:
3.14 - 5.14 i
You can just use the printf sign flag. +
#include <stdio.h>
int main()
{
float f = 1.0;
printf("%f%+f",f,f);
return 0;
}
Output
1.000000+1.000000
Change to -1:
-1.000000-1.000000
If you really need the spaces, you're going to have to do something like you described:
#include <stdio.h>
#include <math.h>
#include <complex.h>
void complexToString(double complex num, char * buffer){
double imag = cimag(num);
char sign = (imag<0) ? '-':'+';
sprintf(buffer,"%f %c %f i",creal(num),sign,fabs(imag));
}
int main()
{
double complex s = 3.14 + 5.14*I;
char buffer[50];
complexToString(s,buffer);
printf("%s",buffer);
return 0;
}
output:
3.140000 + 5.142000 i
First get the sign character:
double x = ...;
char c = signbit(x) ? '-' : '+';
Then use it however you want:
printf ("%c %f", c, fabs(x));
With the help of answers from #Antoine and #yhyrcanus, the simplest way to code for space is:
double complex s = 3.14 - 5.14*I;
printf("\ns is: %f %c %f i", creal(s), signbit(cimag(s)) ? '-' : '+',cabs(cimag(s)));
i want to convert from binary to decimal but convert function does not return any thing where is the problem ??
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int convert(int x); //fun to convert from binary to decimal
int main()
{
int x; //binary number
printf("plz enter binary number !\n\nbinary: ");
scanf("%d",&x);
printf("\ndecimal: ",convert(x));
return 0;
}
int convert(int x)
{
int sum=0; //decimal number
int i=0;
int r; //remainder
while(x!=0){
r=x%10;
sum+=r*pow(2,i);
x=x/10;
i++;
}
return sum;
}
You can also try
scanf("%i", &x);
printf("%d", x);
you should change your printf in main().
printf("\ndecimal: %d",convert(x));// add %d
Because to print a value by printf need to spacify the data type such as for int %d for character %c.
binary numbers are series of ones and zeros (0|1), if you are referring to this type of numbers then check this source code below. you may also find this link useful http://www.math.grin.edu/~rebelsky/Courses/152/97F/Readings/student-binary
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int bintodec(char bin[32]){
int dec=0;
while(*bin){
if((*bin!='0') && *bin!='1')
break;// only accept '0' & '1'
dec<<=1;
dec+=*bin-'0';
bin++;
}
return dec;
}
int main(){
char bin[32]=""; //i believe binary numbers are series of '1' and '0'
printf("plz enter binary number !\n\nbinary: ");
scanf("%s",bin);
printf("\ndecimal: %d\n",bintodec(bin));
return 0;
}
Use format specifier:
%d for integer (in this case)
%c for character
%f for float value
%s for string
%lf for double
%x for hexadecimal
In this case change print statement to:
printf("\ndecimal: %d",convert(x));
**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.