Bubba ADD??

The professor was going over characters and strings in our last class and didn't explain to well what/how/why we are doing this program. I'm sure it's leading up to something more difficult.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;
ifstream inf("BubbaIN.txt");
ofstream out("Bubba.out");
/*PROGRAM INSTRUCTIONS
You are to write a program that will add two
numbers and print out the two input numbers and the results.

The format is one number per line.  You will have several pairs
of numbers to process.

Label your output.

Format of output:  When you print out your answers, make sure
the three numbers are right hand justified and do not print
any leading zeros, NO leading zeros on output.
*/
//Your program should handle up to 35 digit integer numbers.
const int MAX=36;

void ReadIt(int ary[], int& cnt)
{
    string num;
    cnt=0;
    while(inf >> num && cnt < MAX)
    {
        ary[cnt]=num;
        cnt++;
    }
}

void PrintIt(int ary[], int cnt)
{
    for(int i=0; i<cnt; i++)
    {
        out << ary[i] << "\n";
    }
}

int main()
{
    int Bubba[MAX],cnt;
    ReadIt(Bubba,cnt);
    PrintIt(Bubba,cnt);
    return 0;
}

I'm attempting one thing at a time. I know I've messed up just unsure of where, when I attempt to read in the values from the infile, it only prints 4 out of 14 numbers.
Last edited on
It sounds like you're supposed to read them as strings and write a routine to add the two "numbers" in string form.
Why am I having such an issue with reading in? I've tried different variations to read in. Either I get the first couple of numbers or I get nothing or I get a lot of junk.

I tried using "infile.get()-'0';" and it would print the first, junk, second, junk, third, junk. I've spoken with others in my class and they're all about as stuck as I.
Please don't make this thread go on as long as the last one.
READ THEM AS STRINGS.
PERIOD.
DO THE MATH ON THE STRINGS.
Your code has
1
2
3
string num;
...
ary[cnt]=num;

so you are trying to put a string (num) into an int array (ary[]).

Your code obviously doesn't compile, so I can't see how it would read or print anything.

Do you intend num to be an int or a string?

Please show your input file.
Last edited on
This is a simple exercise, as alluded to by dutch, in adding two numbers expressed as strings. ie

"123456789181723525345344262819993667"
+
"76768768678678798798792132143244434656757"
=
"767688 ... blah blah blah ... 4"

The first job is to write a simple .txt data file with two numbers as strings and read them in, including just printing them out.

We live in hope (unlikely as it is) that OP can do that in the next post.

Perhaps OP could even start by doing the sum on paper.

Now for the litany of excuses ...
@againtry,

I think this excuse should cover it all
CodeNovice01 wrote:

Just so you know, I have a brain injury from active service, so that can play a role at times.


That was in http://www.cplusplus.com/forum/beginner/267497/2/#msg1151509

I guess you either missed that or this just means you are anti-military.

So maybe he or she, my apologies not sure here, is not as quick as you are, so that makes it OK to put them down.

I give credit to CodeNovice for at least trying.

Andy
Hello CodeNovice01,

Your "ReadIt" function may or may not work depending on what the input file looks like. It really helps everyone to see the input and know what you are working with. It also helps to be using the same information.

This while loop: while(inf >> num && cnt < MAX) is known as formatted input and will read until it finds a white space or new line character whichever comes first. Also a character in the file that would not print on the screen may be causing a problem. It may also not even show up when you look at the file. Hard to say at this time.

If each number is on its own line then using "getline": while(getline(inf, num && cnt < MAX)) could be a better choice.

Andy
Well, Namby Pamby, you’ve managed again to to take another one of your flaccid shots and pulled out the military card to encourage another drawn out go nowhere self important attempt at ‘helping someone’ with your serial episode long dull and boring explanations most of which is condescendingly irrelevant.

OP obviously hasn’t moved forward since the last failed hand holding you perpetrated and you’re making sure it stays that way. If OP has got half a brain he’ll, no apology even remotely necessary, tell you to fuck off and let him have a go with the tips and encouragement he, still no apology needed, already has.


I thought the output was more difficult than that @againtry. I hope you just had a bad day!

The professor wanted the output to align right and each number be the addition of the numbers above
IE
11
+12
____
13

