problem with if condition

this is my code, i am having trouble with my if conditions at the end to make sure that if sum of n1 and n2 is greater than 20 digits it says its invalid. the way i have it now it just gives me invalid right away.

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
92
93
94
95
96
97
98
99
100
101
102
//programmed by hamzah khachiche
#include <iostream>
using namespace std;
#include <string>
#include <cstring>

void read(string n1, string n2, int num1[20], int num2[20], int& a, int& c);
void total(string n1, string n2, int num1[20], int num2[20], int sum[21], int a, int c);

int main()
{
	string n1, n2;
	int a, c;
	int num1[20];
	int num2[20];
	int sum[21];
	read(n1, n2, num1, num2, a, c);
	total(n1, n2, num1, num2, sum, a, c);
	
	return 0;
}

//my function to input the integer and then store it into the array
void read(string n1, string n2, int num1[20], int num2[20], int& a, int& c)
{
	
	
	cout << "Enter the first postive integer of at least 20 digits: " << endl;     //where you enter the first integer
	cin >> n1;
	while (n1.length() > 20)  //this is where you input an integer of 20 digits if you do more than 20 it is invalid and must be redone
	{
		cout << "this is invalid please enter a positive integer of at least 20 digits: " << endl;  
		cin >> n1;
	}
	cout << endl;
	


	cout << "Enter the second postive integer of at least 20 digits: " << endl;  //where you input the second integer
	cin >> n2;
	while (n2.length() > 20)  ////this is where you input an integer of 20 digits if you do more than 20 it is invalid and must be redone
	{
		cout << "this is invalid please enter a positive integer of at least 20 digits: " << endl;
		cin >> n2;
	}
	cout << endl;
	

	int j = 0;  //this is where we are a doing a loop to read the numbers in reverse
	for (int i = n1.length() - 1; i >= 0; i--)
	{
		num1[j] = static_cast<int>(n1[i]) - static_cast<int>('0');  //this then stores the integer into an array
		j++;

	
	}
	a = atoi(n1.c_str()); //this is where i convert the string into an integer

	int k = 0;
	for (int b = n2.length() - 1; b >= 0; b--) //this is the loop for the second set of numbers to store them into reverse
	{
		num2[k] = static_cast<int>(n2[b]) - static_cast<int>('0');  //this stores the second set into an array
		k++;
		
	}
	c = atoi(n2.c_str()); //this is where i convert the second set into an integer
	
}

//this function is where i add the two positive integers
void total(string n1, string n2, int num1[20], int num2[20], int sum[21], int a, int c)
{
	//this first for loop is to add my first set of integers
	int i;
	for (i = 0; i < n1.length(); i++)
	{	
	}
	
	cout << endl;
	//this second for loop is for the second set of integers
	int b;
	for (b = 0; b < n2.length(); b++)
	{ }
	
	int d;
	int h = 2147483647; 
	d = a + c; //this is to add the two integers that where converted into strings
	int q;
	q = (n1.length() + n2.length());
	//these are my if condition statements where i make sure the sum is not greater than h and the sum isnt more than 20 digits
	//this is the part not working, trying to make sure not greater than 20 digits
	if (d > q) //this condition is to make sure the sum of the digits is not greater than 20
	{
		cout << "This is invalid" << endl;
	}
	else if (d > h) //this condition is to make the sum is not greater than 2147483647
		cout << "This is invalid" << endl;
	else
		cout << "the total of " << a << " and " << c << " is: " << d << endl;
	
	
}
Last edited on
You're comparing the sum of two integers d = (a + c) and comparing it with
the length of n1 + n2. I've added comments to the appropriate code areas below. (Might be a good idea to format your code using code tags when posting :) )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
a = atoi(n1.c_str()); //this is where i convert the string into an integer

c = atoi(n2.c_str()); //this is where i convert the second set into an integer


//(Okay, here you're adding the sum of two integer VALUES)
d = a + c; //this is to add the two integers that where converted into strings

//Here, you're getting the number of characters in each string and summing 
//them
q = (n1.length() + n2.length());

//Now, you're comparing a value against the number of chars in each string
//the result will most likely always be false
if (d > q) 


Let me know if you need more help,

Joe
sparkprogrammer@gmail.com
hi joe,
thanks for answering, and this is my first time using a forum so i didn't know how to make it look neat.

I see what you mean and why it wasn't working, but how can i fix it where i can write a condition that will check and make sure that if the total digits is greater than 20 it will not work but if 20 or less it will work?
thanks for answering, and this is my first time using a forum so i didn't know how to make it look neat.

http://www.cplusplus.com/articles/jEywvCM9/
Topic archived. No new replies allowed.