I've not added any features yet in this program and only added some tasks in "1)Create an account" while in this code block when you enter your name after pressing enter, it starts looping infnitely even after using "break;" it won't stop. here's the code
#include <iostream>
#include <stdlib.h>
using namespace std;
int login_as;
int i = 3;
int std_choice;
int std_deposit;
void student() {
char std_name;
int std_age;
int s = 6;
while (s != std_choice) {
cout << "1) Create an account\n2) View balance\n3)Purchase a Book\n4)Rent a Book\n5)Return a book\n6) Login Screen" << endl;
cin >> std_choice;
switch (std_choice)
{
case 1:
cout << "REGISTER!" << endl;
cout << "Enter Name:\n" << endl; //after this line it starts looping and wont break//
cin >> std_name;
cout << "Age: \n" << endl;
cin >> std_age;
cout << "Please deposit 10$ as initial deposit!\n1)Yes\n2)No" << endl;
cout << "curl qreno.de/https://www.google.com" << endl;
cin >> std_deposit;
break;
default:
break;
}
}
}
int main() {
while (i != login_as ){
cout << "Login as:\n1) User\n2) Librarian\n3) Exit" << endl;
cin >> login_as;
student();
}
return 0;
}
I'm avoiding asking chatgpt or phind as it would straight forwardly show me the solution, i want it to see from the human perspective. I was expecting it would print the statements one by one after entering the input
you have used a char
datatype for the variable std_name
which is used to store the name the user inputs. cin >> std_name
could break your code if you enter more than one character (as mentioned by @Eljay), since your trying to store multiple characters in a variable that is meant to store a single character.
To fix this, change the datatype of std_name
to std::string
e.g.
string std_name;