Thread: please help..very simple question

  1. #1
    novice
    Guest

    Question please help..very simple question

    Hi,

    thanks for clicking on my post.My problem is if the user enters say 4 digit number the output should be 4 digits with 2 spaces in b/w them..
    for example. say number is 7689, output should be
    7 6 8 9.
    Any suggestions, hints and ideas????????Other than accepting the number ,I cant go further .

    Merry Christmas ...and thanks in advance.

    novice

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

    What do you mean?

    Do you want the program to input a number then print it out with 2 spaces in between each digit? If so you should use a string:

    #include <iostream.h>

    void main(void)
    {
    char number[15];
    int loop;
    cout << "Hello, Please Enter A Number... \n";
    cin.getline(number, 14); //get string number
    while (number[loop] != '\0') //while not end of string (\0)
    {
    cout << number[loop] << " "; //print number then " "
    }
    cout << "press a key to continue... \n";
    cin >> loop;
    }

    That Might have a few errors but it should do the job.
    ...

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    28

    ..

    I forgot to say loop should be -1 at begining, and when declaring loop you shhould go while(number[loop++] != '\0') - i forgot the ++.
    ...

  4. #4
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    accept the numbers into a character array then output the first with two spaces after, the second with two spaces after, etc etc...

    cin.getline(array,number of letters, '\n') works well to get the numbers into four array slots.


    Consider the following...

    #include <iostream>

    using namespace std;

    int main()
    {

    char array[5];

    cout << "four digit number: ";
    cin.getline(array,5,'\n');

    cout << array;


    return 0;
    }


    Now you have to figure out the spaces... (which is terribly easy)

    psuedocode hint... loop cout array[counter++]
    Blue

  5. #5
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    or you could just have someone nice enought to write it for you as in the above.... I hope you learned it though...
    Blue

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM