This is code I need Condition when user keypress wrong enters examples: Entered
ID: 3767644 • Letter: T
Question
This is code I need Condition when user keypress wrong enters
examples:
Entered wrong choose menu
Entered string for phone
Entered mistake emails
.
.
....etc
I need improvement this code for possible choose users input
Example:
Valid input in main menu numbers 1 or 2 not alphabet not other numbers Sub main menu the same things above when user input 1 opened sub menu admin when choose another numbers for sub admin or inputs alphabets "that 's wrong please enter valid values" ....etc - Enhancement this code and explain the comments
Thanks a lot ,,
-------------------------------------------------------------------------------------
#include <iostream>
#include <string>
using namespace std;
//structure defined
struct Student
{
std::string name;
std::string address;
std::string phone;
std::string email;
std::string id;
};
int main()
{
//declared array of structs
Student * newJoinee = new Student[100];
int count = 0;
while(true){
//displaying menu
cout<<"Welcome to CoC If you are the admin, please press 1. If you are an applicant, please press 2. Press 0 to Exit.";
int choice;
cout<<" Please enter your choice: ";
cin>>choice;
//id admin
if(choice == 1){
while(true){
cout<<" You are a system admin";
cout<<" 1- Enter a new student. 2- Delete a student. 3- Send a message to a student. 4- Back to the main menu.";
int adminChoice;
cin>>adminChoice;
cout<<"Your option: "<<adminChoice;
//creating new student data
if(adminChoice == 1){
cout<<" Please enter student information: ";
string name,address,phone,email,studentId;
//reading data from user
cout<<" Enter name: ";
cin>>name;
cout<<" Address: ";
cin>>address;
cout<<" phone: ";
cin>>phone;
cout<<" E-mail: ";
cin>>email;
cout<<" Student ID: ";
cin>>studentId;
int b = atoi(studentId.c_str());
if (isdigit(b))
cout<<"correct";
newJoinee[count].name = name;
newJoinee[count].address = address;
newJoinee[count].phone = phone;
newJoinee[count].email = email;
newJoinee[count].id = studentId;
count++;
//student created
cout<<" Information has been saved successfully.";
}
else if(adminChoice == 2){ //deteting existing student data
string id;
int temp=0;
cout<<" Enter id to delete";
cin>>id ;
for(int i=0;i<count;i++){
if(newJoinee[i].id == id){
newJoinee[i].name = "";
newJoinee[i].address = "";
newJoinee[i].phone = "";
newJoinee[i].email = "";
newJoinee[i].id = "";
temp=1;
}
}
if(temp == 0){
cout<<" Sorry, the student with this number can’t be found.";
}
}
else if(adminChoice == 3){ //reading message
cout<<" Enter message: ";
string msg;
cin>>msg;
}
else{ //exiting
break;
}
}
}
//printing existing student details
else if(choice == 2){
cout<<"]nYou are applicant: ";
string name;
cout<<" Enter name: ";
cin>>name;
int temp=0;
for(int i=0;i<count;i++){
if(newJoinee[i].name == name){
cout<<" "<<newJoinee[i].name;
cout<<" "<<newJoinee[i].address;
cout<<" "<<newJoinee[i].phone;
cout<<" "<<newJoinee[i].email;
cout<<" "<<newJoinee[i].id;
temp=1;
}
}
if(temp == 0){
cout<<" Sorry, we can’t find your name in the database, please check the spelling or contact the student affair in CoC.";
}
}
else{ //exiting from app
break;
}
}
return 0;
}
Explanation / Answer
/*
* 728.cpp
*
* Created on: Dec 3, 2015
* Author: aravindv
*/
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
//structure defined
struct Student {
std::string name;
std::string address;
std::string phone;
std::string email;
std::string id;
};
bool is_valid_email(string email) {
if (isalpha(email[0]) == 0) //email should start with a alphabet
return 0;
int at = -1;
int dot = -1;
for (int i = 0; i < email.length(); i++) {
if (email[i] == '@') // found @
at = i;
else if (email[i] == '.') // found .
dot = i;
}
if (at == -1 || dot == -1) // email should contain atleast one @ and one .
return 0;
if (at > dot) // @ should come before .
return 0;
return !(dot >= (email.length() - 1)); //Chech there is some other letters after the Dot
}
int main() {
//declared array of structs
Student * newJoinee = new Student[100];
int count = 0;
while (true) {
//displaying menu
cout<< "Welcome to CoC If you are the admin, please press 1. If you are an applicant, please press 2. Press 0 to Exit.";
int choice;
cout << " Please enter your choice: ";
cin >> choice;
//id admin
if (choice == 1) {
while (true) {
cout << " You are a system admin";
cout<< " 1- Enter a new student. 2- Delete a student. 3- Send a message to a student. 4- Back to the main menu.";
int adminChoice;
cin >> adminChoice;
cout << "Your option: " << adminChoice;
//creating new student data
if (adminChoice == 1) {
cout << " Please enter student information: ";
string name, address, phone, email, studentId;
int error;
//reading data from user
cout << " Enter name: ";
cin >> name;
cout << " Address: ";
cin >> address;
do {
cout << " phone: ";
cin >> phone;
error = 0;
for (int i = 0; i < phone.length(); i++) {
if (phone[i] < '0' || phone[i] > '9') {
error = 1;
cout<< "Please enter a valid phone number which contains only numberic values ";
break;
}
}
} while (error == 1);
do {
cout << " E-mail: ";
cin >> email;
if (is_valid_email(email))
break;
else {
cout<< "enter a valid email-id ex: abcd@efg.com ";
}
} while (1);
cout << " Student ID: ";
cin >> studentId;
int b = atoi(studentId.c_str());
if (isdigit(b))
cout << "correct";
newJoinee[count].name = name;
newJoinee[count].address = address;
newJoinee[count].phone = phone;
newJoinee[count].email = email;
newJoinee[count].id = studentId;
count++;
//student created
cout << " Information has been saved successfully.";
} else if (adminChoice == 2) { //deteting existing student data
string id;
int temp = 0;
cout << " Enter id to delete";
cin >> id;
for (int i = 0; i < count; i++) {
if (newJoinee[i].id == id) {
newJoinee[i].name = "";
newJoinee[i].address = "";
newJoinee[i].phone = "";
newJoinee[i].email = "";
newJoinee[i].id = "";
temp = 1;
}
}
if (temp == 0) {
cout<< " Sorry, the student with this number can’t be found.";
}
} else if (adminChoice == 3) { //reading message
cout << " Enter message: ";
string msg;
cin >> msg;
} else if (adminChoice == 4) { //exiting
break;
} else {
cout << "enter a valid option ";
}
}
}
//printing existing student details
else if (choice == 2) {
cout << "]nYou are applicant: ";
string name;
cout << " Enter name: ";
cin >> name;
int temp = 0;
for (int i = 0; i < count; i++) {
if (newJoinee[i].name == name) {
cout << " " << newJoinee[i].name;
cout << " " << newJoinee[i].address;
cout << " " << newJoinee[i].phone;
cout << " " << newJoinee[i].email;
cout << " " << newJoinee[i].id;
temp = 1;
}
}
if (temp == 0) {
cout<< " Sorry, we can’t find your name in the database, please check the spelling or contact the student affair in CoC.";
}
} else if (choice == 0) { //exiting from app
break;
} else {
cout << "enter a correct choice ";
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.