help me in this program....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • isabelle
    New Member
    • Dec 2006
    • 14

    help me in this program....

    help me in this program....

    Write a program that prompt the user ti input an integer and then outputs the
    numbers with digits reversed. for example, if the input is 12345 the output should be 54321. Your program should find the summation of all digits, for previous example the summation will be 15.

    this is my solution.. but. I think.. its not correct..

    [CODE=cpp]
    #include <iostream>
    #include<fstrea m>
    using namespace std;

    int main()
    {

    int num,digit,sum=0 ;

    ifstream infile;
    ofstream outfile;

    infile.open("in Data.txt");
    outfile.open("o utData.txt");

    infile>>num;
    outfile<<"Numbe r: "<<num<<end l;



    while (num>0)
    {
    digit=num%10;
    num=num/10;

    if(digit>0)
    num=num%10;
    num=num/10;
    sum++;

    }
    outfile<<"Numbe r in reverse Order:"<<num<<e ndl;

    outfile<<"Summa tion of Digits: "<<sum<<end l;



    infile.close();
    outfile.close() ;

    return 0;
    }
    [/CODE]

    can any body help me???
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Your code compiles and runs without errors, are you getting a logical error? Can you describe your issue, show us output?

    Also, next time, please read and follow this .

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      What is going on here?
      [CODE=cpp]

      while (num>0)
      {
      digit=num%10;
      num=num/10;

      if(digit>0)
      num=num%10;
      num=num/10;
      sum++;

      }[/code]

      Your sum variable is just incrementing, it's not getting the values you're pulling added to it.

      Also, you're just dividing the numbers by 10, not reversing the order... Can you come up with an algorithm for that?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by isabelle
        [CODE=cpp]
        while (num>0)
        {
        digit=num%10;
        num=num/10;

        if(digit>0)
        num=num%10;
        num=num/10;
        sum++;

        }
        [/code]
        Bad indentation screams for errors like these. You can easily follow what is
        happening here in this loop (hint: pencil and paper!). That if statement doesn't
        make any sense.

        kind regards,

        Jos

        ps. and please correct your indentation.

        Comment

        • isabelle
          New Member
          • Dec 2006
          • 14

          #5
          Originally posted by sicarie
          Your code compiles and runs without errors, are you getting a logical error? Can you describe your issue, show us output?
          when I input this:

          inData.txt:
          12345

          I get this:

          outData.txt:
          Number: 12345
          Number in reverse Order:0
          Summation of Digits: 1

          But , the outData should be like this:

          outData.txt:
          Number: 12345
          Number in reverse Order:54321
          Summation of Digits: 15

          Comment

          • JamC
            New Member
            • Apr 2007
            • 8

            #6
            How about something like...

            Code removed per Posting Guidelines

            Comment

            • isabelle
              New Member
              • Dec 2006
              • 14

              #7
              Thanks all..

              and; if any one have another indentation for this, I will be grateful.

              Thanks again..

              Comment

              Working...