473,506 Members | 17,393 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help me in this program....

14 New Member
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..

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>  
  2. #include<fstream>                      
  3. using namespace std;                       
  4.  
  5. int main() 
  6. {         
  7.  
  8. int num,digit,sum=0;
  9.  
  10.     ifstream infile;
  11.     ofstream outfile;
  12.  
  13.     infile.open("inData.txt");
  14.     outfile.open("outData.txt");
  15.  
  16.     infile>>num;
  17.     outfile<<"Number:               "<<num<<endl;
  18.  
  19.  
  20.  
  21. while (num>0)
  22. digit=num%10;
  23. num=num/10;
  24.  
  25. if(digit>0)
  26.     num=num%10;
  27.   num=num/10;
  28.   sum++;
  29.  
  30. }
  31.     outfile<<"Number in reverse Order:"<<num<<endl;       
  32.  
  33.     outfile<<"Summation of Digits:   "<<sum<<endl;
  34.  
  35.  
  36.  
  37.     infile.close();
  38.     outfile.close();
  39.  
  40.     return 0;                           
  41. }
  42.  
can any body help me???
Jul 30 '07 #1
6 1276
sicarie
4,677 Recognized Expert Moderator Specialist
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 .
Jul 30 '07 #2
sicarie
4,677 Recognized Expert Moderator Specialist
What is going on here?
Expand|Select|Wrap|Line Numbers
  1.  
  2. while (num>0)
  3. digit=num%10;
  4. num=num/10;
  5.  
  6. if(digit>0)
  7.     num=num%10;
  8.   num=num/10;
  9.   sum++;
  10.  
  11. }
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?
Jul 30 '07 #3
JosAH
11,448 Recognized Expert MVP
Expand|Select|Wrap|Line Numbers
  1. while (num>0)
  2. digit=num%10;
  3. num=num/10;
  4.  
  5. if(digit>0)
  6.     num=num%10;
  7.   num=num/10;
  8.   sum++;
  9.  
  10. }
  11.  
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.
Jul 30 '07 #4
isabelle
14 New Member
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
Jul 30 '07 #5
JamC
8 New Member
How about something like...

Code removed per Posting Guidelines
Jul 30 '07 #6
isabelle
14 New Member
Thanks all..

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

Thanks again..
Jul 31 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2740
by: PHPkemon | last post by:
Hi there, A few weeks ago I made a post and got an answer which seemed very logical. Here's part of the post: PHPkemon wrote: > I think I've figured out how to do the main things like...
6
4304
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
5
2996
by: Bec | last post by:
I'm in desperate need of your help.. I need to build an access database and have NO idea how to do this.. Not even where to start.. It IS for school, and am not asking anyone to do my...
7
3250
by: tyler_durden | last post by:
thanks a lot for all your help..I'm really appreciated... with all the help I've been getting in forums I've been able to continue my program and it's almost done, but I'm having a big problem that...
2
2100
by: Erik | last post by:
Hi Everyone, I'm having real problems compiling some source for eVC4++. The errors I am getting are below: It all seems to be centred around winsock. If I move the afsock.h reference to before...
2
2114
by: Bsnpr8 | last post by:
I need help guys, i have to many stuff to do, because i am in my last 2 weeks of the university, my last assignment is to do a spell checker in C++, i have an idea but nothing is coming out. I really...
6
1623
by: HelpME | last post by:
I wrote a program in Vb.Net that was running fine. However I am unable to install it on a couple of machines. When i run it I get a windows error message that says My Project.exe has...
1
1905
by: ligong.yang | last post by:
Hi all, I got tortured by a very weird problem when I was using k. wilder's random generator class in my program. PS: wilder's generator class can be found at...
1
1901
by: ligong.yang | last post by:
Hi all, I got tortured by a very weird problem when I was using k. wilder's random generator class in my program. PS: wilder's generator class can be found at...
0
7220
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7479
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
410
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.