get line function in c++ not working as it should - c++

I was trying to get the user input and it should include a space because I am asking for a full name. So I used the getline method but I can't input the data correctly.
Here's the code:
#include <iostream>
#include <string>
string name, mobile, landline, work;
cout << "Enter the name of the contact to be added:";
getline(cin, name);
cout << "Enter the mobile number of this contact:";
getline(cin, mobile);
cout << "Enter the landline number of this contact:";
getline(cin, landline);
cout << "Enter the work number of this contact:";
getline(cin, work);
When I run the program this is what I get:
Enter the name of the contact to be added:Enter the mobile number of this contact:
This is the main method:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: main.cpp
* Author: Gaby
*
* Created on March 25, 2020, 7:14 PM
*/
#include "Contact.h"
#include "DCSList.h"
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;
void showMenu();
void saveChanges(ofstream& file, DCSList& list);
/*
*
*/
int main(int argc, char** argv) {
fstream file; //This is the file we read from
ofstream outputFile; //This is the file we will write on at the end
string name, numberString; //Strings to extract the data from the file
string numbers[3]; //string array of numbers
string numberExtracted; //Used to extract each number from the numberString
string numbOfContacts; //Number of Contacts we want to read from file
int number; //Integer variable of the parsed value of the number read
int counter = 1; //Number of contacts read from file
int choice = 0; //Choice of user
DCSList list;
file.open("./ContactList.txt");
if(file.is_open()){
//If file is open then read from it
//First get the number of contacts in list
getline(file, numbOfContacts);
//Convert the number in the file to an integer
stringstream count(numbOfContacts);
//Set the value of number
count >> number;
while(counter <= number){
//Extract the name of the contact
getline(file, name);
//Extract the mobile, work and home numbers
getline(file, numberString);
//We will need to split the numberString with the - as delimiter
stringstream ss(numberString);
int i = 0; //Counter for numbers
while(getline(ss, numberExtracted, '-')){
numbers[i] = numberExtracted;
i++;
}
//Create a Contact object and add the contact to the list
Contact c(name, numbers[0], numbers[1], numbers[2]);
list.addNode(c);
counter++;
}
//Write on the other file
outputFile.open("NewContactList.txt");
if (outputFile.is_open()){
list.start();
while(list.hasNext()){
outputFile << list;
list.operator ++();
}
}
else{
cout << "File not opened!" << endl;
}
outputFile.close();
}
else{
cout << "File is not opened!" << endl;
}
file.close();
do{
showMenu();
cin >> choice;
switch(choice){
case 1:{
list.start();
while(list.hasNext()){
cout << list;
list.operator ++();
}
break;
}
case 2:
{
string name, mobile, landline, work;
cout << "Enter the name of the contact to be added:";
getline(cin, name);
cout << "Enter the mobile number of this contact:";
getline(cin, mobile);
cout << "Enter the landline number of this contact:";
getline(cin, landline);
cout << "Enter the work number of this contact:";
getline(cin, work);
//Create a contact object
Contact contact(name, mobile, work, landline);
//Add contact to list
list.addNode(contact);
}
break;
case 3:
{
string contactToDelete;
cout << "Enter a contact name or "
"any mobile, work, land number :";
cin >> contactToDelete;
if (list.deleteNode(contactToDelete)){
cout << "Contact has been deleted!" << endl;
}
}
break;
case 4:
{
string searchCriteria;
cout << "Enter a contact name or "
"any mobile, work, land number :";
getline(cin, searchCriteria);
if (list.searchNode(searchCriteria)){
cout << list;
}
else{
cout << "No contact found!" << endl;
}
break;
}
case 5:
{
cout << "Thank you for using the program." << endl;
cout << "All changes applied will be "
"saved to the output file" << endl;
//Write on the other file
outputFile.open("NewContactList.txt");
if (outputFile.is_open()) {
list.start();
while (list.hasNext()) {
outputFile << list;
list.operator++();
}
} else {
cout << "File not opened!" << endl;
}
file.close();
return 0;
}
default:{
cout << "Please enter a valid value to do an operation!" <<endl;
break;
}
}
}while(choice != 1 || choice != 2 ||
choice != 3 || choice != 4 || choice != 5);
return 0;
}
void showMenu(){
cout << "-------------PHONEBOOK-------------" << endl;
cout << "Enter a number according to "
"the operation you want to do: " << endl;
cout << "1. Show phone book" << endl;
cout << "2. Add new contact" << endl;
cout << "3. Delete a contact" << endl;
cout << "4. Search for a contact" << endl;
cout << "5. Exit program" << endl;
}
I am not able to input each value individually.
Do you know how can I fix this problem?
Thank you for your help.

