IF command help

Hello, this is my first consultation ever on c++. :p I'm self learner so it's very hard to figure my code problems out without any consultation. ;/ I hope you will help me a bit. :)

My question is on IF function. If you can see on my code I'm writing simple program which will give me on the end of the program, all values that user wrote in. But there is one problem. When user writes name, last name, and when he needs to write MAN/WOMAN (even he writes correctly) it goes to another IF function as so on until all program executes on 3th IF function. I don't know what I'am doing wrong. I tried to use ("return char;") or ("return sex;") on the end of every function but it gave me just error.

I want to make that when they input wrong value (MAN/WOMAN) it would give them another hint till 2-3 (maybe I need loop?) and then execute program if they writes it again in wrong value.

Thank you very much for your answers!

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
#include <iostream>
#include <unistd.h>
#include <stdlib.h>

using namespace std;

void sleep(){
    usleep(3000000);
}

/* INT value
Name: n
Last Name: ln
Gender: sex
Age: a
Birth: b
Birth Place: bp
Favorite color: fc
*/

int main(){
    int a;
    char sex[5];
    char n[20];
    char ln[40];
    char b[10];
    char bp[35];
    char fc[15];

    //int n
    cout << "Name: ";
    cin >> n;
    cout << "\n";

    //int ln
    cout << "Last Name: ";
    cin >> ln;
    cout << "\n";

    //int sex
    cout << "Gender(MAN/WOMAN): ";
    cin >> sex;
    cout << "\n";


    if(sex != "MAN" || sex != "WOMAN"){
        cout << "Wrong input value!(MAN/WOMAN)" << endl;
        cout << "\nGender(MAN/WOMAN): ";
        cin >> sex;
    }
        if(sex!="MAN" || sex!="WOMAN" || sex!="Man" || sex!="Woman" || sex!="man" || sex!="woman"){
            cout << "Too many wrong values!" << endl;
            cout << "\nError: Gender(MAN/WOMAN)";
            sleep();
            system("cls");
            cout << "Gender(MAN/WOMAN): ";
            cin >> sex;
        }
            if(sex!="MAN" || sex!="WOMAN"){
                return EXIT_FAILURE;
            }

    //int a
    cout << "Age: ";
    cin >> a;
    if(a<1, a>100){
        cout << "\nWrong input value!(1-100)" << endl;
        cout << "\nAge: ";
        cin >> a;
        cout << "\n";
    }

    //int b
    cout << "Your birth date(year/month/day): ";
    cin >> b;

    //int bp
    cout << "\nBirth place: ";
    cin >> bp;

    //int fc
    cout << "\nFavorite color: ";
    cin >> fc;

    //RESULT
    cout << "Name: " << n << endl;
    cout << "Last Name: " << ln << endl;
    cout << "Gender: " << sex << endl;
    cout << "Age: " << a << endl;
    cout << "Birth: " << b << endl;
    cout << "Birth place" << bp << endl;
    cout << "Favorite color" << fc << endl;

    return EXIT_SUCCESS;
}
if(sex != "MAN" || sex != "WOMAN")

You need to use strcmp to compare char arrays.
http://www.cplusplus.com/reference/cstring/strcmp/

if(sex!="MAN" || sex!="WOMAN" || sex!="Man" || sex!="Woman" || sex!="man" || sex!="woman")

It would be better to convert it to lower or upper case first and then again use strcmp or use stricmp
https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_73/rtref/stricmp.htm
closed account (21vXjE8b)
Also, line 66 is wrong. Use if(a<1 || a>100) instead of if(a<1, a>100).
Last edited on
The problem is that you are trying to compare strings using != or ==, that is not possible.

The problem is that "char sex[5];" does not just create a variable it creates an array of in this case 5 variables and "sex" becomes a pointer to the first of those variables. As you probably already know a pointer is just a value that says where in the memory something is stored. Let's assume the pointer value is 42.
Now you try to compare the strings using:
 
if(sex != "MAN" || sex != "WOMAN")

We said that sex has the value 42, so we know that one.
"MAN" is a new string, so a new array is created, the characters are placed in it and the pointer value is for example 69.
This means that your if-statement is actually
 
if (42 != 69 || something) 

42 can not ever be 69, because the memory at location 42 is reserved for the "sex" string and thus was not available for the nameless "MAN" string.

Have a look here and you will find how to compare the text of two strings, probably you will be able to fix your program yourself then.
http://www.cplusplus.com/reference/cstring/strcmp/

Kind regards, Nico
Last edited on
Thank you for answers!

But I have few questions more. When I input all informations I get on result nothing, probably I'm writing bad code? Or bad format?

And one more question :P In the RESULT I can't use int a. Any ideas?

<code>#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <strings.h>

using std::cout;

void sleep(){
usleep(3);
}

void exitF(){
printf("Too many incorrect values!\n");
printf("Program is shutting off...\n");
system("PAUSE");
}

/* INT value
Name: n
Last Name: ln
Gender: sex
Age: a
Birth: b
Birth Place: bp
Favorite color: fc
*/

int main(){
int a;
char sex[] = "MAN" "WOMAN" "Man" "Woman" "man" "woman";
char buffer[10];
char n[20];
char ln[40];
char b[10];
char bp[35];
char fc[15];

//int n
printf("Name: ");
scanf("%s", n);
printf("\n");

//int ln
printf("Last Name: ");
scanf("%s",ln);
printf("\n");

//int sex
printf("Gender(MAN/WOMAN): ");
fflush (stdout);
scanf("%9s", buffer);
printf("\n");


if(strcmp(sex,buffer) != 1){
printf("Wrong input value!(MAN/WOMAN)\n");
printf("\nGender(MAN/WOMAN): ");
scanf("%9s",buffer);
}
if(strcmp(sex,buffer) != 1){
printf("Too many wrong values!\n");
printf("\nError: Gender(MAN/WOMAN)");
sleep();
system("cls");
printf("Gender(MAN/WOMAN): ");
scanf("%9s",buffer);
}
if(strcmp(sex,buffer) != 1){
return EXIT_FAILURE;
}

//int a
printf("Age: ");
scanf("%i", &a);
if(a<1 || a>100){
printf("\nWrong input value!(1-100)\n");
printf("\nAge: ");
scanf("%i", &a);
printf("\n");
}
if(a<1 || a>100){
exitF();
}

//int b
printf("Your birth date(year/month/day): ");
scanf("%s", b);

//int bp
printf("\nBirth place: ");
scanf("%s", bp);

//int fc
printf("\nFavorite color: ");
scanf("%s", fc);

//RESULT
system("cls");
vprintf("Name: ", n);
printf("\n");
vprintf("Last Name: ", ln);
printf("\n");
vprintf("Gender: ", sex);
printf("\n");
printf("Age: "); // can't use printf("Age: ", a); it's because i want to use int a, but how I should do that?
printf("\n");
vprintf("Birth: ", b);
printf("\n");
vprintf("Birth place", bp);
printf("\n");
vprintf("Favorite color", fc);

return EXIT_SUCCESS;
}
</code>
Last edited on
Topic archived. No new replies allowed.