Read string from console along with whitespaces - c++

I want to read a string from cin until the return key is pressed. What's wrong with the following code? The output repeatedly says "Invalid response found, please enter again: " even when i enter single words.. I've updated the code..
int readInt() {
int num;
while (!(std::cin >> num)) {
std::cout << "Invalid response found, please enter again: ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
return num;
}
string readString() {
string str;
while (std::getline(std::cin, str) && std::cin.fail()) {
std::cout << "Invalid response found, please enter again: ";
std::cin.clear();
std::cin.ignore();
}
return str;
}
DVD_Node* addNewDvd(DVD_Node *head) {
DVD_Node* temp = new DVD_Node;
int new_id = getNewID(head);
temp->id = new_id;
cout << "\n\n-- Add a new DVD to your collection --";
cout << "\n\nEnter movie name: ";
temp->title = readString();
cout << "\n\nEnter duration in minutes: ";
temp->duration = readInt();
cout << "\n\nEnter movie year: ";
temp->year = readInt();
cout << "\n\nEnter movie actor 1 name: ";
temp->actor1 = readString();
cout << "\n\nEnter movie actor 2 name: ";
temp->actor2 = readString();
temp->next = head;
changes_remaining = 1;
cout << "\n\nYour DVD Data has been added successfully.";
return temp;
}

The below test is not correct if you want to detect error :
std::getline(std::cin, str) && std::cin.fail()
Indeed, because getline return the input, you want to detect null input or errors with :
std::getline(std::cin, str) == 0 || std::cin.fail()

Related

data validation in c++ for integer input

void addItem(struct InventoryItem inventory[], int& size){
cout << "\nEnter the item name: ";
cin.getline(inventory[size].itemName, 100, '\n');
while (strlen(inventory[size].itemName) == 0){
cout << "Invalid item name!" << endl;
cout << "Enter item name: ";
cin.getline(inventory[size].itemName, MAX_CHAR, '\n');
}
cout << "Enter the item price: ";
cin >> inventory[size].itemPrice;
while(!cin || inventory[size].itemPrice < 0){
cout << "Please enter a valid number: ";
cin >> inventory[size].itemPrice;
clear();
}
cout << endl;
size++;
}
My !cin keeps accepting letters as numbers ...
any ideas why?
note: I'm new to programming.
If operator>> fails to extract a value, it puts the input stream into an error state. You are not clearing that state, or ignoring the bad input that caused it to fail, so operator>> keeps failing.
Try this instead:
void addItem(struct InventoryItem inventory[], int& size){
do {
cout << "Enter the item name: ";
if (cin.getline(inventory[size].itemName, MAX_CHAR, '\n')) {
if (strlen(inventory[size].itemName) > 0) {
break;
}
}
else {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "Invalid item name!" << endl;
}
while (true);
do {
cout << "Enter the item price: ";
if (cin >> inventory[size].itemPrice) {
if (inventory[size].itemPrice >= 0) {
break;
}
}
else {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "Invalid item price!" << endl;
}
while (true);
cout << endl;
size++;
}

Checking integer for input validation of a node

I'm trying to do my input validation where I have already predefined data in my program. When I want the user to key in a new employee id, I want it to check whether that ID is already in the database, if yes, how do i keep looping it until the user types in an id which is not in the database?
struct employee {
int empNo, salary, performance;
string name, phoneNo, address, depNo, depName;
employee* next;
employee* previous;
} *temp, *head, *tail, *newhead, *newtail, *newnode;
void validate()
{
cout << "Input not accepted, please try again!" << endl;
cin.clear();
cin.ignore(INT_MAX, '\n');
}
int main()
{
int choice = 1;
int num, salary, performance = 0;
string name, hpnum, depName, depNo, address;
bool execute = true;
insertemployeerecord(0002, "Ethan Tan", 16000, "017-2399193", "Kuala Lumpur, Malaysia", "F001", "Finance", 2);
insertemployeerecord(0003, "Nikshaen Kumar", 15000, "016-9188131", "Los Angeles, California", "A001", "Audit", 3);
insertemployeerecord(0001, "Koughen Mogan", 17500, "014-1233241", "Los Angeles, California", "F001", "Finance", 4);
while (execute)
{
cout << "..........................................................." << endl;
cout << " EMERGE EMPLOYEE SYSTEM " << endl;
cout << "..........................................................." << endl;
cout << endl;
cout << "1. Add an Employee Record" << endl;
cout << "2. Display All Records" << endl;
cout << "3. Search by Employee ID" << endl;
cout << "4. Search by Employee overall performance" << endl;
cout << "5. Sort and display by Employee ID in ascending order" << endl;
cout << "6. Sort and display by Employee Salary in ascending order " << endl;
cout << "7. Sort and display by Employee Overall Performance in ascending order " << endl;
cout << "8. Modify an Employee Record" << endl;
cout << "9. Delete an Employee Record" << endl;
cout << "10. Calculate salary package of an employee" << endl;
cout << "11. Exit" << endl;
cout << endl << endl;
cout << "Select your option: ";
cin >> choice;
if (choice == 1)
{
system("cls");
cout << "Enter employee number: ";
cin >> num;
while (!cin >> num) //to see if user types anything besides number
{
validate();
cout << "Enter employee number: ";
cin >> num;
}
temp = head;
bool verify = true;
if (temp != NULL)
{
while (temp->empNo != num)
{
temp = temp->next;
if (temp == NULL)
{
verify = false;
break;
}
}
while (verify == true) //if the user types an id that is found in the database
{
validate();
cout << "Employee found in database!" << endl;
cout << "Enter employee number: " << endl;
cin >> num;
}
if (verify == false)
{
cin.get();
}
}
cout << endl;
cout << "Enter employee name: ";
getline(cin, name);
As you can see, my output can detect if there is an alphabet being typed and also if the user types an id thats already in the database, but when I type a new id, example being 4, the system still says it detects from the database
Enter employee number: a
Input not accepted, please try again!
Enter employee number: 1
Input not accepted, please try again!
Employee found in database!
Enter employee number:
2
Input not accepted, please try again!
Employee found in database!
Enter employee number:
3
Input not accepted, please try again!
Employee found in database!
Enter employee number:
4
Input not accepted, please try again!
Employee found in database!
You can do something like this :
int enter_employee_number() {
int number;
do{
cout << "Enter employee number: ";
cin >> number;
bool fail = cin.fail();
if(fail)
cout << "Input not accepted, please try again!" << endl;
} while (fail);
return number;
}
string enter_employee_name() {
string name;
do{
cout << "Enter employee name: ";
cin >> name;
bool fail = cin.fail();
if(fail)
cout << "Input not accepted, please try again!" << endl;
} while (fail);
return name;
}
employee get_employee(int number) {
// retrieve your employee and return it.
}
int main() {
// other part of your code
if (choice == 1) {
system("cls");
while(true) {
int empNo = enter_employee_number();
employee emp = get_employee();
if (emp != nullptr) {
cout << "Employee found in database!" << endl;
string empName = enter_employee_name();
// call method that use empName
}
else {
cout << "Employee not found in database!" << endl;
}
cout << endl;
}
}
}
Recommended way of input validation is via getting everything in a string, like define string inputLine and use std::getline() and then parsing it using std::istringstream(inputLine). If you want to continue till validation succeeds, put below code in loop.
if ( stringStream >> ID >> std::ws && stringStream.get() == EOF)
//At this point you have Employee Id in ID variable , Now you can do database check.
if(validate(ID)) // if validation succeeds.
break; // Break out of loop, otherwise continue till you get correct input.

The while loop keeps executing no matter whatever the second user input is within the second case of the switch

The code has been updated in order to solve a different section of the whole picture within the coding process. Now I need help within the second case of the switch. The problem now is that the while loop always executes within the second function of the switch. I don't no if the array is verifying the number or the user input.. I could use while (string::npos != studID2[i].find_first_of(studID2[a])) I need some help here it is getting very complex and I am hitting a brick wall.
1)I need to verify each user input using a for loop and two arrays. I tried to increment the of the arrays in order to execute the while statement.
2) If the condition is true the while loop will execute telling the user that he must enter 3 different digits, the reason why I am using an array and a for loop is because the user gets to choose how many names and IDs he would like to input into the archive.
3) The for loop increments a++ in order to check to see if the last input is the same as the newest user input.
It is getting too complex here any help would be appreciated.
Number 4 is the expected error...
4) The second user input will always make the while loop run regardless of what digits you use.
5)The reason for so much code is because I am not completely sure where the problem begins and the problem ends...
6)I am using two arrays here in this problem.
7)There is another error if you change the a from 0 to 1 it will automatically close the program. The reason you would change the a to a 1 is so that a will increment by 1.
//I am trying to verify multiple inputs with a array and a for loop..
// The expected output is that the second ID you input in the second option of the switch case is going to execute the while loop.
//I am trying to execute the while loop properly within the second function of the switch case.
//I need help any form of help can be appreciated.
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include <cstddef>
#include <limits>
#include <cstdlib>
int name2 = 0;
int studentID = 0;
int email2 = 0;
int studentID2[100];
std::string let("abcdefghijklmnopqrstuvwxyz");
int numbers = (1, 3, 0);
std::string str;
std::string name;
std::string studID;
std::string studID2;
std::string email;
std::string emailbackup;
std::string studFinal;
std::stringstream concatenate;
std::stringstream concatenatetwo;
int x;
std::string fileName = "StudentArchive.txt";
void emailEntry();
void readDocument();
int readNumber();
void deleteInformation();
void getStudentinformation();
using std::cout;
using std::cin;
int main()
{
do {
std::cout << "What would you like to do?" << std::endl;
std::cout << "1)Would you like to see the archive?" << std::endl;
std::cout << "2)Would you like to register student information?" << std::endl;
std::cout << "3)Would you like to find a student within the registry?" << std::endl;
std::cout << "4)Delete all student information?" << std::endl;
std::cout << "5)Exit Program?" << std::endl;
std::cin >> x;
switch (x)
{
case 1:
readDocument();
break;
case 2:
emailEntry();
break;
case 3:
deleteInformation();
break;
case 4:
getStudentinformation();
break;
case 5:
cout << "Exiting Program." << std::endl;
system("PAUSE");
break;
}
} while (x != 5);
}
void emailEntry()
{
std::ofstream outfile;
outfile.open(fileName, std::ios_base::app);
int amountofStudent;
std::cout << "How many student Identities would you like to enter?" << std::endl;
std::cin >> amountofStudent;
cin.ignore();
Here is where the user chooses how many students he would like to enter into the registry. I am having difficulty verifying the user input regarding the studentIDs.
if (outfile.is_open()) {
for (int i = 0; i < amountofStudent; i++)
{
std::string studID2[100];
std::stringstream(name) >> name2;
cout << "Please enter your name.." << std::endl;
getline(cin, name);
outfile << name;
std::stringstream(name2) >> name;
while (std::string::npos != name.find_first_of("0123456789"))
{
cout << "You must have letter within user input." << std::endl;
cout << "Please enter your name." << std::endl;
getline(cin, name);
outfile << name;
}
//I need to check to see if the first 3 numbers are correct?
//The student ID must be at least 6 digits, and the first 3 numbers must be 130.
cout << "Please enter Your student I.D." << std::endl;
getline(cin, studID);
outfile << studID;
std::stringstream(studID) >> studentID;
while (/*std::string::npos != studID.find_first_of("130") */ studentID != 130 /*&& studID.length() <= 6*/)
{
std::stringstream(studentID) >> studID;
cout << "You must enter 130 as the first 3 digits" << std::endl;
getline(cin, studID);
std::stringstream(studID) >> studentID;
}
//==============
//std::stringstream(studentID2) >> studID2[i];
cout << "Please enter the second part of the student I.D. " << studentID << "-" << std::endl;
getline(cin, studID2[i]);
outfile << studID;
//outfile << studID2[i];
std::stringstream(studID2[i]) >> studentID2[i];
//cout << i;
This is the for loop, and array I need help with. Below this text is where I am having problems. I don't understand why the while loop won't execute I am trying to verify the first user input with the next user input. For example if the user enters 888 on the first input then tries to enter 888 on the second input they need to re-enter different digits or the input will go on forever. The main struggle is if the user chooses to enter multiple student accounts within this minor registry.
for (int a = 0; a < i; i++)
{
while (studID2[i] == studID2[a])
{
cout << "The numbers cannot be repeated you must re-enter the student ID." << std::endl;
std::stringstream(studentID) >> studID;
cout << "You must enter 130 as the first 3 digits" << std::endl;
getline(cin, studID);
std::stringstream(studID[i]) >> studentID;
//std::stringstream(studID2[i]) >> studentID2;
cout << "Please enter the second part of the student I.D. " << studentID << "-" << std::endl;
getline(cin, studID2[i]);
outfile << studID;
outfile << studID2[i];
//std::stringstream(studID2[i]) >> studentID2;
}
}
This is where the verification of the studentIDs end...
while (/*std::string::npos != studID.find_first_of("130") */ studID2[i].length() < 3 || studID2[i].length() > 3)
{
//stringstream(studentID) >> studID;
cout << "Add 3 more digits." << std::endl;
getline(cin, studID2[i]);
outfile << studID2[i];
}
concatenate << studentID << "-" << studID2 << std::endl;
studFinal = concatenate.str();
/*while (studID.length() != 6)
{
cout << "You must enter 130 as the first 3 digits and you must have 6 digits." << std::endl;
std::cin >> studID;
}*/
cout << "Please enter your email.." << std::endl;
std::stringstream(email) >> email2;
getline(cin, email);
outfile << email;
std::stringstream(email2) >> email;
while (email == emailbackup || email.empty())
{
cout << "Please enter your email..." << std::endl;
std::stringstream(email) >> email2;
getline(cin, email);
outfile << email;
std::stringstream(email2) >> email;
}
concatenatetwo << email << "#atlanticu.edu" << std::endl;
email = concatenatetwo.str();
emailbackup = email;
cout << "Your email is" << email << std::endl;
std::system("pause");
}
}
outfile.close();
}
Here is where the user deletes info..
void deleteInformation()
{
std::ofstream infile(fileName, std::ios::trunc);
if (infile.is_open()) {
cout << "You have now have no books." << std::endl;
system("PAUSE");
system("cls");
infile.close();
}
}
void getStudentinformation()
{
std::ifstream outfile;
outfile.open(fileName);
if(outfile.is_open())
{
int x;
cout << "1)Name" << std::endl;
cout << "2)studentNumber" << std::endl;
cout << "3)Exit" << std::endl;
cin >> x;
switch (x)
{
case 1:
cout << "Please enter the student's name.." << std::endl;
getline(cin, name);
cout << name << std::endl;
outfile >> name;
break;
case 2:
cout << "Please enter the first 3 digits of the student's ID number.. " << std::endl;
getline(cin, studID);
cout << "Please enter the last 3 digits of the student's ID number.. " << std::endl;
getline(cin, studID2);
outfile >> studID;
outfile >> studID2;
break;
case 3:
std::string choice;
cout << "Would you like to return to the main menus?" << std::endl;
cin >> choice;
break;
}
}
}
int readNumber()
{
int number;
cin >> number;
std::string tmp;
while (cin.fail())
{
cin.clear();
std::getline(cin, tmp);
cout << "Only numbers please: ";
cin >> number;
}
getline(cin, tmp);
return number;
}
Here is where the user reads the txt doc.
void readDocument()
{
std::ifstream infile;
infile.open(fileName);
if (infile.is_open()) {
std::string info;
while (getline(infile, info))
{
cout << info << std::endl;
}
infile.close();
}
else {
cout << fileName << " doesn't exists !" << std::endl;
}
std::system("PAUSE");
std::system("cls");
}
//std::ofstream outfile;
//outfile.open(fileName, ios_base::app);
//if(outfile.is_open()) {
// cout << "How many books do you want to add: ";
// int n = readNumber();
// while (n <= 0)
// {
// cout << "Only positive numbers, please: ";
// n = readNumber();
// }
// for (int i = 0; i < n; i++) {
// string title = "Book's Title: ";
// cout << title;
// title += readText();
// string author = "Author: ";
// cout << author;
// author += readText();
// string genre = "Book's Genre: ";
// cout << genre;
// genre += readText();
// cout << endl;
// outfile << title << endl;
// outfile << author << endl;
// outfile << genre << endl << endl;
// }
//}
// outfile.close();
// cout << "Books have been added to the library !" << endl;
// system("PAUSE");
// system("cls");
Think about your function signature.
void email(string emailbackup);
Do you actually want the caller to pass a string to the function email()?
Because you then define emailbackup inside the function again.
You want something like this probably...
...
case 2:
const string backup = "backup#email";
email(backup);
break;
...
void email(string emailbackup)
{
// string emailbackup; // delete this line
...
}
Also, remove using namespace std; and be consistent with your namespace usage. Why add using namespace cout, cin etc if you are calling them with the namespace explicitly i.e. std::cin, std::cout.
Either add using namespace std::cout and use cout rather than std::cout or vice versa. Don't mix it up. Same with std::string and string. Makes it look like you defined your own string...
Just one last thing, for the sake of readability I would suggest breaking this email function into smaller functions. For instance:
void email(string emailbackup)
{
...
int studentID = getStudentID();
string studentEmail = getStudentEmail();
...
}
int getStudentID()
{
int studentID;
cout << "Please enter Your student I.D." << std::endl;
std::cin >> studID;
stringstream(studID) >> studentID;
while (/*std::string::npos != studID.find_first_of("130") */ studentID != 130 /*&& studID.length() <= 6*/)
{
stringstream(studentID) >> studID;
cout << "You must enter 130 as the first 3 digits" << std::endl;
std::cin >> studID;
stringstream(studID) >> studentID;
}
return studentID;
}