How #NathanOliver has suspected you used a cin. In your main method in lines 111 and 153 (if formated in the same way as above) you can find them. Try to change them to getline() too. That should work.

Related

How to ignore .txt for cin for output file in C++?

This is my C++ code and I wish to create an output file. In order to create the output file, I request the user to include .txt at this input, cin >> accnum ; , hence, when output file is produced, it will show accnum.txt as plain text. My question is, what should I do in order to ignore .txt to be appeared in the text file, provided that I must input .txt(this is to produce txt file, without .txt at the input, no output file will be produced). I have put a comment line, kindly refer to that line. Thanks for your help!
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main ()
{
double withdraw,deposit,number,transfer,num,amt;
string name,first_name,middle_name,last_name;
int x=0;
int option;
int A = 300;
int B = 500;
int C,W,y;
string accnum;
ifstream infile;
ofstream outfile;
do {
cout <<" Welcome to ATM Machine "<<endl;
cout <<"\n\t****Menu****";
cout <<"\n\t* 1.Creating a new account *";
cout <<"\n\t* 2.Cash Deposit *";
cout <<"\n\t* 3.Cash withdrawal *";
cout <<"\n\t* 4.Fund transfer between two account *";
cout <<"\n\t* 5.Exit *";
cout <<"\n\t* *";
cout <<"\n\t********";
cout <<"\n\t Please choose an option: ";
cin>>option;
switch(option){
case 1:
cout <<"Press 1 to Confirm" << endl << "Press 0 to exit " << endl ;
cin >> C ;
{
if( C == 1 )
{
cout << "Enter first name: " << endl;
cin >> first_name;
cout << "Enter middle name: " << endl;
cin >> middle_name;
cout << "Enter last name: " << endl;
cin >> last_name;
name = first_name + " " + middle_name + " " + last_name;
cout << "enter a 4 digit number : " << endl << "gentle reminder : please add .txt at the end of your number" << endl << "e.g 1234.txt" << endl;
cin >> accnum ; //how to ignore .txt
cout << "What is your primary balance: " << endl;
cin >> amt;
infile.open(name);
outfile.open(name);
cout << "your account has been created " << endl ;
outfile << "name" << name << endl;
outfile << "account no. : " << accnum << endl ;
outfile << "current balance : RM" << amt << endl ;
}else {
cout << "Bye, have a nice day";
}
infile.close();
outfile.close();
}
break;
case 2:
cout<<"Deposit amount: "<<endl;
cin>>deposit;
if(deposit<10) {
cout<<"The smallest note acceptable is RM10."<<endl;
}else {
x=static_cast<int>(deposit)%10;
if(x!=0)
{
cout<<"Invalid deposit amount"<<endl;
cout<<"Example of deposit amount: RM10, RM20, RM50, RM320..."<<endl;
}else {
A+=deposit;
cout<<"current balance: RM"<< A <<endl;
}
}
break;
case 3:
cout<<"Amount you want to withdraw"<<endl;
cin>>withdraw;
if(A < withdraw || withdraw < 10) { // A is balance
cout <<"Failed to withdraw money! Please try again."<<endl;
}else {
A-=withdraw;
cout <<"current balance: "<< A <<endl;
}
break;
case 4:
infile.open("1212.txt");
infile >> x;
cout<<"Enter Details of transfering Account(Account A)"<<endl;
cout<< "Your account number is:"<<endl;
cin>>W;
cout<< "What is your name:"<<endl;
cin.ignore();
getline(cin,name);
cout<<"Enter Details of receiving Account(Account B)"<<endl;
cout<<"Enter account number:"<<endl;
cin>>W;
cout<<"Enter name:"<<endl;
cin.ignore();
getline(cin, name);
cout <<"your account balance is : " << x << endl << "how much do you want to transfer ? ";
cin >> num ;
outfile.open("1212.txt");
outfile << x - num ;
cout << "your account balance is : " << x - num ;
infile.close();
outfile.close();
infile.open("2121.txt");
infile >> y ;
outfile.open("2121.txt") ;
outfile << y + num ;
infile.close();
outfile.close();
break;
case 5:
cout<<"Thank you for using ATM machine. Bye, have a nice day !"<<endl;
return 0;
default:
if(option != 5) cout<<"Invalid option.Please try again!"<<endl;
break;
}
}while(option != 5);
system("pause");
return 0;
}
Just create the file without a format, the name will be the file_name content exactly, be aware that in windows has reserved names such as CON.
#include <iostream>
#include <fstream>
#include <string>
int main (void) {
std::string file_name = "1234";
std::fstream f_handle;
f_handle.open(file_name, std::fstream::out);
if (f_handle.is_open()){
f_handle << "Some text" << std::endl;
f_handle.close();
}
f_handle.open(file_name, std::fstream::in);
if (f_handle.is_open()){
std::cout << f_handle.rdbuf() << std::endl;
f_handle.close();
}
return 0;
}
You asked about how to remove the .txt part from accnum and here's one way to do that:
cin >> accnum ; //how to ignore .txt
// check that the length/size of the entered accnum is at least 4 characters
// and
// that the last 4 characters is ".txt"
if(accnum.size() >= 4 && accnum.substr(accnum.size() - 4) == ".txt") {
// extract a substring from accnum without the last 4 characters
accnum = accnum.substr(0, accnum.size() - 4);
}
However, I have my doubts about the requirement that the user should actually add .txt to the account number, because seeing this at an ATM would be really confusing:
enter a 4 digit number :
gentle reminder : please add .txt at the end of your number"
e.g 1234.txt
It's especially confusing since the only thing you want to do with .txt is to remove it. I would consider changing this user interface.
Suggestions:
Let the user add the account number without .txt
Instead of saving the account information in a file with the account holders name as the filename, save it in a file named accnum + ".txt". This makes it possible for a person to have more than one account - just as in real life.

