codes infinite loop

my code bugs when i put a long name at line 180

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
  #include <iostream>
using namespace std;

class calculator{
    public:
    double n[2];

    void alldata(double num1, double num2){
        n[0] = num1;
        n[1] = num2;
    }

    double addition(){
        return n[0] + n[1];
    }

    double substraction(){
        return n[0] - n[1];
    }

    double division(){
        return n[0] / n[1];
    }

    double multiplication(){
        return n[0] * n[1];
    }
};

class gwacal{
    public:
    double science,math,history,english,filipino;

    void allgrades(double g1, double g2, double g3, double g4, double g5 ){
        science = g1;
        math = g2;
        history = g3;
        english = g4;
        filipino = g5;
    }

    double gradesres(){
        return (science + math + history + english + filipino) / 5;
    }
};

class database{
    private:
    int choice;
    string acc,pass;

    public:
    void menu();
    void addingacc();
    void viewacc();
};

void database::menu(){
    //Authentication
    string logging = "gabbz29";
    int pass = 387025;
    cout << "Enter your account name: ";
    cin >> logging;
    cout << "Enter your password: ";
    cin >> pass;
    while(!(logging == "gabbz29" || pass == 387025)){
        cout << "\nCheck the information that you provided and try again.\n";
        cin.clear();
        cin.ignore(100 , '\n');
        cout << "Enter your account name: ";
        cin >> logging;
        cout << "Enter your password: ";
        cin >> pass;
    }

    //Database menu
    cout << "--------------Database Menu--------------\n";
    cout << "1.Add an account.\n";
    cout << "2.View your accounts.\n";
    cout << "3.Delete an existing account.\n";
    cout << "4.Modify an account.\n";
    cout << "5.Exit.\n";
    cout << "6.Go back to menu.\n";
    cout << "Press your transaction: ";
    cin >> choice;
    while(!(choice == 1 || choice == 2 || choice == 3 || choice == 4 || choice == 5 || choice == 6)){
        cout << "Only choose between number's 1-6.\n";
        cout << "Try again: ";
        cin.clear();
        cin.ignore(100 , '\n');
        cin >> choice;
    }

    if(choice == 1){
        addingacc();
    }else if(choice == 2){
        viewacc();
    }else if(choice == 5){
        exit;
    }
}

void database::addingacc(){
    cout << "----------------Adding section----------------\n";
    cout << "E-mail/G-gmail/Username: ";
    cin >> acc;
    cout << "Enter password: ";
    cin >> pass;
}

void database::viewacc(){
        
}

