Hi. Can anyone help me get started with the following program please? I know the concept but i am having problem writing the syntax codes.
I know we have to use arrays and then split the array and convert it into string and then back to integer. I believe we also have to use a loop. But i am not able to write the codes for the program. Please please help me.

Write a program that will calculate the under-10 total of a number of integers. The following example will illustrate

the process:

We need to get the under-10 total of 93, 42 and 33.

- Add the individual digits of the first integer together (thus 9+3 = 12)

- If the total is greater than 9 (which it is in this case), repeat the process (thus 1+2=3) until the total is less

than 10. This is the first running total.

- Then repeat the process for the second integer (thus 4+2 = 6).

- Now add the under-10 total of the first integer to that of the second integer and repeat the process if the

running total is greater than 9 (thus 3+6=9).

- Get the under-10 of the third integer (thus 3+3=6) and add to the previous running total and repeat the

process to get a total less than 10 (thus 9+6=15; 1+5=6).

- The under-10 total of the three integers 93, 42 and 33 is therefore equal to 6.

Create an input file called input.dat that contains the following set of integers:

39 88 55 21 90 77 24 67 76 87 35 79 45 93

You can assume that there will be at least 2 integers in the file and that all integers in the file will have two

individual digits. Your program needs to read one integer at a time from the file, and then use the operators of C++

(e.g. +, - , /, %, which ever is applicable) to separate the digits and do the calculations. Write the running totals to an

output file called under10.dat. The last integer in the output file will therefore be the under-10 total of all the

integers in the input file.

Hint: If you divide a two-digit integer by 10, the result will be the first digit, e.g. 28 / 10 = 2. If you use the modulus

operator (%) on a two-digit integer and the value 10, the result will be the second digit, e.g. 28 % 10 = 8. You

now have the digits 2 and 8 separated, and can add them together. The sum in this example is 10, which is

more than 9, so we repeat the process. 10 / 10 = 1, giving the first digit, and10 % 10 = 0, giving the second

digit. Now 1 + 0 = 1, which is a valid under-10 total.

For the above input file the output file should be:

3 1 2 5 5 1 7 2 6 3 2 9 9 3

You must at least try on your own
6 Years
Discussion Span
Last Post by dusktreader

Why do you want to convert the numbers to string first, and then back to integer?
I guess you want to do this so that from "93" you can get the 9 and 3 seperately and do the calculations... but that would actually prove that you haven't even read the assignment completely.

So... please read the assignment, and give it your best shot(you should be able to at least start) and then post again with what you have tried, and a _detailed_ explanation of where you are stuck.


You don't need to make them a string you could just use %10 and /10 to figure out the 1s and 10s digit then add those 2 numbers together to check.

Edit: Didn't even read the whole post until after mine and it says right in there how to do it.

Edited by sfuo: n/a

Good job quoting his assignment.

my problem in fact is that i am understanding the concept a bit but getting difficulty to write the code since i don't have any reference or book with me..and its the first time i am doing programming.
And deadline is close..i still need to wait for my book to reach me.


I find it useful to see source code and read over it trying to figure out what is going on. I would recommend taking this and playing around with it to see how stuff works.

Also a website that I find very useful is http://www.cplusplus.com/doc/tutorial/. It goes over lots of the basics and there is also a reference section that goes over STL and many other things.

#include <iostream>
#include <fstream>
using namespace std;

int main()
	int num, total = 0;
	ifstream in("input.dat"); //open "intput.dat" for reading
	ofstream out("output.dat"); //open "output.dat" for writing
	while(in >> num) //while there is still information in the file to be read
		if( num%10 + num/10 >= 10 ) //sum of the 2 digits is less than 10
			num = num%10 + num/10; //split and add

		total += num;
		if( total%10 + total/10 >= 10 ) //sum of the 2 digits is less than 10
			total = total%10 + total/10; //split and add

		out << (total%10 + total/10) << " "; //output the result of total
	out.close(); //close after use
	in.close(); //close after use

	return 0;

Edited by sfuo: n/a


The purpose of homework is to learn how to solve problems similar to the ones you will face in your career. You must try to do your own homework. When you have struggled through some code and feel hopelessly stuck, post specifically what you are trying to do with your code and the code you have tried. People on this forum will then try to explain the underlying concepts and provide abstract examples. No one on this forum should supply solution code to you.

Please, please get the text book. If you can't get it, scour the internet for information. There is so much information about programming online. You could go from caveman to Linus Torvalds by simply using the internet IF YOU ARE WILLING TO INVEST THE EFFORT.

Now, go forth and code.

Come back when you have been defeated. We will pick you up, dust you off, and give you strategies to continue the fight.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.