C++ :: Check If Number Belongs To Particular Base Or Not
May 2, 2013
I am making a number base conversion program in c++, suppose that user enters an base 2 number and want to convert that number in base 4. when he enter the number (which he wants to be converted), how can i detect that number is belongs to base 2 number system or not.
For example: if user enter 1001011001 , program should continue and convert that number to base 4. and if user enter 1200101 or 1001012 , program should not convert that number and output that this number is not belongs to base 2 number system.
using namespace std; void Conversion (int n); int main () {
[Code] .....
I now have a follow on exercise that requires me to convert to binary from ant base up to 10, i thought this would just be replacing the 2 with a variable obtained form the user, but i am having problems as within the function i am getting an error that i haven't passed enough arguments and i cant see why i get this. I did the following:
Code: #include <iostream> #include <iomanip> #include <cmath> using namespace std; float Conversion (int n, int b);
You enter decimal number into the program and what base you want. The integer part of the decimal is being handled fine, but the decimal is not.
For example, I enter 15.6847 and base 10, which means I'm going from base 10 to base 10. It spits out 68469999999999 for the decimal part. (Do not worry about the first block of numbers. The second block seperated from the first by a space is where the decimal will appear in order.)
#include <iostream> #include <string> #include <math.h> using namespace std; int baseConverter(int, int, int *, int *);
I just wanted to add strings in any base form (example 1101+100 = 10001 in base-2 but it could be added using any base-form like in base-3 to base-36) and I'm having a big trouble with my code because it gave me incorrect results.
addition(char st[], char st2[], int base){ int i, j, carry = 0, ans, len, o=0, z=1, l=0; char final[50]; if(strlen(st)>=strlen(st2)) len = strlen(st); else len = strlen(st2);
The code on lines 44-53 is suppose to display a message when the user enter a negative number, however, when a correct positive number is entered the message is display again.
#include<iostream> #include<cctype> using namespace std; int main() { char carType; int A, B, C;
I can have at most 3 structs in array, but it could be 0,1,2 or 3 structs in array. I am trying to avoid dynamic memory allocation. I initialize sensors to 3 to reserve space for them in memory, since there may be at most 3 elements in the array. But I am testing a condition where there will only be 2 elements:
Code:
#include <stdio.h>#include <stdlib.h> #include <string.h> typedef struct { unsigned long long int address; float current; unsigned char pressure_units; } sensor;
[Code]...
The problem is even though there are only 2 sensors out of 3 in the array, sizeof(sensors)/sizeof(sensors[0]) returns 3. I assume because when it allocates memory for 3, it includes that allocated memory even though it really doesn't contain the struct. How can I figure out how many elements were really inserted into array, not just allocated to array?
fstream infile1(argv[1]); if(!infile1.is_open()) cout << "Could not open file"; else { char listNum; while(infile1.get(listNum)) cout << listNum; }
However, when I check for odd or even numbers it will check each and every number.
printed like this (partial list): 1 is odd 3 is odd 8 is even 9 is odd
But it should print: 138 is even 9 is odd
I tried using getline, but it keeps giving me the errors: invalid conversion from 'void*' to 'char**' invalid conversion from 'char' to 'size_t*' too few arguments to function 'ssize_t getline(char**, size_t*, FILE*)'
Here is the getline code, what am I doing wrong? I have tried switching things around, adding things. Just nothing works.
ifstream infile1(argv[1]); if(!infile1.is_open()) cout << "Could not open file"; else { char listNum; getline(infile1, listNum); cout << listNum; }
I have to obtain a check number from a datafile and then also get company information also from a datafile.
So my first question is about the:
Code: char outputFilename[]= "out.list"
Is this the name of the output file I'm going to write to? And also the file has to be created before being used...is that line of code creating the file or do i have to create it in notepad?
I got this algorithm of conversion and now I'm stuck at how to code it.
"Algorithm to Convert From any Base to Base 10 Decimal."
Let 'n' be the number of digits in the number. For example, 104 has 3 digits, so 'n'=3. Let 'b' be the base of the number. For example, 104 is decimal so 'b' = 10. Let 's' be a running total, initially 0.
For each digit in the number, working left to right do:
Subtract 1 from 'n'. Multiply the digit times b^n and add it to 's'.
When done with all the digits in the number, the decimal value should be 's' .
I need to convert an integer, for example 10, to its base 16 equivalent, 16. I found this code that converts a base 16 number to base 10, but I need the opposite. Plus, this code doesn't seem to work properly with input values under 32.
Whenever I run the program I get no errors yet it doesn't return a value for total it just exits the program. The program is supposed to output the number of the day based on the month day and year and check whether it is a leap year.
#include <iostream> using namespace std; int totaldays(); int days(); int month, day, year; bool leap(); int main() { char dash; cout << "Enter the date in mm-dd-yyyy format to find out what day this is in the year." << endl;
I have run into a problem which is mostly just an annoyance. I need to know if i can have pass a derived class to a function which has the base class as its parameter. For example i have been creating a program but i have a function which needs to work for multiple classes all derived from the BaseObject class
Code :
class folder : public BaseObject {} class BaseObject {void function(BaseObject B)}
I'm working on a program and I need to convert big numbers to radix 64. I would like to shorter them whit conversion that's why I choosed base 64. I have two problems:
1. The conversion works only for not so big numbers. Untill about 2^32. I would like to convert bigger numbers. Is there any solution? (I thought on GMP/MPIR library, but I can't managed it.)
2. The conversion back to decimal base doesn't works, because I use 'strtoul()' which doesn't support bigger bases like 36.
A pointer to base class, if assigned to a derived class, only points to the base part right? So you can only use the base part of the derived class with that pointer and no methods from the derived class?
I have 2 classes with a Function with the same definition (both inherited from the same base class) and in my derived class I derive from both of those 2. Is it possible to use the Methods of both classes? for example with an other name?
class A { protected: int print(int a) { std::cout << "int A: " << a << std::endl; } }; class B : A
[Code] ....
is there something like using C::print as printc;?
The Problem, I have a Sprite class that derives from a Rectangle with properties Position, Size, Origin and Angle and a Text class that derives from Rectangle. Now i have a Button class deriving from both Sprite and Text.
- The Position, when moving the Button i have to change the position of both so i Implemented a new Method which calls SetPosition from the Sprite and the Text. - The SetSize just affects the Button so i just did using Sprite::SetSize; - The angle affects both so i just implemented a new Method and hide the other two
The problem is here: - The Origin: writing button.SetOrigin(-1,0) should set the Origin of the Button and writing button.SetTextOrigin should set the Origin of the text.
Should i just reimplement a Mehtod named SetTextOrigin and call Text::SetOrigin from there and hide the button.Text::SetOrigin or is there something like using Text::SetOrigin as SetTextOrigin?