int main(){
    char opt;
    char chc;
    do{
    cout << "---------------------------------------------ALL IN ONE SOFTWARE---------------------------------------------\n";
    cout << "A.Account saving.\n";
    cout << "B.Calcaulator.\n";
    cout << "C.General Weighted Average Calculator.\n";
    cin >> chc;
    
    if(chc == 'A' || chc == 'a'){ 
    database alldata;
    alldata.menu();
    }else if(chc == 'b' || chc == 'B'){
        do{
        double num[2];
        char op;
        cout << "---------------------AIO Calculator---------------------\n";
        cout << "Choose an operator [+ / * / (/) / -]: ";
        cin >> op;
        while(!(op == '+' || op == '*' || op == '/' || op == '-')){
            cout << "Choose only betweeen the operators available.\n";
            cin.clear();
            cin.ignore(100 , '\n');
            cout << "Choose an operator [+ / * / (/) / -]: ";
            cin >> op;
        }

        cout << "Enter two digits: ";
        while(!(cin >> num[0] >> num [1])){
            cout << "The only valid input is a number. Please try again.\n";
            cin.clear();
            cin.ignore(100 , '\n');
            cout << "Enter two digits: ";
            cin >> num[0] >> num [1];
        }
        calculator cal;
        cal.alldata(num[0],num[1]);

        if(op == '+'){
            cout << num[0] << " + " << num[1] << " = " << cal.addition() << '\n';
        }else if(op == '*'){
            cout << num[0] << " * " << num[1] << " = " << cal.multiplication() << '\n';
        }else if(op == '/'){
            cout << num[0] << " / " << num[1] << " = " << cal.division() << '\n';
        }else if(op == '-'){
            cout << num[0] << " - " << num[1] << " = " << cal.substraction() << '\n';
        }

        cout  << "Use it again or go back to main Menu?\n [Y] to use it again\n [N] to exit\n [C] to go back to Main menu.";
        cin >> opt;
        while(!(opt == 'y' || opt == 'Y' || opt == 'n' || opt == 'N' || opt == 'c' || opt == 'C')){
            cout << "Only choose between [Y/N/C]\n";
            cin.clear();
            cin.ignore(100 , '\n');
            cout << "Y = continue. N = exit. C = Back to main menu: ";
            cin >> opt;
        }
        }while(opt == 'y' || opt == 'Y');
    }else if(chc == 'c' || chc == 'C'){
        string name,sect;
        double sc,mt,hst,eng,fil;
        do{
        cout << "-----------------General Weighted Average-----------------\n";
        cout << "Enter the your name: ";
        cin >> name;
        cout << "Enter your section: ";
        cin >> sect;
        cout << "-----------------SUBJECTS-----------------\n";
        cout << "Science: ";
        cin >> sc;
        cout << "Math: ";
        cin >> mt;
        cout << "History: ";
        cin >> hst;
        cout << "English: ";
        cin >> eng;
        cout << "Filipino: ";
        cin >> fil;

        gwacal grades;
        grades.allgrades(sc, mt, hst, eng, fil);

        cout << name << '\n';
        cout << sect << '\n';
        cout << "List of grades\n";
        cout << "Science: " << sc << '\n';
        cout << "Math: " << mt << '\n';;
        cout << "History: " << hst << '\n';;
        cout << "English: " << eng << '\n';;
        cout << "Filipino: " << fil << '\n';;
        cout << "Overall grade: " << grades.gradesres() << '\n';

        cout << "Make another transaction?\n Press [Y] to redo.\n Press [N] to exit the program.\n Press [C] to go back to main menu.\n";
        cout << "Choice? ";
        cin >> opt;
        if(opt == 'n' || opt == 'N'){
            cout << "Program terminated.";
            exit(0);
        }
        }while(opt == 'y' || opt == 'Y');
    }
    }while(opt == 'c' || opt == 'C');
}
@gabzz29

Well, I see a couple of problems. One, you don't have #include <string> and two, you didn't declare the variable string name.
whitenite1 wrote:
you didn't declare the variable string name

Look at line 175.

gabzz29 wrote:
my code bugs when i put a long name at line 180

Are you sure this doesn't only happen when you enter multiple words?

Note that the >> operator only reads the next word. If you want to read the whole line you can use std::getline instead.

But if you start using std::getline you need to be aware that:
• >> stops as soon as it has read the word/value and leaves everything that comes after, including the newline character '\n' that marks the end of the line, still in the stream for future read operations to read.
• std::getline stops reading as soon as it finds a newline character.

This means that if you use >> to read a value on one line and then tries to use std::getline to read the next line it won't work as intended because the >> operator leaves the newline character inside the stream so the call to std::getline will just stop immediately when it finds that newline without reading a second line.

Example
1
2
3
4
int num;
std::cin >> num;
std::string line;
std::getline(std::cin, line);
If the user types 158 and press enter then std::cin will contain the content "158\n" ('\n' is the newline character).
The >> operator on line 2 will then read the integer 158 and store it inside the num variable. This leaves std::cin with the content "\n" (i.e. only a newline character).
std::getline on line 4 then finds the newline and returns immediately leaving the line string variable empty.

See an earlier post of mine for ways you can handle this: https://cplusplus.com/forum/beginner/282767/#msg1224018
Last edited on
it worked but at line 205 and 206 it only shows the first name not the whole name
Line 205 and 206 in the code above prints the filipino and average grade...

Are you perhaps talking about your updated code that you haven't shown us?
Last edited on
Topic archived. No new replies allowed.