divisible by 11, could use some help.

Hello all! First time posting here, my friend just recently recommended it.
First semester c++ course and we are on do/while and for loops.

here is the question (i've seen it posted here before but its slightly different in this question):

let n = ak ak-1 ak-2....a1 a0 be an integer and t = a0 - a1 a2 - ... +(-1)k ak. it is known that n is divisible by 11 if and only if t is divisible by 11. For example, suppose that n = 8784204. Then, t = 4 - 0 + 2 - 4 + 8 - 7 +8 = 11. Because 11 is divisible by 11, 8784204 must be divisible by 11. Write a C++ program that prompts the user to enter a positive integer and then uses this criteron to determine whether the number is divisible by 11.

Hint: you need the following functions to solve the program. (1) the string length function to determine the size of the string of digits. (2) the string at function to extract each of the digits (in character form) (3) the type cast function to convert each digit from char to int. (4) subtract 48 from the converted digit (e.g. '4' = 52 in ASCII) to get the digit in decimal form.

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

#include <string>
#include <iostream>

using namespace std;

int main()
{
	int decision = 1, input, endCalc;
	string inputChar;
	
	while (decision != 0)
	{
		cout << "Enter a positive integer" << endl;
		cin >> inputChar;
		input = inputChar.length();

		do
		{
			char x = inputChar.at(input - 1);            
			int y = (x - 48);						      
			input--;
		} while (input > 0);

		cout << "Would you like to continue? (0 to exit, 1 to continue)" << endl;
		cin >> decision;
	}

}




this is as far as I have gotten. I simply do not understand how to institute a loop that will subtract... then add... etc. those digits.
I feel a for-loop with multiple declared variables would be better than a do-while loop in this situation. That way you'd be able to create a 'sign' variable (probably of bool type because you have a binary either-or option) that you could then test conditionally to see if you're adding or subtracting.

There are other things about this code that need to be fixed, but that's certainly the first thing to be addressed before continuation.
off topic, but:
Hello all! First time posting here, my friend just recently recommended it.
First semester c++ course and we are on do/while and for loops.

there is a c++ skype group called c++ benders (iirc) and there is also irc.quakenet.org #cplusplus
cout << "Enter a positive integer" << endl;
cin >> inputChar;
input = inputChar.length();

for (int i = input - 1; i > 0; i--)
{
char x = inputChar.at(i);
int y = (x - 48);
}


something along those lines?
Better; the most appropriate loop is now being used. Also, you're condition is wrong in your for statement. You're going to want to be able to access the 0th element of your string (as that is the first digit of the number), so i > 0 should actually be i >= 0. Now expanded a bit with the bits you'll need.
1
2
3
4
5
6
7
8
9
10
11
12
bool positive = true;
int digit_sum = 0;
for (int i = input - 1; i >= 0; i--)
{
    char x = inputChar.at(i);
    int y = (x - 48);
    //check to see if y is going to be positive or negative based on positive variable
    //change sign of y if necessary
    //add correctly signed y to digit_sum
   ///change value of positive
}
//check to see if digt_sum is divisible by 11 
i think i get where you are going

the only thing i dont understand is how do i check to see if y is going to be positive or negative. the first loop gives a positive.. then what?

something like
if (i % 2 != 0)
{
dig_sum = dig_sum + y
}
else
dig_sum = dig_sum - y;


?
btw thank you so much for the help!
YOU MAGNIFICENT BASTARD!

I GOT IT!


thank you so much!!!!!!!!!!!!!! just needed some nudges.

Take advantage of using a bool variable. If it is true, then the next number you're adding to digit_sum is going to be positive. If it is false, the next number you're adding is going to be negative.

So something like:
1
2
if ( positive ) dig_sum += y;
else dig_sum -= y;

Checking the i variable is also dangerous in this case because you don't know whether the last number in inputted number is going to have a positive or negative index.
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
#include < string > ;
#include <iostream>;

using namespace std;

int main()
{
	int decision = 1, input;
	string inputChar;

	while (decision != 0)
	{
		cout << "Enter a positive integer" << endl;
		cin >> inputChar;
		input = inputChar.length();

		int digit_sum = 0;
		for (int i = input - 1; i >= 0; i--)
		{
			char x = inputChar.at(i);
			int y = (x - 48);

			if (i % 2 != 1)
			{
				digit_sum = digit_sum + y;
			}
			else
				digit_sum = digit_sum - y;
		}

		if (digit_sum % 11 != 0)
			cout << "This integer is not divisible by 11" << endl;
		else
			cout << "This integer is divisible by 11" << endl;
		cout << "Would you like to continue? (0 to exit, 1 to continue)" << endl;
		cin >> decision;
	}
}







just to follow up, checking the i variable allows me to figuratively set the sign. because of the way the code is written (using the string function) i = input - 1 will always send the first loop through the else statment and switching each time.
Actually, you are correct there. I was thinking the first digit had to be positive, but so long as you alternate signs, the final sum will be divisible be 11 either way. Good job. :P
Topic archived. No new replies allowed.