Save file troubles

Ok. I'm trying to finish writing to a file and then reading from a file but I keep getting an error when I start the void readData(void).The error is "no matching token found." What could I do to make this work/ make it easier?
I'm not understanding how to use the void function, I think. I'm sure there are more issues because when I remove void readData(void) the error moves to void writeData(void).
//Specification: Append and display records in a address database
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void menu(void);
void writeData(void);
void readData(void);
ifstream infile;
string cont = "Y";
string name;
string street;
string city;
string state;
string zip;
string choice;
const char FileName[] = "TestAddress.txt";
ofstream outfile("TestAddress.txt", ios::app);
int main() {
menu();
writeData();
readData();
return 0;
} //end main
void menu(void) {
cout << "Would you like to add to the records, display them, or exit?\n";
cout << "Enter a to add, d to display, or e to exit";
cin >> choice;
}
//allow user to choose to append records, display records or exit the program
//end menu
void writeData(void) {
while (choice != "a") {
cout << "Enter the address name: ";
getline(cin, name);
cout << "Enter the house number and street: ";
cin >> street;
cout << "Enter the city: ";
cin >> city;
cout << "Enter the state: ";
cin >> state;
cout << "Enter the zip code: ";
cin >> zip;
outfile << name << "#" << street << city << "#" << state << "#" << zip << endl;
cin.ignore();
cout << "Do you want add another addressor exit? (a/e)";
getline(cin, choice);
}
outfile.close();
//Write the Address Info to a file
//loop while user still has data to write to file
//eg outStream<<name<<”#”; //where # is the delimiter
}
//end write data
void readData(void) {
//read data from a file
//use the split function to break a
//deliminated line of text into fields
infile.open("TestAddress.txt");
ifstream inMyStream("TestAddress.txt");
while (choice != "d") {
if (inMyStream.is_open()) {
//set character to use as a line between record displays
string recBreaks = "#";
recBreaks.assign(20, '#');
int fieldCount = 0; //keep track of the number of fields read
int recordCount = 1; //keep track of the number of records read
//read the first field
fieldCount = 1;
string fieldBuffer;
getline(inMyStream, fieldBuffer, '#');
while (!inMyStream.eof()) {
//display the field
switch (fieldCount) {
case 1:
cout << recBreaks << endl;
cout << "record # " << recordCount << endl;
cout << "Name...." << fieldBuffer << endl; break;
case 2:
cout << "Street.." << fieldBuffer << endl; break;
case 3:
cout << "City...." << fieldBuffer << endl; break;
case 4:
cout << "State..." << fieldBuffer << endl; break;
case 5:
cout << "Zip....." << fieldBuffer << endl;
fieldCount = 0;
recordCount++; break;
}
//read the next field
getline(inMyStream, fieldBuffer, '#');
fieldCount++;
}
cout << recBreaks << endl;
inMyStream.close();
}//end read data
}

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;
}

