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.. -
#include <iostream>
-
#include<fstream>
-
using namespace std;
-
-
int main()
-
{
-
-
int num,digit,sum=0;
-
-
ifstream infile;
-
ofstream outfile;
-
-
infile.open("inData.txt");
-
outfile.open("outData.txt");
-
-
infile>>num;
-
outfile<<"Number: "<<num<<endl;
-
-
-
-
while (num>0)
-
{
-
digit=num%10;
-
num=num/10;
-
-
if(digit>0)
-
num=num%10;
-
num=num/10;
-
sum++;
-
-
}
-
outfile<<"Number in reverse Order:"<<num<<endl;
-
-
outfile<<"Summation of Digits: "<<sum<<endl;
-
-
-
-
infile.close();
-
outfile.close();
-
-
return 0;
-
}
-
can any body help me??? 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 .
sicarie 4,677
Recognized Expert Moderator Specialist
What is going on here? -
-
while (num>0)
-
{
-
digit=num%10;
-
num=num/10;
-
-
if(digit>0)
-
num=num%10;
-
num=num/10;
-
sum++;
-
-
}
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?
JosAH 11,448
Recognized Expert MVP -
while (num>0)
-
{
-
digit=num%10;
-
num=num/10;
-
-
if(digit>0)
-
num=num%10;
-
num=num/10;
-
sum++;
-
-
}
-
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.
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
How about something like...
Code removed per Posting Guidelines
Thanks all..
and; if any one have another indentation for this, I will be grateful.
Thanks again..
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |