Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › noob question; spltting a number into separate didgits [c++]
New Posts  All Forums:Forum Nav:

noob question; spltting a number into separate didgits [c++]

post #1 of 11
Thread Starter 
im writing a homework program, no im not asking for you to do it for me, but its converting a number of an arbitrary base into a decimal number. i know how to convert it and all that jazz, but what im having trouble with is splitting the number up and putting it into an array so that i can actually convert it. This is what i have so far.
Code:
#include <iostream>
using namespace std;

int numAr[8];

void convert(int number, int base){
 
    
}

int main(){

    
    int number;
    int base;
    int decimal;
    
    
    cout << "enter the number to convert: " << endl;
    cin >> number;
    //split number into digits
    cin >> base;
   // convert( int number, int base);
    cout << "Your number " << number << " of base " << base << " is now " <<  decimal << " of base 10." << endl;
    
    return 0;
}

havent written the convert function yet cause thats easy, but i would appreciate guidance on my problem. I saw something reading around about like number = number %10 or something like that, but plz help.

thanks!
post #2 of 11
yup. the mod (%) operator is what you want. % is the remainder operation. for example, 15%10=5.

just use a for loop. this code will split off the last digit in the original number. then just put last_digit into the array.
Code:
int last_digit = number%10;
number = number/10;
array[i] = last_digit;
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
post #3 of 11
Thread Starter 
Thanks Man, i knew it was something like that but i kinda got it, finished my program, but im getting a few errors, maybe you could help sort em out?
Code:
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <stack>

using namespace std;

int numz[8];
int base;

int countDigitsInInteger(int n){
    int count =0;
    while(n>0)
    {
        count++;
        n=n/10;
    }
    return count;
}

int convert( numz[], base){
    int k;
    int decNum;
    int base;
    
    for( k = count-1; k >= 0; k--){
        decNum = decNum + (numz[k]*base);
        
    }
    
    return decNum;  
}



int main(int argc, char *argv[]){  
    int intLength =0;
    int number;
    int digit;      
    int decimal;
    
    string s;
    
    cout << "Please enter an integer ";
    cin >> number;
    cout << "Your number was "<< number <<endl;
    
    //just incase they entered negative, make the number positive
    if (number<0)
        number = -number;    

    //
    intLength = countDigitsInInteger(number);

    
    //break apart the integer into digits
    stack<int> digitstack;
    while(number>0){                         
        digit = number % 10;
        number = number / 10;
        digitstack.push(digit);   
    }
    
    //put numbers off stack into the array
    int i;
    for(i = 0; digitstack.size() > 0; i++){
        numz[i] = digitstack.pop();
    }
    
    decimal = void convert( numz[], base);

    cout <<"The number " << number << " in base " << base << " is " << decimal << " in base 10 (decimal)." << endl;
    return 0;
}

and the errors im getting are
Quote:
main.cpp:21: error: expected primary-expression before ']' token
main.cpp:21: error: initializer expression list treated as compound expression
main.cpp:21: error: expected ',' or ';' before '{' token

they are all referring to the line "int convert( numz[], base){"


thank you sir!
post #4 of 11
you need to tell the compiler what type they are.
Code:
int convert(int numz[], int base){
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
post #5 of 11
Thread Starter 
thanks for replying man, but i just got even more errors by doing that frown.gif
ahh oh well, guess ill figure it out one day!

+rep tho
post #6 of 11
you can also try to google c++ compilation errors to get an idea of what needs to be fixed. good luck thumb.gif
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
post #7 of 11
Code:
decimal = void convert( numz[], base);

change to:
Code:
decimal = convert(numz[], base);

I will go over your code one more time though

I would avoid global variables...but you can do it the way you did it.

I would have created:

int numz[8];
int base;

in main...and then passed them as arguments to your convert function.
Edited by surfbumb - 3/15/12 at 3:05pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #8 of 11
Thread Starter 
still having a little bit of trouble, but im still working on it.

thanks alot for the help tho both of you, not often ppl reply to such noobie questions
+ rep to u 2 surfbum!
post #9 of 11
give this a go...I condensed your program to make it simpler...got rid of the countDigitsInInteger function because your stack you created holds the correct amount...also your conversion algorithm was a little bit off....you have to use the pow function from the math library. Look at how I did the placing of stack contents into the array...you have to use top to return an integer...then pop the stack and repeat till the stack is empty.


In addition, if you want to convert say hexidecimal to decimal...then you would need to do a slight tweak which i did not do for you, which is pretty simple though. Any hexidecimal number with numbers 0-9 will work....if you have A-E in the hexidecimal number, simply convert it to integer form then push it on the stack like usual...and the convert function will handle the rest.

I'm not sure if you need to do error checking for input....ex....someone entering a number other than 0 or 1 for binary.

Good job though, keep it up....let me know if you need additional help.
Code:
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <stack>

using namespace std;

double convert(int numz[], int base, int count);

int main(){ 

        int numz[8];
        int base;

    int intLength =0;
    int number;
    int digit;      
    double decimal;
    
        cout << "Enter a base: ";
    cin >> base;

    cout << "Please enter a number in base " << base << ": ";
    cin >> number;
    cout << "Your number was "<< number << " in base " << base << endl;
        
        int original_num = number;
    
    //just incase they entered negative, make the number positive
    if (number<0)
        number = -number;    

    //break apart the integer into digits
    stack<int> digitstack;
    while(number>0){                         
        digit = number % 10;
        number = number / 10;
        digitstack.push(digit);   
    }
    
        int length = digitstack.size();
    //put numbers off stack into the array
    int i = 0;
    while(!digitstack.empty()){
        numz[i] = digitstack.top();
                digitstack.pop();
                ++i;
    }
    decimal = convert(numz, base, length);

    cout <<"The number " << original_num << " in base " << base << " is " << decimal << " in base 10 (decimal)." << endl;
    return 0;
}

//converts the given base to its decimal equivalent
double convert(int numz[], int base, int count){
   
    double decNum = 0;
        double temp = count;
    
    for(int i = 0; i < count; ++i){
                decNum = decNum + (numz[i] * pow(base, temp - 1));
                --temp;
    }
    
    return decNum;  
}


Edited by surfbumb - 3/16/12 at 5:36pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #10 of 11
Thread Starter 
wow... first of all, i didnt know people like you existed.
That was honestly so kind of you to help me through this.

i didnt even occur to me that the stack was hindering the soulution.
being new and all im just trying to go by stuff that i know when, guess what..
all i needed to do was make it simpler, but i was tryig to make it more complex for nothing doh.gif

i really want to lea how to program, and seeing what i had which was "almost complete"
and seeing how you fixed it helps me out so much man,

thank you, seriously.

i would say + rep (which i did) but that isnt even enough to show my gratitude!
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › noob question; spltting a number into separate didgits [c++]