C++ :: Change Of Base Function (hex To Octal Or Decimal)

Oct 6, 2014

I need to create a generic function that changes from any starting base, to any final base. I have everything down, except my original function took (and takes) an int value for the number that it converts to another base. I decided to just overload the function. I am Ok with changing between every base, but am slightly off when using my new function to take in a string hex value.

The code below should output 1235 for both functions. It does for the first one, but for the second, I am currently getting 1347. Decimal to Hex works fine - It's just the overloaded function (Hex to anything else) that is slightly off.

#include <iostream>
#include <stack>
#include <string>
#include <cmath>

using namespace std;

void switchBasesFunction(stack<int> & myStack, int startBase, int finalBase, int num);
void switchBasesFunction(stack<int> & myStack, int startBase, int finalBase, string s);

[Code] .....

View 2 Replies


ADVERTISEMENT

C++ :: Program To Convert Octal Number To Decimal

Mar 27, 2013

I nead to write a program that convert an octal number to decimal number, I thought I did it right but it doesn't work.. I have to use in the first for loop as it is because it is part of the instructions (student homework).

#include <iostream>
#include <math.h>
using namespace std;
void main() {
double numOfDig, num, newNum;

[Code] ....

View 1 Replies View Related

C :: Algorithm To Convert From Any Base To Base 10 Decimal

Mar 25, 2013

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' .

View 6 Replies View Related

C++ :: Convert From Decimal To Binary With Base 2

Dec 7, 2014

I had an exercise to convert from decimal to binary with base 2, it was simple simple i did the following:

Code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void Conversion (int n);

[Code] .....

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);

[Code] ......

View 5 Replies View Related

C :: Program That Converts Decimal To Any Base (mostly)

Jan 30, 2013

I had the idea to write a program that would convert a decimal integer into any base up to 36 (there are no letters after that) so I decided to give it a try.

Code:
#include <stdio.h>
#include <stdlib.h>
char letters(int r); // prototypes function for converting 10's to A's and so on, base 11+ will work
int main() {

int base;
int d; //declares decimal integer
int d_clone; // clones d for a loop

[Code] ......

View 8 Replies View Related

C/C++ :: Convert Decimal To Another Base And Put It Into String

Jun 14, 2014

I have written this function.

void intToBase(unsigned int val,char *buff, unsigned int base) {
int digit = val%base;
if(val == 0)
return;
intToBase((val / base), buff + 1, base);
if(digit >= 0 && digit <= 9)
*buff = (digit + '0');
if(digit >= 10 && digit <= 15)
*buff = (digit + 'A' - 10);
}

This function is suppose to convert a decimal number (val) to any other base number and put it into a string (buff).

But the function puts the value in a opposite way, like if the answer is suppose to be "123" i get this "321".

Note: the function must be recursive and i can't use loops.

View 1 Replies View Related

C++ :: Enter Decimal Number Into Program And Required Base

Oct 12, 2013

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 *);

[Code] ....

View 2 Replies View Related

C :: Converting Decimal Number To Any Base Then Find Ones And Twos Complement

Oct 10, 2014

I have the code working on converting a decimal number to base 1 through 16. I need getting the code to output the one's and two's complement.

