Help with 1 part of my code

My code works except it's behaving differently than I'd like. the Auth() part of my code has a weird behavior. Also, when I enter user credentials, it says "Pick between Calculator and Beer" twice. Perhaps it's due to the && condition? Not sure why. I'm quite new to all this. The code comes from chapter 6 practice problems of Jumping into C++, I just consolidated all of the code into one part.
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
  #include <iostream>

using namespace std;
int Auth();
void Calculator();
void beer();
double add(double x,double y)
{
    return x+y;
}
double sub(double x,double y)
{
    return x-y;
}
double mult(double x,double y)
{
    return x*y;
}
double div(double x,double y)
{
    return x/y;
}
string username, password;
int main()
{
    Auth();
    string choice;
    while(choice !="Calculator"&&choice !="Beer")
    {
        cout<<"Pick between Calculator and Beer."<<endl;
        getline(cin,choice,'\n');
    }
    if(choice =="Calculator")
    {
        Calculator();
    }
    if(choice =="Beer")
    {
        beer();
    }
}
int Auth()
{
    cout<<"Enter username: ";
    cin>>username;
    cout<<"Enter password: ";
    cin>>password;
    if((username=="Colin") && (password=="jasmine69420"))
    {
        cout<<"Welcome to the program!"<<endl;
    }
    else
    {
        return 0;
    }
}
void Calculator()
{
    double x,y;
    string operation;
    cout<<"Select either +, -, *, /"<<endl;
    cin>>operation;
    cout<<"Pick number 1: ";
    cin>>x;
    cout<<"Pick number 2: ";
    cin>>y;
    if(operation=="+")
    {
        cout<<"The result is: "<<add(x,y)<<endl;
    }
    if(operation=="-")
    {
        cout<<"The result is: "<<sub(x,y)<<endl;
    }
    if(operation=="*")
    {
        cout<<"The result is: "<<mult(x,y)<<endl;
    }
    if(operation=="/")
    {
        cout<<"The result is: "<<div(x,y)<<endl;
    }
}
void beer()
{
    for(double bottle=99; bottle > 0; bottle--)
    {
        cout << bottle << " bottles of beer on the wall, " << bottle << " bottles of beer." << endl <<
                        "Take one down, pass it around, " << bottle - 1 << " bottles of beer on the wall...\n";
    }
}
Last edited on
closed account (48bpfSEw)
int Auth() returns 0 only in the else branch
else it returns randomly any number


One of the debugging techniques is to separat the buggy part of the code and test it for its own:

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
#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

int main()
{
    string choice;
    while(choice !="Calculator"&&choice !="Beer")
    {
        cout<<"Pick between Calculator and Beer."<<endl;
        getline(cin,choice,'\n');
    }
    if(choice =="Calculator")
    {
        cout << "Calculator selected!" << endl;
    }
    if(choice =="Beer")
    {
       cout << "Beer selected!" << endl;
    }
    
    return 0;
}



works fine!
Last edited on
When you enter the password and press return in Auth(), the program reads the password but leaves the newline character in the input buffer. Then at line 30, getline() reads the old newline character and returns an empty string, which causes the loop to execute again.

Since you expect the username and password to be on separate lines, I'd use getline() to get them. This will let a password contain a space, and will consume the newline too.
Topic archived. No new replies allowed.