Thread: a how-to :(

  1. #1
    Unregistered
    Guest

    a how-to :(

    ok keep in mind i have done nothing advanced yet... only using basic strings, variables and outputing things to the screen and formatting them...

    so here goes my question...

    how do i make a program that splits an integer into two numbers eg
    input 27
    first digit is 2
    second digit is 7

    later on i have to modify it to aceept 3 digit numbers... now i am a bnit stumped... i dont want you to wrtie the whole thing for me... i just want a shove in the correct direction

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    If the program accepts the integer as input from a user then it's easy. Simply read the input as a string and print each individual element to break it up.
    Such as if the user enters 23, the array looks like this:
    array[0] = 2;
    array[1] = 3;
    If you print array[0] then it will just output 2.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Use the modulus operator to split digits off the end. Use the place value of the digit to the left to modulus by. E.g, 27 % 10 = 7. The 2, value to the left of the 7, is in the tens place, so 10 was used to modulus by.

  4. #4
    Unregistered
    Guest
    Code:
    #include <iostream.h>
    #include <lvp\string.h>
    
    int main()
    
    {
    	String number;
    
    	cout<<"Enter the two-digit number: ";
    	cin>>number;
    	cout<<"The first digit is "<<number[0]<<endl;
    	cout<<"The second digit is "<<number[1]<<endl;
    	return (0);
    }
    would this be correct?
    my compiler does not have lvp/string and my book sasy i should be using it... i have msvc++ 4.0 wich is a bit old and my book is for visual c++ 6.0

  5. #5
    Unregistered
    Guest
    Originally posted by salvelinus
    Use the modulus operator to split digits off the end. Use the place value of the digit to the left to modulus by. E.g, 27 % 10 = 7. The 2, value to the left of the 7, is in the tens place, so 10 was used to modulus by.
    oooo i think thats is what im suposed to do thankies

    we havent done arrays yet prelude but thx for the help i have seen you helping a lot here and damn you deserve more credit than you get that is all

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >you deserve more credit than you get that is all
    I'm not here for credit, I'm here to help out

    Funny how they ask you to come up with a solution for itoa when you haven't even covered basic arrays yet. I consider the itoa solution more advanced that arrays, but I'm not your instructor

    -Prelude
    Last edited by Prelude; 02-23-2002 at 07:14 PM.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    5
    yea, just take it as a string, i'll try it later myself, hehehe~~

Popular pages Recent additions subscribe to a feed