Bug at line 11 and 24

//Weird Bug (Single letter wont trigger bug BUT when you put multiple letter Bugs at l-24) this is line 11

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
  #include <iostream>
using namespace std;

int main(){
    cout << "Salary\n";
    cout << "";
    cout << "!=====ONLY WHO HAVE 5K SALARY UP TO 500K WILL BE PROCESSED!=====!\n";
    cout << "";
    char cnt = 'Y';
    while(cnt == 'y' || cnt == 'Y'){ //!!!Loop for whole program!!!
    cout << "Enter your name:"; //Weird Bug (Single letter wont trigger bug BUT when you put multiple letter Bugs at l-24)
    char name;
    cin >> name;
    char nct = 'N';
    cout << "Have you spelled your name correctly? Press Y for Yes Press N for No."; //!!!right name or not!!!
    cin >> nct;
    if(nct == 'N' || nct == 'n'){
        cout << "!!! SPELL IT RIGHT THIS TIME IDK HOW TO CODE IT AGAIN IT WONT CHECK AGAIN IF IT IS WRONG !!!\n";
        char name;
        cout << "Enter your name: ";
        cin >> name;
    }
    cout << "";
    cout << "Enter your salary: "; //Bug related at l-11
    double slr;
    while(!(cin >> slr)){
        cout << "Wrong Input. Try again:";
        cin.clear();
        cin.ignore(100,50);
    }
    if((slr >= 5000 ) && (slr <= 10000)){
        cout << "Employee Name: " << name << "\n";
        cout << "Salary: " << slr << "\n";
        cout << "Average Salary.";
        cout << "";
        cout << "Analyze another salary? [Y/N]";
        cin >> cnt;
    }else if((slr >= 10001) && (slr <= 30000)){
        cout << "Employee Name: " << name << "\n";
        cout << "Salary: " << slr << "\n";
        cout << "Medium Average Salary.";
        cout << "";
        cout << "Analyze another salary? [Y/N]";
        cin >> cnt;
    }else if((slr >= 30001) && (slr <= 80000)){
        cout << "Employee Name: " << name << "\n";
        cout << "Salary: " << slr << "\n";
        cout << "High Salary";
        cout << "";
        cout << "Analyze another salary? [Y/N]";
        cin >> cnt;
    }else if ((slr >= 80001) && (slr <= 150000)){
        cout << "Employee Name: " << name << "\n";
        cout << "Salary: " << slr << "\n";
        cout << "Extremely High Salary;";
        cout << "";
        cout << "Analyze another salary? [Y/N]";
        cin >> cnt;
    }else if ((slr >= 150001) && (slr <= 300000)){
        cout << "Employee Name: " << name << "\n";
        cout << "Salary: " << slr << "\n";
        cout << "!!!SUPER DUPER HIGH!!!";
        cout << "";
        cout << "Analyze another salary? [Y/N]";
        cin >> cnt;
    }else if ((slr >= 300001) && (slr <= 500000)){
        cout << "Employee Name: " << name << "\n";
        cout << "Salary: " << slr << "\n";
        cout << "You a Syndicate?";
        cout << "";
        cout << "Analyze another salary? [Y/N]";
        cin >> cnt;
    }else{
        cout << "TERMINATE PROGRAM.";
    }
    }
    

}
name should be a string.

Line 29:
1
2
// cin.ignore(100,50);
std::cin.ignore( 1000, '\n' ) ; 


Consider writing small functions instead of one gigantic main.

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

bool yes( const std::string& prompt ) {

    char ch ;
    std::cout << prompt << " (y/n): " ;
    std::cin >> ch ;
    return ch == 'Y' || ch == 'y' ;
}

std::string get_name()
{
    std::string name ;
    std::cout << "enter name? " ;
    while( name.empty() ) std::getline( std::cin, name ) ;
    if( yes( "name: '" + name + "' is this correct? ") ) return name ;
    else return get_name() ; // try again
}

double get_salary( double max_salary ) {

    std::cout << "enter salary (max " << max_salary << "): " ;
    double sal = 0 ;
    if( std::cin >> sal && sal > 0.0 && sal <= max_salary ) return sal ;

    std::cout << "invalid input. try again\n" ;
    std::cin.clear() ;
    std::cin.ignore( 1'000, '\n' ) ; // delimiter is new line
    return get_salary( max_salary ) ; // try again
}

int main() {

    std::cout << "salary analysis\n---------------\n\n" ;

    do {
        const std::string name = get_name() ;
        const double salary = get_salary( 5'00'000 ) ;

        std::cout << "name: " << name << "  salary: " << salary << " (" ;
        if( salary > 300'000 ) std::cout << "very high)\n" ;
        else if( salary > 150'000 ) std::cout << "high)\n" ;
        else if( salary > 80'000 ) std::cout << "moderate)\n" ;
        else if( salary > 30'000 ) std::cout << "barely tolerable)\n" ;
        else std::cout << "too low)\n" ;
    }
    while( yes( "\nanalyse another? " ) ) ;
} 
thanks i forgot about that
Topic archived. No new replies allowed.