Code works on Linux but not on Windows OS

Hi,
I have been wondering why the code below is giving me different output on Windows OS. Meanwhile, the same code works well on Ubuntu.

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
#include <iostream>
#include <cstring>

using namespace std;

string getname(void);
char getgender(string);
void setinfo(string,char);

int main()
{
    string customer = "";
    char gender;
    do {
        do{
            customer = getname();
        }while(customer =="");
        do{
            gender = getgender(customer);
            cout << "Gender in Main block is: " << gender << endl;
        }while(gender != 'm' && gender != 'f');
        setinfo(customer,gender);
        break;
    }while(1);
    return 0;
}

string getname(void)
{
    string name = "";
    cout <<"Please Enter your Name: " ;
    getline(cin,name);
    if(name == ""){
        cout << "Error: Name cannot be empty" <<  endl;
    }
    return name;
}

char getgender(string name)
{
    char gender;
    string strgender;
    cout << "Male / Female[m/f]: ";
    getline( cin,strgender);
    gender = strgender[0];
    strgender.clear();
    gender = tolower(gender);
    if(gender == 'm' || gender == 'f'){
        cout << "Gender in getgender() is: " << gender << endl;
        return gender;
    }
    else
        cout <<"Error: Must be m/ f" << endl;
}

void setinfo(string customer, char gender)
{
    string prefix;
    if(gender == 'm')
        prefix =  "Mr.";
    else
        prefix = "Ms/Mrs.";
    cout << "Hello " << prefix << " " << customer << endl;
    cout << "Welcome to TOWER HEIGHT" << endl;
}

On my Ubuntu machine, this code works fine. But when I compile and run it on Windows, I get different output and it keeps looping at getgender().



Please Enter your Name: My Name
Male / Female[m/f]: m
Gender in getgender() is: m
Gender in Main block is: ╕

Male / Female[m/f]: f
Gender in getgender() is: f
Gender in Main block is: ╕

Male / Female[m/f]:




Thank you for your time.
Adja
Interesting.

Try to directly change "gender" in main by
getgender(string, char&); //put gender as the second parameter, and in getgender to assign the result directly to the reference.

Take a look what will happen.

Thanks for the reply. I am yet to get to pointers and references so can you please give example of how to put gender as the second parameter.
I was taught functions so I decided to create a program using it.

I enabled warnings on my linux box using -Wall compiler flag and got this message:

1
2
chartest.cpp: In function ‘char getgender(std::string)’:
chartest.cpp:54: warning: control reaches end of non-void function


so I added return 0 after the else statement and it works well now.

I will try on how to change gender and see what will happen when I get to pointers.



Thank you very much for your help
Last edited on
Good on you.

In declaration,
char getgender(string, char&);

Then, In main, call it like this
getgender(customer, gender);

In the function, use the input "gender" parameter like a normal local variable.

Google it, you can find many examples about reference. In C++, try to more use reference than pointer.
Last edited on
Topic archived. No new replies allowed.