junk left in cin buffer

i need to prevent the junk left in the buffer as entering a value for a switch case menu from being used in a function called by the menu where their is user input.
menu code
void menu()
{
bool done = false;
string input;
while(!done)
{
cout << "Welcome to the DVD database." << endl;
cout << "1. Add A DVD" << endl;
cout << "2. Delete a DVD." << endl;
cout << "3. Edit a DVD." << endl;
cout << "4. List By Category." << endl;
cout << "5. Retrieve by a DVD by Title." << endl;
cout << "6. Display collection by year" << endl;
cout << "7. Display collection by title" << endl;
cout << "-999. Exit program" << endl;
cout << "Please choose an option by entering the corresponding number" << endl;
cin >> input;
int value = atoi(input.c_str());
switch(value)
{
case 1:addDVD(); break;
case 2:deleteDVD(); break;
// case 3:editDVD(); break;
case 4:listByCategory();break;
case 6:displayByYear();break;
case 7:displayByTitle();break;
case -999: writeToFile(); exit(0); break;
default : cout <<"Invalid entry"<< endl; break;
}
}
}
void retrieveByTitle()
{
string search;
int size = database.size();
int index = 0;
bool found = false;
cin.ignore();
cout << "Please enter the title of the DVD you would like to retrieve: " << endl;
getline(cin,search);
cout << search;
while(!found && index<size)
{
if(database.at(index)->getTitle().compare(search)==0)
{
cout << database.at(index)->toString();
break;
}
}
cout << endl;
}
if 5 is entered in the menu, the program skips the user input in the method
This code works, but it has the same problem you describe if you eliminate the 'cin.ignore()', which removes the extra delimiters ignored by the cin >> operator:
#include <iostream>
#include <climits>
using namespace std;
int main() {
string a, b;
while (true) {
cout << "write 'x' to exit: " << endl;
cin >> a;
if (a == "x") {
break;
}
cout << "read '" << a << "'" << endl;
cout << "now write a line: " << endl;
cin.clear(); // clears cin status
cin.ignore(INT_MAX); // clears existing, unprocessed input
getline(cin, a);
cout << "read '" << a << "'" << endl;
}
return 0;
}
When dealing with interactive user input you should use std::getline()
The std::cin is flushed to the application every time you hit <enter>. So this is the logical junks you should read data from the user in.
std::string answer;
std::cout << "Question:\n";
std::getline(std::cin, answer);
This gets you everything the user provided in response to the previous question.
Once you have the input you should get the value you think is on the input. Once you have this you should check if there is any other junk on the input (if there is then abort and re-try) otherwise validate the data you expected.
If you were expected an integer;
std::stringstream linestream(answer);
int value;
std::string junk;
if ((answer >> value)) && (!(answer >> junk)))
{
// If you got data
// and there was no junk on the line you are now good to go
}
In your specific example there is already a simple way to do this:
std::getline(std::cin, input);
int value = boost::lexical_cast<int>(input); // throws an exception if there is not
// an int on the input (with no junk)

Unable to delete a record from a c binary program I created

