assigning variables

"If the input is: 611408
Then, Leo would have eaten 6 pounds of meat and 11 bones. Una would have eaten 4 pounds of meat
and 8 bones."
How do I make a program that can do that to a 6 digit integer??
Convert the integer to a string, parse the string into the 4 constituent parts needed as new strings. The pattern is 1 digit, 2 digits, 1 digits and 2 digits. Then convert the 4 strings back into integers.

Or parse the full string into substrings with the same pattern and wave your wand to get 4 integers.

There are other ways to do it.

They all require a bit of time and thought to designing the program before code is written.
The mathy way to do it is to use / and % operators.

611408 / 1000 = 611
611408 % 1000 = 408

611 / 100 = 6
611 % 100 = 11

408 / 100 = 4
408 % 100 = 8
the program needs to work where any given 6-digit integer ABBCDD will output as leo ate A pounds of meat and BB bones and una ate C pounds of meat and DD bones
Yes - that's what Ganado above described using simple arithmetic.

What don't you understand about this? / is integer division, % is modulo (division remaining).

Do you know how to obtain an integer in code? Display numbers?
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string input;
   cout << "Enter a six-digit string: ";   cin >> input;
   if ( input.size() != 6 ) cout << "Can't you count?\n";
   else cout << "Leo ate " << input[0] << " pounds of meat and " << stod( input.substr( 1,2 ) ) << " bones and Una ate " << input[3] << " pounds of meat and " << stod( input.substr( 4,2 ) ) << " bones\n";
}


Cannibals?
Topic archived. No new replies allowed.