I'm trying to make a program that takes up to a seven digit binary number and converts it to its decimal equivalent. I'm still a beginner, so this might be a simple question but how do I stop the user from entering anything but 1s and 0s? This means no decimals or numbers other than 1 or 0.I already made it so it won't accept anything below 0 or above 1111111.
I was trying to program an decimal to binary converter (8-bits) in C. I am a complete beginner so I tried to put the 1's and 0's of the binary number as they come without reversing the order for beginning. I have seen example on the internet but didn't understand them so I decided to write it as I understood it. So, I typed the code as shown below:
Code: #include <stdio.h> #include <stdlib.h> int main() { int number; int BitNum[8], x;
[Code] ....
The problem with the code is that if binary form has 0s in it then program displays a random number instead of a 0. For example if decimal is 7, it should print out 11100000 but it displays only 111(and some stupid numbers instead of 0). I have tried to solve it but failed.
1. Because of how limited integers are in terms of storage, the largest binary number i can give seems to be 1 111 111 111. Anything larger breaks the program. Is there any way to increase the largest input I can give without completely rewriting the program? I tried changing the num/numCounter (and the typecasting) to long doubles, but it just messed stuff up (or I did).
2. I'd also like to make it so that if someone inputs a non-binary number my program tells them so and stops. I figured a switch statement within the while loop would work (for when numCounter/divisor is negative or greater than 1), but I was wondering if instead it was possible to use an if statement that could break the while loop?
I'm trying to figure out why the binary to decimal part is not working correctly when the binary value finishes with a 1. In those cases, the decimal value shown in one unit smaller than it should be.
Code:
#include <iostream> #include <cstdio> #include <cstdlib> using namespace std; void Binary_to_Decimal(), Decimal_to_Binary(); //prototype for the 2 functions that contain the converters int main() //the menu { int a_Choice; cout << "Enter 1 - for binary to decimal" << endl; cout << "Enter 2 - for decimal to binary" << endl;
I have to create a converter but the results must be in 4 decimal places just like, if i Entered 5000mm the result would be 500.0000cm . what should I do ? I used Float function
This program compiles, but has a bunch of logical errors. I know my problem is somewhere in the while loop that I have, but I can't figure out where. Here are some of the issues I am experiencing:
1. At the beginning of the program it asks you to enter a number, and when you do it does nothing while proceeding to the while loop where I have it asking the same question
Code: "Please enter a number between 1 and 20 (Enter 0 to stop) "; cin >> num; cout << endl;
I want to be able to eliminate that first statement but if I only run this in the loop without the above statement the program will display nothing on the screen and proceeds to stop.
2. This code runs fine, except that if you make a mistake, it will prompt you to enter a valid number, however; it ignores your first response if the number you enter is valid and asks you to enter a valid number anyway. Once you enter it a second time, it will accept it and the program will continue on.
Code: while(num != SENTINEL) { cout << "Please enter a number between 1 and 20 (Enter 0 to stop) "; cin >> num; cout << endl;
Also if you type in 0 on your first response, it will prompt you that it is not a valid number and ask you to try again, instead of stopping the program like it is supposed to do. On your second response the program will accept your 0 and stop the program correctly.
//Write a program that displays the roman numeral equivalent of any decimal number between 1 and 20 that the user enters. The roman numerals should be stored in an array of strings and the decimal number that the user enters should be used to locate the array element holding the roman numeral equivalent. The program should have a loop that allows the user to continue entering numbers until an end sentinel of 0 is entered.
Input validation: Do not accept scores less than 0 or greater than 20
#include <iostream> #include <string> using namespace std; int main() { // Declare constants and variables const int romanNum = 21; // Size of the elements in the array
I wrote this code purely for educational purposes. It also learn more about how exactly things look in memory. code I have right now ( I will likely add more and change it in the future) .....
I wrote a program that converts Binary code to Grey code. It works, but I feel like it's highly inefficient. I've also been trying out Project Euler, but I seem to always turn to using vectors whenever solving a problem.
/* Binary to Grey Code Converter */
#include <iostream> #include <vector> using namespace std; // Global Variables (I'd like to not use them because I've been told it's bad practice) static int numDigits; int digit; vector<int> bin;
I am very new to programming and have been working on a program that can receive decimals or binary numbers and convert them. The decimal --> binary works fine. For some reason I cannot figure out I cannot get the "BinaryToDecimal" function to perform. By putting a "printf" into the for-loop.
Code:
#include <stdio.h>#include <string.h> #include <math.h> char* ReverseString (char _result[]) { int start, end, length = strlen(_result); char swap; for (start = 0, end = length-1; start < end; start++, end--)
How to do this program i can easily do it in a simple for loop but i have to do this program with the following directions:
1. Write a function called bitN() that returns the value of bit N in number, where number is the first parameter, and N is the second. Assume N of the least significant bit is zero and that both parameters are unsigned int's. (A simple one-liner will suffice)
2. Write a main() function that uses bitN() to convert a decimal integer into its binary equivalent. Obtain the integer to convert from the first command-line argument.
3. Use the expression unsigned int numBits = sizeof(unsigned int)*CHAR_BIT; to get the number of bits in an unsigned int. (Include limits.h to get the definition for CHAR_BIT.)
I have a code and am asked to modify it so that it will take as input as unsigned binary number up to 16 digits in length and convert it into its equivalent decimal number and output that decimal number.
All I know is that I use library function strlen() in <cstring> to calculate the length of the input string.
I also know I have to use something called pow(2,4);
//pow (); is found in cmath
I was told to use sum = sum >>16-l; (l is the length of />/>
#include <iostream> using namespace std; int main() {
So I tried debugging this program almost 10 times, each time finding a problem then making code to fix it, I used to get wrong values but then now after I changed the calculation from an infinity while loop to a for loop (because after some changes I realized that now I know the number of entries, meaning the start and the end of the loop) the program now displays no values at all. I checked it thoroughly and until the calculation process its functioning exactly the way it should but then the calculation breaks hell loose . Also the for loop that raises 2 to the power needed works fine too so it must be something with the other processes included in the calculation....
This used to be a function of a multiple value types conversion program, I isolated it for easier analysis as a lone standing program.
Code: #include<iostream> using namespace std; int main() { int d[10],e[10],anse=1,r,limit; short int counterd=0,i,j; float bind=0;
I then had an follow up exercise which was to replicate but for any base up to 10, i thought i would just have to replace 2 with a variable obtained from the user, however this did not work as i got an error saying too few arguments function but i cannot see why i am getting this.
Code: #include <iostream> #include <iomanip> #include <cmath> using namespace std; float Conversion (int n, int b);
The goal of my program is to convert a decmial number to a binary number.First, the program gets an input to an array of chars, and the function translate_dec_bin converts this array to a decimal number through the strtoul function.The problem is that my program prints the binary number with an additional "0".For exmaple, for the input 1 - the program prints 01 instead of 1, for the input 3 - the program prints 011 instead of 11.
I'm trying to write a program that converts a decimal number to a binary one. I have most of the program written, but I am having a little bit of trouble. Whenever I enter a decimal number, the program will convert it correctly to binary, however, the last number is not included in the conversion. EX: Converting 37 into binary (0100101) yields 010010 when entered into the program. BTW the program must utilize recursion to achieve this goal.
#include <iostream> using namespace std; void decToBinary(int num1); int main() { int num1;
Now I have the binary numbers printed out in my code, but I don't know how I can covert them into to decimal.
#include <iostream> #include <iomanip> #include <string> #include <cmath> using namespace std; int main() { int numberOfDigits; int numberOfRows; char flag;
What is the difference between the two functions below? I created the function in the top and my friend created the function in the bottom. I just want to know why the function with the while loop prints the binary numbers backwards. And the recursive function prints the binary numbers correctly.
void findBinary(int num) { int remainder = 0; while ( num > 0) { remainder = num % 2; cout << remainder; num = num / 2;
I am learning c because I want to get back into programming microcontrollers, which I previously did in assembly. I wanted to make something fairly tight in terms of program memory and RAM to show me an output in binary form. When you are troubleshooting a file register or serial interface when you can see the actual bit values (on a small LCD for a micro-controller) and compare it to a datasheet.
Code: #include <stdio.h> #include <math.h> int main() { int i; int decimaltoconvert; int convertingarray[7]; int convertingarray2[7];
[Code] .....
Also, how might I go about putting that into a function that I could call?
My size of binary file is 1920 KB and my struct size is 124 kb. now to find number of records in file I divided 1920/124 and it gives me 15.4.... do I add 1 to 15.4 and make it 16 or do i take it as 15?
Now as you can see that all the binary output is in a[] but how do I get it into a string so that I can use something like printf("%s",string) and get the binary output ?
I'm trying to pass a decimal number to a function and convert it to binary and return it and print it out in main. But it prints out 1011 and then seg faults...not sure where it's tripping up
Code: int main(){ char* binNum = decToBin(25); int i = 0; while(binNum != NULL){