C++ Code help

Ok I got this so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cmath>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
  int n = 1234;
  int d1 = n % 10; n = n / 10;
  int d2 = n % 10; n = n / 10;
  int d3 = n % 10;
  int d4 = n / 10;

  cout << d1 << d2 << d3 << d4;

  int n2 = d1 + d2 + d3 + d4;
  cout << n;
  cout << n2;
  cout << n + n2;
  cout << endl;
  return 0;
}


The hard part is I have to

.read the number from a file
.compute the last four digits of the number
.compute the reversed number from the last four digits of the number

Can anyone help me out with this, Thanks a bunch.
So what you're trying to learn is how to read the number in from a file, am I right? If so, this tutorial should help you out a lot (it helped me when I was still learning file I/O): http://www.cplusplus.com/doc/tutorial/files/

Also, something looks a little bit awkward about your reversing algorithm if that's what it is...

-Albatross
So, you store each digit as its own int, that much looks like it works. However, line 15 ruins all that, you add them.

http://www.northstarmath.com/sitemap/ExpandedForm.html
Yes Albatross you're right. I looked over the tutorial and I still don't get how to word it, I was hoping someone could lead me the right way...
Okay. I'll give you a nudge in the right direction.

1
2
3
4
5
6
7
8
ifstream in;
in.open("file-to-open.txt");
if (/*the opening was successful*/) {
    //Read.
    //Close the file.
} else {
   //Complain.
}


You should be able to figure out the missing bits from the tutorial, specifically from the fourth example onward. Good luck!

-Albatross
Topic archived. No new replies allowed.