C/C++ :: Dividing Number By Float Integer?
May 5, 2014
I need to read a float number and show the rest of his division by an integer, but i'm having the following error message:
Quote
error: invalid operands of types 'float' and 'int' to binary 'operator%'
View 8 Replies
ADVERTISEMENT
Dec 2, 2014
So I have a simple calculator that does a few operations (+ - * / %) Pretty basic stuff
I declared int x, y for the numbers, char operation, and float result.
the code is based on switch(operation)
The program is running alright, but when I divide 8/7 it returns 1 as the result, I tried changing the x and y to float but that won't work because of the case '%'
I also tried making local float variables in the case'/' but it won't compile "E2126 Case bypasses initialization of a local variable"
How can I make the division work and return a float value?
View 3 Replies
View Related
Feb 28, 2014
this is my code
#include<stdio.h>
main()
{
[Code]....
N=1st number M=2nd number(accrding to my prof we will name it N and M
ctr= increment of factorial of N, ctr2= increment of factorial of (N-M)
the problem is when i got the factorial of N / factorial of (N-M) i need to get the last non zero digit. so i use mod if it has zero in it. but mod can be only used with an int value. and when i change it to int value, the value of fact1 which is a float change
View 14 Replies
View Related
Mar 10, 2013
I have a float values I'd like to round off to the nearest integer value.
That is to say, if the float value is 44.234533, the integer value should be 44. If the float value is 44.682101, the integer value should be 45.
How do I do this?
View 4 Replies
View Related
Oct 14, 2013
why does the following code output "0.000000" instead of "1.000000"?
Code:
#include <stdio.h>
int main(void) {
int x=3, y=2;
printf("3/2 is %f
", x/y);
return 0;
}
the code was compiled and run using gcc 4.4.7 and glibc 2.17 on linux kernal 2.6.32 running on a PC with an intel i5-2500k cpu(sandy bridge)
View 8 Replies
View Related
Mar 20, 2014
i am writing a program that requires me to write am input a float and an integer and save it to a txt file. When i try to compile my code i get an error "assignment from incompatible data type". ?
int *intconstant;
float *floatconstant;
int *value;
struct FILE *infileptr, *outfileptr;
infileptr = fopen("/home/brinkmann.brendon/assign14data.txt", "r");
outfileptr = fopen("/home/brinkmann.brendon/assign14report.txt", "w");
[Code]...
View 14 Replies
View Related
Nov 28, 2012
I'm reading lines from a text file in C++ which contains integer + string + float number(like 3,67 with comma) + string in this order. I need the float number to sort the lines but I couldn't manage to separate the data into the types I can use so far. I tried different kind of functions and the best I could do was such a code;
void main (){
ifstream records;
records.open("records.txt");
int id;
string line;
char name[100];
float gpa;
[Code] ....
This fails at reading the floating number which has comma in it and then last string is read as string starting with the comma and rest of the number. An output example is:
698 John 3 ,67
It doesn't read last string on the line as well. I understand that part but simply I need another read but what I want exactly is to separate one line using "tab" as a seperator into proper data types and then using the numbers as integers, and the grades as floating numbers. How Can I do this?
View 5 Replies
View Related
Nov 24, 2013
How to reserved int number and print as float number ?
View 1 Replies
View Related
Mar 6, 2015
I am entering numbers to float ... I want program to find out, which first number is not from specific interval. How to do it ? Example: Enter input : 5 10 20 30 50 46 . 30 is invalid. Here is the code :
Code:
while(scanf("%f",&input)!=EOF || input==0) {
sum=input+sum;
if (getchar() == '
[Code]....
View 3 Replies
View Related
Aug 25, 2013
how can I check if the number is float point number without converting the number to string and then find '.'?
For example, this number (5.0) should not be integer. I found the following way in Python but it didn't work in C++
abs(n - (int)n) < 0.000001
View 9 Replies
View Related
May 20, 2014
I'm doing some exercises in c and i have to do one which shows me this output:
Quote
I=0 J=1
I=0 J=2
I=0 J=3
I=0.2 J=1.2
I=0.2 J=2.2
I=0.2 J=3.2
.....
I=2 J=?
I=2 J=?
I=2 J=?
View 2 Replies
View Related
Aug 12, 2013
I want to convert a float number like 2.3 to 0002.3.Is therany inbuilt function in C/C++ for this.
View 4 Replies
View Related
Aug 19, 2014
How do you convert a number float in a range of -10.0f to 17.0f to a eqivalent number in the range of 0.0f to 1.0f?The code does not work well. floaty is the float to change.
//change range to 0..1
diamond[x][y] = (floaty - minY) / (maxY - minY);
View 19 Replies
View Related
Apr 30, 2012
I have a program that runs fine but outputs garbage and skips processes when I input a decimal. It compiles fine and has no errors.
Code:
#include <iostream>
#include <float.h>
using namespace std;
int main() {
int x;
int y;
[Code] ....
View 4 Replies
View Related
Dec 16, 2013
What would be the best way to remove the decimal point from a float number? For instance if I have a float number .2546 I would like to be able to remove the dot and use just the number (int) 2546 for some calculations.
Maybe, convert to string than remove the first character from the string than convert the string back to an int?
View 2 Replies
View Related
Dec 4, 2014
I am unable to find why my code is going into infinite loop as below. This works perfectly fine if I keep entering just the integer values but if I enter a float number it ends up in an infinite loop
int main() {
int x;
while(1){
cin>>x;
cout <<x;
}
return 0;
}
View 3 Replies
View Related
Mar 21, 2015
So, I'm supposed to do : Create a function with unlimited number of arguments, which forms a dynamic string based on the following form (%d, %s, %f, %lf, %c), with the following prototype:
char*create(char*form, ...);
The function is supposed to have the following output:
create("Peter is %d years old and is in %s-%c class.",7,"second",'A');
-> Peter is 7 years old and is in 7-A class.
create("His GPA is %lf.",4.96);
-> His GPA is 4.96.
create("His favourite subject is math!");
-> His favourite subject is math!
I've managed to do the following :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
char *create(char *form, ...) {
char *res =(char*)calloc(1,1),*pos_int,*pos_float,*pos_str,pos_char,*pos_long;
[Code] ....
The part with %d and %s string was not that hard, but now I'm supposed to convert %f and %lf to string, I've tried using sprintf but I've had no luck so far, another problem is the fact that I've gotta use lists to complete the task. I've been trying to convert float to string for the past 2 hours, but I'm drawing a blank now.
View 4 Replies
View Related
Oct 9, 2013
I was told to use a round function to round a number to give an integer number that is closer to the real value. (for example if the number is 114.67 I need to print an int value of 115 instead of 114)
I am not exactly sure how a round function works, but I am told to include math.h library. What I try doesn't seem to work.
View 1 Replies
View Related
May 14, 2014
i am relatively new to C programming so i run into problems on daily basis. But this time i have something i just cant figuer out and i was hoping you could point me towards the right track. I am trying to divide two integers.DevValue by KpTotal. for some reason my micro controller allways crashes.Y is a variable of a distance measuring sensor. i have a 4x4 keypad to enter a three digit number (e.i 123) so Kp1 = 1 Kp2 = 2 Kp3 =3.
Code:
int kp1, kp2, kp3, kpTotal = 0;
char txt[6] = ""
int keypadPort at PORTD;
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
}
[code]....
i think it has something to do with the format of the value. i read that the micro controller crash when dividing by zero.
View 1 Replies
View Related
Apr 4, 2015
I wrote the following code to divide 100 customers into three clusters but it kept on hanging during execution. I used while loop. Attached is the text file to use. and the code below :
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <cstdlib>
#include <ctime>
#include <stdio.h>
#include <math.h>
[Code] ....
Attached File(s)
c201.txt (7.13K)
View 1 Replies
View Related
Jun 21, 2013
i made a calculator that you have to give terms and operators one by one but now i want to improve it. the user now should be able to type something like 5*78+325/sin(3*pi)-1 and the program should be able to calculate it. but i don't know how to use the signs '*','+','-' and '/' as delimiters and turn the input string into a string array. i mean the string "5*78+325/sin(3*pi)-1" will become a string array like "5,*,78,+,325,/,sin(3*pi),-,1,"
View 2 Replies
View Related
Mar 2, 2013
So i have this -
time_t t4 = time(NULL);
//and then some stuff
time_t t5 = time(NULL);
iElapsedYah = t5-t4;
fElapsedYah2 = iElapsedYah/iNumTimes;
printf("It took %f seconds", fElapsedYah2);
but it always prints 0.0000000, and i dont know why even though individually
iElapsedYah = not zero and iNumTimes = not zero
I am using windows 7 and m complier is Dev C++
View 3 Replies
View Related
Nov 20, 2013
I can do the folowing:
float var1 ;
var1 = 9.12345 ;
printf("%.2f",var1) ;
the output will be 9.12. What if I wanted to save that as another separate float with displaying it on screen?
View 1 Replies
View Related
Jun 8, 2014
#include <iostream>
#include <string.h>
#include <sstream>
[Code]....
View 1 Replies
View Related
Mar 12, 2014
I'm supposed to store the value of a countrys population. Then gather out the percentage that countrys population holds when compared with the global population.
Anyway here's the code:
Code:
#include <iostream>
long swe_pop = 9644864;
int main ()
[Code] .....
The result I'm getting is 0%.
I was under the impression that long (or long long) integers could hold high values. And that I could then divide these and answer with a float type value. Giving space for the decimals.
View 3 Replies
View Related
Jan 9, 2014
Im using the remquo function in the cmath library as follows:
int quotient;
double a = remquo (10.3, 4.5, "ient);
This gives the correct remainder (a = 1.3) and quotient (quotient = 2).
Infact about 50% of the answers are right when I play around, however, trying something like:
int quotient;
double a = remquo (2.25, 1.5, "ient);
yields an incorrect quotient of 2 and remainder of 0.
I do think this has something to do with float arithmetic. I recall tinkering with the float number 0.500 and that the CPU actually saves it as 0.50000000000000231. However if my suspicion of float arithmetic as the suspect is correct, I do not understand why a tenth decimal would make such a drastic difference as changing the quotient result.
View 10 Replies
View Related