User integer input & selection

How can I have my program count the number of characters in the string that is being entered and then add either 10 or 11 to the character count depending on whether or not the count is odd or even.

This is the Question:

1) Prompt the user for their first name, family, number of siblings and their lucky whole number.
- The password creation is a 4 step process:
Step 1: At the end of this step you will have a 2-digit number which you will use in step 2: if the number of letters in the first name is even, then the resulting 2-digit number is the sum (10+number of letters) otherwise it is (11+number of letters).



1
2
3
4
5
6
7
8
9
10
11
 string FirstName;
    cout<< "Please enter your First Name:";
    cin>> FirstName;
 
    
    if (FirstName){
        FirstName = FirstName.length() + 10;
    } else {
        FirstName = FirstName.length() + 11;
    };
    
How can I have my program count the number of characters in the string that is being entered and then add either 10 or 11 to the character count depending on whether or not the count is odd or even.

The same way you do everything else when coding. Slowly, in simple steps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int number_of_characters_in_string = FirstName.length();

bool number_of_characters_in_string_is_even;
if ( (number_of_characters_in_string % 2)  == 0)
{
  number_of_characters_in_string_is_even = true;
}
else
{
  number_of_characters_in_string_is_even = false;
}

int two_digit_number;
if ( number_of_characters_in_string_is_even == true)
{
  two_digit_number = FirstName.length() + 10;
}
else
{
  two_digit_number = FirstName.length() + 11;
}
When I execute this there's an error in the last if statement somewhere, it doesn't add the 10 or 11, it only adds 10? not sure why the else isn't being used
there's an error in the last if statement somewhere

Repeater’s code seems to work pretty fine to me:
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
#include <iostream>
#include <limits>
#include <string>

void waitForEnter();

int main()
{
    std::cout << "What's your name? ";
    std::string FirstName;
    std::getline(std::cin, FirstName);
    int number_of_characters_in_string = FirstName.length();
    bool number_of_characters_in_string_is_even;
    std::cout << "There are " << number_of_characters_in_string 
              << " characters in your name, " << FirstName << '\n';
    if ( (number_of_characters_in_string % 2)  == 0)
    {
        number_of_characters_in_string_is_even = true;
    }
    else
    {
        number_of_characters_in_string_is_even = false;
    }

    int two_digit_number;
    if ( number_of_characters_in_string_is_even == true)
    {
        two_digit_number = FirstName.length() + 10;
    }
    else
    {
        two_digit_number = FirstName.length() + 11;
    }
    std::cout << "two_digit_number: " << two_digit_number << '\n';
    waitForEnter();
    return 0;   
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Output:
What's your name? John
There are 4 characters in your name, John
two_digit_number: 14

Press ENTER to continue...

What's your name? Ann
There are 3 characters in your name, Ann
two_digit_number: 14

Press ENTER to continue...

What’s your issue?
Topic archived. No new replies allowed.