Certain things may be deemed easy, but this one was difficult for myself. I appreciate your intellect and ability to understand things at a faster rate.
It's not about my intellect or ability, or me having a bad day none of which is remarkable or relevant. You and Namby Pamby are the one's putting that sort of defeatism on the table. Ask yourself how much that rubbish advances your coding skill.

The problem is in fact easy, especially to get started on, just as I suggested to you, just as @dutch indicated.

You've decided not to do so and want to argue the point.

I'm not going to be brow-beaten by people like Namby Pamby who is doing you a great disservice in actually preventing you developing your skills as a programmer by being a proxy for what you should be doing.

I actually took the lead from @dutch and expanded on the string possibilities he mentioned and the 35 size of the integer to make clearer the problem as you describe.

Focussing on formatting the output is not even a secondary issue, it's not what you originally asked. You're dancing. The good side of the addition is maybe you're starting to move - maybe.

I dare you to take the first step of reading in two strings etc or wait for Namby to do it for you.
Who is Namby?
It's Mr Pamby, the least concern.
You can do better than pick the wrong part of the last sentence.

Where's your code to read in two strings from a text file?
(One cut and paste and two lines .txt file)

http://www.cplusplus.com/doc/tutorial/files/ and the third sample with a minor adaptation. You don't need getline() just read a string each time.

BTW Always benchmark your code as it develops with your prof/tutor
What do you mean by benchmark the code?

Here's where I am so far with the program, I don't know why this one is being an aggravating task.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;
ifstream infile;
ofstream outfile;

//Create array
void createary(int n1[], int &lstd) {
	char k;
	int i = 0;
	infile.get(k);
	while (k != '\n') {

		n1[i] = k - '0';
		cout << n1[i];
		infile.get(k);
		i++;
	}
	cout << endl;

	lstd = i - 1;
}

//Prints the array
void print(int n1[]) {
	for (int i = 0; i < 36; i++) {
		cout << n1[i];
	}
	cout << endl;
}

//Move array values to the right
void right(int n1[], int lstd) {
	int j = 35;
	for (int i = lstd; i >=0; i--) {
		n1[j] = n1[i];
		j--;
	}
	for (int k = j; k >= 0; k--) {
		n1[k] = 0;
	}
}

//Remove leading zero's
void remove0(int n1[]) {
	for (int i = 0; i < 36; i++) {
		if (n1[i] == '0') {
			n1[i] = ' ';
		}
	}
}

//Add the arrays and get the remainder
void add(int n1[], int n2[], int ans[]) {
	for (int j = 35; j >= 0; j--) {
		ans[j] = n1[j] + n2[j];
		ans[j] = ans[j] % 10;
		ans[j-1] = ans[j] / 10;
	}
}


int main() {
	int n1[36];
	int n2[36];
	int ans[36];
	char k;
	int i = 0;
	int lstd;

	infile.open("BubbaIN.txt");

	createary(n1, lstd);
	createary(n2, lstd);

	right(n1, lstd);
	right(n2,lstd);

	add(n1, n2, ans);
	//remove0(n1);
	//remove0(n2);

	print(n1);
	print(n2);
	print(ans);


	return 0;
}

INPUT FILE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
78
39
51023
58009
38727731237456793
894123
11111111111111111111111111111111111
91111111111111111111111111111111111
9999999999999999999999999999999999
1999999999999999999999999999999999
10000000000000000000000000000000002
10000000000000000000000000000000001
57657657657657657657657657657656756
444444444444444444444444444
What do you mean by benchmark the code?

You want to make sure you're going along the right track as far as what the problem requires you to solve.

I don't know why this one is being an aggravating task
I do, and I think a few other people here do as well. Just making excuses pisses ppl (me too) off.

Anyway, moving on. If you actually wrote the code then I am impressed, even surprised.

So, forgetting about the fine points of leading zeroes etc. You now have to implement add with carry. So your function needs a carryover variable in it.

You'll notice that 78 + 39 is not 07 it is 117. You should still do the pencil and paper exercise. Maybe just mental addition.

Start with zero carry over
9 + 8 + (CO = 0) = 17 -> 0007, CO = 1
7 + 3 + (CO = 1) = 11 -> 0017, CO = 1
0 + 0 + (CO = 1) = 1 -> 0117, CO = 0 etc

Follow that pattern for all 35 digits and there's the total before post processing.

Let's see you do that.

BTW You're better off reading the numbers in as strings.
Last edited on
Topic archived. No new replies allowed.