Thread: I need help!!!!!

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    2

    I need help!!!!!

    I have to enter a three digit number and then get an output of
    the first digit
    the second digit
    the third digit

    ~Sam Mixter~

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    Please post your code. Its not easy to guess what's going on.

  3. #3
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    use an array

  4. #4
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    if you don't know how to do an array, here is a liitle bit of code...
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
    int array[2]; // remember that C++ regards 0 as one of the parts of the array
    cout << "Enter your 3 digit number: ";
    cin >> array[2];
    cout << "\n";
    cout << array[0];
    cout <<"\n";
    cout << array[1];
    cout << "\n";
    cout << array[2];
    
    system("pause"); // system calls are crap, I know
    return 0;
    }
    hope this helps

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    Hi

    just few remarks about the code above:

    int array[2]; -
    declare an array with only 2 elements of type int
    they can be accessed via indexes 0 and 1 so the last cout with array[3] is illegal, it will print a garbage.

    cin >> array[2]; -
    is it supposed to read several int's from the cin? if true - then you are in trouble.
    this expression just read one integer value from the keyboard and places it in an unexisting place - array[2];

    so to correct the code above - enlarge the array ehough to hold all the three elements and read them one by one.

    damyan

  6. #6
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    in this little code, when it inputs array[2], it gets all three numbers. eg. if you enter 111, it will output:
    1
    1
    1
    but if you input 1111 it will output a whole lot of garbage. In other words: it works, end of story.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    Hi face_master,

    :-) did you ever try this code, so becouse you didn't
    just for example

    let declare an variable of the same type as the elements of the array - int - just before the declaration of the array[2]

    ....
    int foo = 5;
    int array[2];
    ...
    and output that variable - foo - after your
    cin >> array[2];
    cout << foo;

    these are all local variables allocated in the stack
    when you modify an unallocated variable - in our case array[2]
    you change the memory just above the array elements - the variable declared just before the array or if such variable didn't exist you override the return addres of the function and it will not exit normally.

    damyan

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Damyan is right on declaring arrays. Declaring
    int array[2]; will declare a 2 element array, with values array[0] and array[1].
    As for inputting cin >> array[2] to input the entire number into each array element, maybe, but I've never seen that. You usually use a loop to input multiple values. cin >> array[2] would input only into the specific variable array[2], which in this case doesn't exist. It may be compiler dependent, but this will give out of bounds errors in msvc.

  9. #9
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If this is a homework question then you may not be able to use an array. Check this post for details on how to split the number up.

  10. #10
    Registered User WayTooHigh's Avatar
    Join Date
    Aug 2001
    Posts
    101
    i'd write this program like this:
    Code:
    #include<iostream.h>
    #include<stdio.h>
    
    int main()
    {
    	int x;
    	char y[2];
    
    	cout<<"Enter a three digit number\n";
    	cin>>x;
    	cout<<"\n";
    	sprintf(y, "%i", x);
    	cout<<y[0]<<"\n";
    	cout<<y[1]<<"\n";
    	cout<<y[2]<<"\n";
    
    	return 0;
    }
    is this right? or is there some reason i shouldn't do this?
    Sometimes, the farthest point from the center is the center itself.

    Your life is your canvas, it's only as beautiful as you paint it.

  11. #11
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    is this right?
    Probably (if you add an extra charater to the array), but you can do it without the function call and array -

    #include <iostream>

    using namespace std;

    int main()
    {
    int a;
    cout << "Enter 3 digit number: ";
    cin>>a;

    for(int i=100;i>0;i/=10)
    {
    cout <<a/i << " ";
    a%=i;
    }

    return 0;
    }

Popular pages Recent additions subscribe to a feed