need help.

I have to make a lil program that does the following:
- Asks user to input a 10 digit number
- Checks that the inputted number is 10 digits no characters (numbers only)
- Extracts the first digit, 2nd and 3rd digits and the 4th to be used in an if statement.
Example: 4th digit can be either 1 or 0 (if there is a way to make the program only allow 1 or 0 that is great) if it's 1 then it will cout male, if yes then female.
ty

almost everything has worked (Thanks a lot Mochops) however the only thing that did not go well is extracting the 4th digit alone.
Given a string containing many digits (as characters), here is how to get the 4th digit (as a character).

char digit = theString[3];
1
2
3
4
5
6
7
8
char digit = ID[3];
  
  if(digit == 1)
  cout << "ID belongs to a male";
  else if (digit == 0)
  cout << "ID belongs to a female";
  else
  cout << "Invalid ID";

what's wrong with this?
if I do cout << digit; it returns 1 however the if function returns invalid of female doesn't return male.
the if function returns invalid of female doesn't return male.
No idea what this means.

1 is the number 1. '1' is the character '1'. Since you're working with characters, you should be comparing with characters; not numbers.
Last edited on
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
#include <iostream>
#include <sstream>

using namespace std;

int main()
{
    int number;
    startAgain:
    ostringstream convert;

    cout << "enter a number 10 digits long" << endl;
    cout << "4th number must be 0(female) or 1(male)" << endl;
    cin >> number;
    int store = 0;
    if(number < 10000000000 && number > 1000000000){

         store = number;
         cout << "the number you entered is :" << store << endl;

    }else{

        cout << "invalid number \n";
        goto startAgain;
    }

   string result;
   convert << store;
   result = convert.str();

   char check = result.at(3);
   if(check == '1'){

      cout << "male";
   }

   if(check == '0'){

    cout << "female";
   }
}


not too sure what you want this program to do but heres what I came up with it,look over it hope it helps
Topic archived. No new replies allowed.