Code:
#include <stdio.h>
int main(void) {
char base_digits[16] =
{'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

int converted_number[64];
long int number_to_convert;
int next_digit, base, index=0, a;

/* get the number and base */

[Code] ....

View 8 Replies View Related

C++ :: Converting Octal Value To Actual Number?

Jan 22, 2015

I'm a games/apps developer and right now I'm developing games/apps for iOS, Android and also PC / Facebook. I've also been working on C++ for some time but since I'm learning it all by myself, I still have some beginner doubts.

Is there any easy way to code a value conversion (for example an octal value to an actual number)?

View 2 Replies View Related

C/C++ :: Adding Base-N Through Strings (between Base-2 And Base-36)

Feb 27, 2015

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);

[Code] ....

View 1 Replies View Related

C/C++ :: Converting Number Into Binary Or Hexadecimal Or Octal Using Arrays

Mar 1, 2013

Write a program in c++ to accept a number and convert this number into binary or hexa decimal or octal number according to the user choice using the concept of array.

View 6 Replies View Related

C++ :: Calling Base Function From Derived Overloaded Function

Nov 10, 2014

Here is a sample of my question

class Base{
public:
int getNum();
private:
int numToGet;
}
class Derived: public Base {
public:
friend ostream& operator<<(ostream& output, const Derived &B);

[Code]...

View 1 Replies View Related

C/C++ :: Function Within A Base Class

Mar 1, 2012

I am working on a program for school and I have managed to get everything to work, except the last portion of the program. It seems that the program is not calling the last function EmployeeSummary. The program will compile, and will accept data and compute overtime pay, etc. Just won't go on to the last function.
 
#include <iostream>
#include <string>
#include <iomanip>  
using namespace std;  
class CPayroll {  

[Code] ...

View 3 Replies View Related

C++ :: Convert Base 10 To Base X Number Within String Of Characters

Apr 15, 2013

How would you convert say "238273615237287352536266362524382737272" base 10 to a base x number contained within a string of characters?

View 2 Replies View Related

C++ :: Base Class Member Function

Apr 9, 2014

I have encountered following lines in base class and I do not comprehend its meaning of "= 0" at the end of the member functions;

distance_list intersect(ray & r) = 0;
appearance get_appearance(vector & pt) = 0;

where distance_list is a list of doubles and appearance is properties.

In general, what does this "equal sign and 0 " mean for the member functions in the base class?

View 3 Replies View Related

C++ :: Base Class Function Gets Called

Oct 26, 2012

Here are the classes:

BaseClass.h

Code:
class BaseClass {
public:
BaseClass();
virtual ~BaseClass();
virtual void printStuff() const;

[Code] ....

When I call printStuff, the DerivedClass's function gets called. Now, if I remove the const part from the DerivedClass's printStuff function, we call the BaseClass's printStuff function.

View 4 Replies View Related

C++ :: Function To Convert Decimal To Binary

Jul 11, 2014

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;

[Code] .....

View 3 Replies View Related

C++ :: Inheritance - Class Base Has No Member Function

Jan 16, 2014

I have a simple question about inheritance. Consider the following code:

Code:
Class Base {
int type;
Base(){};
};
Class Derived1 : public Base

[Code] ....

I get the following error: Class "Base" has no member "Function1";

That makes sense - as Base has not declared Function1. But how can I loop through a vector of Bases, and then if the object is of type Derived1, call the function Function1?

View 11 Replies View Related

C++ :: Can't Output Decimal Number From A Function Result

Mar 28, 2013

I have this simple code here:

Code:
#include <iostream>
int multiply (double x, double y) {
double result = x*y;
return (result);

[Code] ....

I get the answer 5.94 (which is what I'm looking for). I can't work out why the first example is not outputting a decimal number. I have set the variables as a double so I just can't see why this is not working for me.

View 1 Replies View Related

C :: Function To Convert String To Floating Decimal

Dec 24, 2014

This was an exercise from a book (convert a character string into a floating point value). It seems to work with both negative and positive decimal numbers.

It basically get each digit from the string multiplies by 10 to hold the place, and then adds the next to the result, if that makes sense.

Then determines where the decimal and null character are to figure out what to multiply by (1/1000 or whatever) to determine where the decimal should go. The variable names dealing with this part of the program aren't accurate names right now.

My question is, the output, is always putting 6 zero's. So if the argument is .95, Ideally the output should say .95 and not .950000, even though the value is still correct.

I know there the %.2f to determine the amount of decimals, but the amount of decimals in these instances would be varying depending on the argument sent to the function.

So, if 600.158 was sent as an argument, only 3 decimals would be displayed, as opposed to two from the previous example. Is there a way to do this?

Code:
// Function to convert a string to an integer
#include <stdio.h>
double strToFloat (const char string[])

[Code].....

View 7 Replies View Related

C++ :: Singleton Base Class - How To Implement GetInstance Function

Aug 20, 2014

I'm playing with the idea of a singleton base class, but I'm having an issue with how to implement the GetInstance() function in the base class. Since I'm trying to make this ridiculously simple for the child, I'd like to handle that in the base.

class Singleton {
private:
static Singleton* instance;
Singleton() { Construct(); } // Private to avoid other instances

[Code] .....

It would be easy to use like so:

class Hello : public Singleton {
private:
std::string hello;
void Construct() { hello = "hello"; }
public:
std::string GetHello() const { return hello; }
};

Then the instance would be handled like so:
std::cout << Hello::GetInstance()->GetHello();

View 12 Replies View Related

C :: Pass Decimal Number To Function And Convert It To Binary?

Feb 26, 2013

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){

[Code].....

View 6 Replies View Related

C++ :: Recursive Function - Printing Integer With Decimal Separators

Apr 7, 2013

I am trying to solve the following task:

-Write a recursive function that prints an integer with decimal separators. For example, 12345678 should be printed as 12,345,678.

The problem is that I don't know how to modify the integer in such way. I was thinking to convert it into a string, do an algorithm and then turn the string back to integer.

How to do such modification.

View 9 Replies View Related

C++ :: Member Function In Derived Class Call Same Function From Its Base Class?

Sep 18, 2013

How can a member function in my derived class call the same function from its base class?

View 1 Replies View Related

C++ :: Pointer Usage To Base Class Object In Vector Int Main Function

Aug 12, 2013

I know what are pointer's and how to use them but there is one point i am not able to understand. Below is the example code

I understand everything in the below code except 1 thing why i am using pointer to base class object in vector int the main() Function?

Code:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
// base class

[Code] ...

Here is the lines of code i want to understand.

Code:
vector<Employee*> employees;
employees.push_back(&emp1);
employees.push_back(&mgr1);

I know if i will not use the pointer base class function "virtual double grossPay" will be called for both base class object and derived class object and when i will use pointer with reference to the object because base class function is virtual it will look for same function in derived class and if available it will execute it.

View 3 Replies View Related

C :: Need To Convert Base 10 Integer To Base 16

Mar 20, 2014

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.

Code:

uint32_t input = 16;
uint8_t temp;
temp = ((input>>8)*100)|((input>>4)*10)|(input&0x0f);

View 13 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved