Thread: Program question!?!?!?!

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Program question!?!?!?!

    I am using "C++ how to program" by deitel/deitel forth edition. The problem is as follows:

    "Write a program that inputs a five-digit number, spearates the number into its individual digits and prints the digits separated from one another by three spaces each. (Hint: use the integer division and modulus operators)."

    It's in chapter 1, so the only things I have learned so far is the basics: cout, cin, cerr, mathmatic operators (order of operations) and if statements. Here is my code:

    Code:
    01     #include <iostream>
    02
    03     int main()
    04     {
    05       int 5digitnumber;
    06       int newoutput;
    07
    08      std::cout <<"Input a 5 digit number"<<std::endl;
    09      std::cin >>5digitnumber <<std::endl;
    10      
    11      newoutput = 5digitnumber / 1; //trying to separate digits...im so lost here.     
    12      
    13      std::cout <<newoutput;
    14      return 0;
    15      }
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 5digitnumber
    Variable names must begin with a letter

    > std::cin >>5digitnumber <<std::endl;
    Don't mix << and >> in the same line
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    1479
    Join Date
    Aug 2003
    Posts
    253
    Thanks. I am not using a compiler yet, im just writing out on a text editor untill I think I have the program solved then I will compile it a sort out the syntax errors later.
    Knowledge is power and I want it all

    -0RealityFusion0-

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Separate from the coding part, you should work on how to use division (/) and modulus (%) to separate the integer into digits. It is a very common trick, and you should be able to figure it out if you just think about how to get the first digit by dividing the number by something else. Play with it on a calculator, your Operating System's calculator probably has both / and Mod. If you are having trouble coming up with it, you can search for the answer on this board.

  5. #5
    1479
    Join Date
    Aug 2003
    Posts
    253
    Sorry but I am still struggling on this. I searched the board and found this snippet of information:

    Code:
    digit = x % 10 + '0';
    x /= 10;
    I have no clue how or why that would work. What does the '/=' operator do?!?

    So with that, I applied my program to it and this is what I am coming up with:

    Code:
    output = bignumber % 10 + '0';
    bignumber /= 10;
    Doing this gives me the number 49..... I am assuming that the ASCII equivelent of '0' is 49 because I tried substituting the '0' with spaces and nothing was printed to the screen.
    Knowledge is power and I want it all

    -0RealityFusion0-

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    I don't think the '0' is necessary, since you can just output the number instead of converting it to a character. Did you try this with a calculator? For example, if your big number is 34567, what do you get when you put in 34567 Mod 10? Mod 100? Div 10? Div 100?

    By the way, operator /= simply divides the number on the left by the number on the right, and stores the answer in the number on the left. A little experimentation can show you that:
    Code:
    #include <iostream>
    int main()
    {
        int bignumber = 34567;
        std::cout << bignumber << std::endl;
        bignumber /= 10;
        std::cout << bignumber << std::endl;
        bignumber = bignumber / 10; // Same as /=
        std::cout << bignumber << std::endl;
    }
    I have a feeling you won't be wanting to use /=, though. Maybe %=.

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    28
    Hi after seen your post i had to also try this and it took me ages to figure it out.

    I couldn't do it with the modulas operator so can someone show me how?
    Maybe just an example?

    My code is here and it works but it is horrible.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
      int number,
          first,
          second = 0,
          realsecond = 0,
          third = 0,
          realthird = 0,
          actualthird = 0,
          forth = 0,
          realforth = 0,
          fifth = 0,
          sixth = 0,
          realsixth = 0,
          seventh = 0;
    
    
       cout << "Input a 5 digit number: " << endl;
       cin >> number;    // 23456 was entered
    
       first = number / 10000;
       cout << "first\t " << first << endl;  // just to understand it :)
    //--------------------------------------------------------------
       second = 10000 * first;   // 20000
       cout << "second\t " << second << endl;
    
       realsecond = number - second;
       cout << "third\t " << realsecond << endl;  // 3456
    
       third = realsecond / 1000;     //   3
       cout << "forth\t\t " << third << endl;
    //----------------------------------------------------
       realthird = 1000 * third; // 3000
       actualthird = realsecond - realthird;
       cout << "fifth\t " << actualthird << endl;  // 456
    
       forth = actualthird / 100;  // 456
       cout << "sixth\t\t " << forth << endl;
    
       fifth = 100 * forth; // 400
       realforth = actualthird - fifth;
       cout << "seventh\t " << realforth << endl; // 56
       realsixth = realforth / 10;
       cout << "2nd last number\t\t " << realsixth << endl;
       seventh = realforth % 10;
    
       cout << "\t\t " << seventh << endl;
    
         return 0;
    }

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    num = 123

    123 % 10 = 3
    123 / 10 = 12 using integer math where decimals are ignored.

    keep repeating until num is less than 10.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Well, since the exercise didn't specify what order the digits needed to be in, you could just do this:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int num;
    
      cout<<"Enter a number: ";
      if ( !( cin>> num ) ) {
        return 0;
      }
      while ( num != 0 ) {
        cout<< num % 10;
        num /= 10;
        if ( num == 0 )
          cout<<'\n';
        else
          cout<<"   ";
      }
    }
    To get them in the right order takes more work or more ingenuity:
    Code:
    #include <iostream>
    
    using namespace std;
    
    void print_digit ( int num )
    {
      if ( num == 0 ) {
        return;
      }
      print_digit ( num / 10 );
      cout<< num % 10 <<"   ";
    }
    
    int main()
    {
      int num;
    
      cout<<"Enter a number: ";
      if ( !( cin>> num ) ) {
        return 0;
      }
      print_digit ( num );
      cout<<endl;
    }
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Dec 2003
    Posts
    28
    I'm sorry , but it has to be done with alot less c++ knowledge using if if/else - + / % only as it is the first chapter of the book.
    I know ou have no idea whats in the chapter but thtas what he was trying and i want to try also.
    I could have did it with fucntions also but if i can't do it using the above i'm a retard.
    Thanks for your help prelude and elad.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >it has to be done with alot less c++ knowledge using if if/else - + / % only as it is the first chapter of the book
    Is this dumb enough?
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int num = 12345;
    
      cout<< num / 10000 <<"   ";
      cout<< num % 10000 / 1000 <<"   ";
      cout<< num % 1000 / 100 <<"   ";
      cout<< num % 100 / 10 <<"   ";
      cout<< num % 10 <<endl;
    }
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Dec 2003
    Posts
    28
    That code is oozing quality.
    Thanks alot i understand it now for the modulas operator.
    Cya.

  13. #13
    1479
    Join Date
    Aug 2003
    Posts
    253
    Thanks to everyone that helped. What I learned was that if you use an integer of int typed and use any kind of math function to get a different value, if the value is a decimal it will ignore everything past the decimal. Thanks once again.
    Knowledge is power and I want it all

    -0RealityFusion0-

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding a this C program
    By cnb in forum C Programming
    Replies: 10
    Last Post: 10-11-2008, 04:43 AM
  2. newb question: probs with this program
    By ajguerrero in forum C Programming
    Replies: 5
    Last Post: 04-19-2006, 08:04 AM
  3. Random Question Assign Program
    By mikeprogram in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2005, 10:04 PM
  4. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM
  5. Replies: 8
    Last Post: 03-26-2002, 07:55 AM