How can I solve this pointer/memory issue?

I trying to write a program that asks the user for movie information. stores the information of a movie as a struct in a vector and then output the result to the screen with the 2 functions having a return type of void.
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
using namespace std;
void make_movie(struct movie *film);
void show_movie(vector <movie> data, int cnt);
struct movie {
string name;
string director;
int year;
int duration;
};
int main() {
int count = 0;
char input;
vector <movie> record;
movie *entry = nullptr;
do {
make_movie(entry);
record.push_back(*entry);
count++;
cout << endl;
cout << "Do you have more movie info to enter?\n";
cout << "Enter y / Y for yes or n / N for no: ";
cin.ignore();
cin >> input;
cout << endl;
} while (input == 'y' || input == 'Y');
show_movie(record, record.size());
return 0;
}
void make_movie(struct movie *film) {
cout << "Enter the title of the movie: ";
cin.ignore();
getline(cin, film -> name);
cout << "Enter the director's name: ";
cin.ignore();
getline(cin, film -> director);
cout << "Enter the year the movie was created: ";
cin >> film -> year;
cout << "Enter the movie length (in minutes): ";
cin >> film -> duration;
}
void show_movie(vector <movie> data, int cnt) {
cout << "Here is the info that you entered: " << endl;
for (int i = 0; i < cnt; i++) {
cout << "Movie Title: " << data[i].name << endl;
cout << "Movie Director: " << data[i].director << endl;
cout << "Movie Year: " << data[i].year << endl;
cout << "Movie Length: " << data[i].duration << endl;
cout << endl;
}
}
I am getting a error that says that i am trying to access a prohibited memory address.
The least amount of changes you need to make is to change:
movie *entry = nullptr;
do {
make_movie(entry);
record.push_back(*entry);
to:
movie entry;
do {
make_movie(&entry);
record.push_back(entry);
Further improvements would be:
Change make_movie to accept parameter by reference, then your program does not use any pointers and therefore is not vulnerable to any of the problems associated with pointers.
Change make_movie to return by value instead of taking a reference parameter.
cin.ignore(); is being used incorrectly. Your program will lose the first character of several of the input strings. Instead, remove all of those calls, and at the end of the make_movie function, ignore the rest of the current line. Also, change cin >> input; to use getline.
your bug
movie *entry = nullptr;
and
you have extra cin.ignore();
cout << "Enter the title of the movie: ";
// cin.ignore();
getline(cin, film -> name);
cout << "Enter the director's name: ";
// cin.ignore();
getline(cin, film -> director);
how to fix
movie main_info;
movie* entry = &main_info;
test
intput:
Enter the title of the movie: any_thing
Enter the director's name: yourself
Enter the year the movie was created: 2016
Enter the movie length (in minutes): 120
Do you have more movie info to enter?
Enter y / Y for yes or n / N for no: n
output
Here is the info that you entered:
Movie Title: any_thing
Movie Director: yourself
Movie Year: 2016
Movie Length: 120

C++ value updating/ignoring

So I have this
int main(){
string input;
string lastName;
string firstName;
int age;
int streetNum;
string streetName;
string town;
string zipCode;
float balance;
Update(lastName, firstName, age, streetNum, streetName, town, zipCode, balance);
}
and here is the function Update
void Update(string &lastname, string &firstname, int &age, int &streetnum, string &streetname, string &town, string &zipcode, float &balance){
cout << "Update the following, enter nothing to leave the same: " << endl;
string input;
cout << "Last name: ";
getline(cin, input);
if (input != "\n") { lastname = input; }
cout << "First name: ";
getline(cin, input);
if (input != "\n") { firstname = input; }
cout << "Age: ";
getline(cin, input);
if (input != "\n") { age = atoi(input.c_str()); }
cout << "Street number: ";
getline(cin, input);
if (input != "\n") { streetnum = atoi(input.c_str()); }
cout << "Street name: ";
getline(cin, input);
if (input != "\n") { streetname = input; }
cout << "Town name:";
getline(cin, input);
if (input != "\n") { town = input; }
cout << "ZipCode: ";
getline(cin, input);
if (input != "\n") { zipcode = input; }
cout << "Balance: ";
getline(cin, input);
if (input != "\n") { balance = atof(input.c_str()); }
}
My goal is to update the value or skip to the next value if the input is '\n'.
Once running and the program calls Update, it prints out "Last Name: First Name: " on the same line without letting the user input anything into lastname. I have no idea why it does this. Any tips or clues to directions to go in would be helpful.
getline() doesn't wait for user input. I believe unless you're told to use getline() you may want to use cin. Which would look as such:
cout<< "Lastname: ";
cin>>input;
if(input != " ")
{
lastname= input;
}
The only problem I foresee is that you won't be able to use '\n' as your condition for your if statements. In the above example I used a space as my skip character.