thanks for your anticipated help. I have been looking at this code for hours and days but I just cant seem to find out why my delRecFunc() isn't deleting just one record at a time. It appears it is deleting the whole file.
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <iomanip>
using namespace std;
const int NAME_SIZE = 40, ADDR_SIZE = 50, PHONE_SIZE = 14;
char y, Y;
struct Info
{
char name[NAME_SIZE];
int age;
char address1[ADDR_SIZE];
char phone[PHONE_SIZE];
}people;
void popFunc();
void dispFunc();
void appFunc();
void delRecFunc();
void searchFunc();
void modRecFunc();
int getNumRec();
int validateRecord (int);
void wipeFunc();
int main()
{
Info person;
char choice, option;
cout << " The file we are working with is named stuff.dat,\n"
<< "I will let you know if it unavailable " << endl;
cout << " "<< endl;
do
{
cout << " \n \n Simple People Records... \n \n \n "
<< " Please choose from the following options: \n \n \n"
<< " 1. POPULATE THE BINARY FILE \n"
<< " 2. DISPLAY THE CONTENTS OF THE FILE\n"
<< " 3. APPEND A RECORD TO THE FILE\n"
<< " 4. DELETE A RECORD FROM THE FILE\n"
<< " 5. SEARCH FOR A CERTAIN RECORD\n"
<< " 6. MODIFY A CERTAIN RECORD. \n"
<< " 7. WIPE OUT THE ENTIRE CONTENTS OF THE FILE. \n" <<endl;
cin >> option;
switch(option)
{
case '1': popFunc();
system("pause");
break;
case '2': dispFunc();
break;
case '3': appFunc();
break;
case '4': delRecFunc();
break;
case '5':searchFunc();
break;
case '6':modRecFunc();
break;
case '7':wipeFunc();
break;
}
}
while(choice!= 1000);
system("pause");
return(0);
}
void popFunc()
{ fstream file("stuff.dat", ios::out | ios::binary);
if (file.fail())
{
cout << "the file does not exist so we are creating it now... ";
system("pause");
file.open("stuff.dat", ios::out);
}
cout << "enter the following data about a person: "<<endl;
cout << "Name: " <<endl;
cin >> ws;
cin.getline(people.name,NAME_SIZE);
cout << "Age (integers only or the program will be corrupted): " <<endl;
cin >> people.age;
cin.ignore(); //skip over the remaining newline.
cout<< "Address line 1: ";
cin >> ws;
cin.getline(people.address1,ADDR_SIZE);
cout << "Phone: in the following format ie: 201.123.1234 (no hyphens)";
cin >> ws;
cin.getline(people.phone,PHONE_SIZE);
file.write(reinterpret_cast<char *>(&people), sizeof(people));
file.close();
}
void dispFunc()
{
ifstream file;
file.open("stuff.dat", ios::binary);
if (file.fail())
{
cout << "the file does not exist so we are creating it now... ";
system("pause");
file.open("stuff.dat", ios::out);
}
while(file.read(reinterpret_cast<char *> (&people), sizeof(people)))
{
cout << "Name: "<< people.name <<endl;
cout << "Age: "<< people.age <<endl;
cout << "Address: " <<people.address1 <<endl;
cout << "Phone #: " <<people.phone << " \n\n"<<endl;
}
file.close();
}
void appFunc()
{ ofstream file;
file.open("stuff.dat", ios::binary | ios::app);
if (file.fail())
{
cout << "the file does not exist so we are creating it now... ";
system("pause");
file.open("stuff.dat", ios::out);
}
cout << "Name: " <<endl;
cin >> ws;
cin.getline(people.name,NAME_SIZE);
cout << "Age (integers only or the program will be corrupted): " <<endl;
cin >> people.age;
cin.ignore(); //skip over the remaining newline.
cout<< "Address line 1: ";
cin >> ws;
cin.getline(people.address1,ADDR_SIZE);
cout << "Phone: Phone: in the following format ie: 201.123.1234 (no hyphens)";
cin >> ws;
cin.getline(people.phone,PHONE_SIZE);
file.write(reinterpret_cast<char *>(&people), sizeof(people));
file.close();
}
void searchFunc()
{
int count =1;
int answer;
long recNum;
int recCount = getNumRec();
fstream search;
search.open("stuff.dat", ios::in | ios:: binary);
cout << " Please enter 1 - " <<recCount ;
cout << " and I will display the data /n"<<endl;
cin >>recNum;
while(recNum<1 || recNum > recCount)
{
cout<<"Please enter a number between 1 and "<<recCount<<": ";
cin>>recNum;
}
answer = validateRecord(recNum);
}
void modRecFunc()
{
int count=1;
int answer;//to hold choice from the user
long recNum;//to hold a record number
int recCount = getNumRec();//variable to hold how many record the file has
fstream search;
//open file
search.open("stuff.dat",ios::in|ios::out|ios::binary);
cout<<"Which record do you wish to edit? ";
cin>>recNum;//variable to store the record the user wish to delete
//validation so the user is not allow to enter more numbers than the actual
//size of the file
while(recNum<1||recNum>recCount)
{
cout<<"Please enter a number between 1 and "<<recCount<<": ";
cin>>recNum;
}
//move pointer to desire position
cout<<endl;
answer= validateRecord(recNum);//make sure the record is the right one
cout<<endl;
if(answer==1)
{
//get the new data
cout << "Name: " <<endl;
cin >> ws;
cin.getline(people.name,NAME_SIZE);
cout << "Age (integers only or the program will be corrupted): " <<endl;
cin >> people.age;
cin.ignore(); //skip over the remaining newline.
cout<< "Address line 1: ";
cin >> ws;
cin.getline(people.address1,ADDR_SIZE);
cout << "Phone: Phone: in the following format ie: 201.123.1234 (no hyphens)";
cin >> ws;
cin.getline(people.phone,PHONE_SIZE);
search.seekp((recNum-1)*sizeof(people), ios::beg);
search.write(reinterpret_cast<char *>(&people), sizeof(people));
}
cout<<endl;
search.close();
}
void wipeFunc()
{ char option;
ofstream file;
cout << "This option will delete all the contents of the file "<<endl;
cout << " Are you sure you want to delete the file? Y or N " <<endl;
cin >> option;
if (option == 'y' || option == 'Y')
{
cout << " Warning, if you hit Y, you will lose everything " <<endl;
cout << " Y to confirm, N to return to main menu" <<endl;
cin >> option;
if(option == 'y' || option == 'Y')
{
file.open("stuff.dat", ios::out | ios::binary);
file.close();
}
}
}
int validateRecord (int recNum)
{
int answer;// variable to hold the answer from the user
fstream validate;
//open file
validate.open("stuff.dat",ios::in|ios::out|ios::binary);
validate.seekg((recNum-1)*sizeof(people));
//read record from file
validate.read(reinterpret_cast<char*>(&people),sizeof(people));
cout<<endl;
cout << "the name is "<< people.name <<endl;
cout << "the age is "<< people.age <<endl;
cout << "the address is" <<people.address1 <<endl;
cout << "the phone is Phone: " <<people.phone <<endl;
cout<<"Is this the file you want?\n"
"1. yes\n"
"2. no\n"
"Choice: ";
cin>>answer;
// validate answer to 1 or 2
while(answer<1||answer>2)
{
cout<<"enter only 1 or 2: ";
cin>>answer;
}
cout<<endl;
validate.close();
return answer;
}
int getNumRec()
{
int count =0;
fstream file;
file.open("stuff.dat", ios::in|ios::binary);
if (file.fail())
{
cout << "Error opening file. Program aborting.\n";
}
file.read(reinterpret_cast<char *>(&people),sizeof(people));
while (!file.eof())
{
count++;
file.read(reinterpret_cast<char *>(&people),
sizeof(people));
}
file.close();//close the file
return count;//return count the erase function
}
void delRecFunc()
{
int count=1;
int answer; //to hold choice from the user
double recNum;//to hold the record number the user wishes to delete
int recCount = getNumRec();//variable to hold how many record the file has
fstream erase;
erase.open("stuff.dat", ios::in|ios::out|ios::binary);
Info Temp; //*****NEW TEMP STRUCT VAR*****//
cout << "Please enter the phone number of the person you want to delete in this
format 201.123.1111)";
cin >> recNum;
//move pointer to desire position
erase.seekg((recNum-1)*sizeof(people));
//read record from file
erase.read(reinterpret_cast<char*>(&people),sizeof(people)); //read record from file
//get answer from the validateRecord function
answer = validateRecord (recNum);
if(answer == 1)
{
ofstream copy;
copy.open("stuff2.dat", ios::out | ios::binary);
erase.seekg(0*sizeof(people)); // Move pointer to beginning of the file
erase.read(reinterpret_cast<char*>(&people),sizeof(people));
// BEGIN NEW CODE //
// Keep looping till there are no more records in the customer.dat file
while (!erase.eof())
{
// If the id that the user wants deleted doesnt match the current
// records id, copy it to the temp directory
if(people.phone != Temp.phone)
{
// Write the record to copy.dat
copy.write(reinterpret_cast<char*>(&people),sizeof(Info));
}
// Read the next line from customer.dat
erase.read(reinterpret_cast<char*>(&people),sizeof(Info));
}
// Close both files
erase.close();
copy.close();
}
// Delete the Old customer.dat file
remove("stuff.dat");
// Rename the temporary holder to "customer.dat"
rename("stuff2.dat","stuff.dat");
}
You're taking an improperly formatted double (inputted from the cin >> recNum), seeking that far (*sizeof(people) into the file, and going from there. Seems like you need to start there.
Looks like you are entering the phone number in recNum (in the format 201.123.1111) and doing a seekg, which will give incorrect results