Basically My Program Goal is that i have a file containing 4 telephone numbers
it would Look Like this
Harry Keeling (555)123-4567
Frank James (555)123-8901
Arthur Paul (555)987-4567
Todd Shurn (555)987-8901
What my program is doing right now is prompting the user for the name and last name and iterates through the file to see if their is a match if their is a match the phone number is saved in the variable phone number if their isnt a match the program outputs error right now my program is doing what is susposed to do but after each match is found the program is susposed to prompt do you want to continue looking up numbers y or n if it is a yes it is susposed to loop through the file again but basically my program isnt working and i have no idea why my code
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void lookup_name(ifstream&, string&); // prototype
int main()
{
ifstream myfile;
string phonenumber;
string choice;
lookup_name(myfile, phonenumber);
if (phonenumber == " ") {
cout << "Error" << endl;
}
else {
cout << "The Telephone Number you Requested is" << phonenumber << endl;
cout << "Do you Want to look up another name in the directory?" << " " << "<Y/N" << endl;
cin >> choice;
if (choice == "Y")
lookup_name(myfile, phonenumber);
}
}
void lookup_name(ifstream& myfile, string& phonenumber)
{
string fname;
string lname;
string name1, name2, dummy, choice;
myfile.open("infile.txt");
cout << "What is your first name" << endl;
cin >> fname;
cout << "What is your last name" << endl;
cin >> lname;
for (int i = 0; i < 4; i++) {
myfile >> name1 >> name2;
if (fname + lname == name1 + name2) {
myfile >> phonenumber;
myfile.close();
if (choice == "Y")
{
continue;
}
else {
myfile >> dummy;
}
}
}
}
You need to add a loop inside of main() itself to prompt the user to continue, and you need to fix the mistakes in your lookup_name() function.
Try something more like this instead:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <limits>
using namespace std;
bool lookup_name(istream&, const string&, string&); // prototype
int main()
{
ifstream myfile("infile.txt");
string name, phonenumber, choice;
do
{
cout << "What is the name? ";
getline(cin, name);
if (!lookup_name(myfile, name, phonenumber)) {
cout << "Error" << endl;
}
else {
cout << "The Telephone Number you Requested is '" << phonenumber << "'" << endl;
}
cout << "Do you want to look up another name in the directory (Y/N)? ";
cin >> choice;
cin.ignore(numeric_limits<streamsize_t>::max(), '\n');
if ((choice != "Y") && (choice != "y"))
break;
myfile.seekg(0);
}
while (true);
return 0;
}
bool lookup_name(istream& myfile, const string& name, string& phonenumber)
{
string line, fname, lname;
while (getline(myfile, line))
{
istringstream iss(line);
if (iss >> fname1 >> lname)
{
if (name == (fname + " " + lname))
return getline(iss, phonenumber);
}
}
return false;
}
Related
I am making a car rental service and am trying to write to an already existing file.
I made a previous file that has the username they entered as the name of the file. This is where the problem comes in, I made an if statement and used ofstream file and then did file.open(username + ".txt") but when I do this vs code says the identifier is unknown. The variable was made in the main function and the if statement is in the main function and I am unsure how to proceed. here is ever line of code I have written:
#include <iostream>
#include <string>
#include <fstream>
bool IsLoggedIn(){
std::string username, password, un, pw;
std::cout << "enter your username: "; std::cin >> username;
std::cout << "enter your password: "; std::cin >> password;
//checks the file for a password and username
std::ifstream read(username + ".txt");
getline(read, un);
getline(read, pw);
if(un == username && pw == password){
return true;
} else {
return false;
}
};
int main(){
int choice;
std::string option;
std::cout << "hello welcome to nathans car rental press 1 to register press 2 to login: "; std::cin >> choice;
if(choice == 1){
std::string username, password;
std::cout << "make a username: "; std::cin >> username;
std::cout << "make a password: "; std::cin >> password;
//makes a file and puts the username and password in it
std::ofstream file;
file.open(username + ".txt");
file << username << std::endl << password;
file.close();
main();
} else if (choice == 2){
bool status = IsLoggedIn();
if(!status){
std::cout << "you do not have an account" << std::endl;
system("PAUSE");
return 0;
}else{
std::cout << "welcome back" << std::endl;
system("PAUSE");
}
}
std::cout << "what car would you like to rent maserati=m lamborghini=l and ferrari=f" << std::endl;
std::cin >> option;
if(option == "m"){
std::cout << "you now have the maserati" << std::endl;
std::ofstream file;
file.open(username + ".txt");
}
};
the red squiggly line is at the username in the bottom in file.open()
you have to define your variables outside the if, if you want to use them
I've been struggling all day with this problem. I can't make my program to delete an specific line of my file. I have a list of names in my file and I would like to delete one of them, I don't know what I'm missing.
EDIT: Here is my code:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <stdlib.h>
using namespace std;
string fname;
string keyword;
int phone_num;
ifstream myfile;
ofstream phone;
void Add()
{
ofstream phone("number.txt", ios::app);
cout << "Enter Name: ";
cin >> fname;
cout << "Enter Phone Number: ";
cin >> phone_num;
if (phone.is_open())
{
phone << fname << endl;
phone << phone_num << endl;
cout << "Contact saved successfully!" << endl;
}
phone.close();
}
void All()
{
ifstream myfile("number.txt", ios::in);
while(myfile >> fname >> phone_num)
{
for(auto name : fname)
{
cout << name;
}
cout << endl;
}
}
void Delete()
{
ifstream myfile("number.txt", ios::in);
cout << "Enter Name to delete : ";
cin >> keyword;
while(getline(myfile, keyword))
{
fname.replace(fname.find(keyword),keyword.length(),"");
}
}
I have a question in this code. What does fucntion receive when we use cin as a argument.
For example cin stops when he finds a white space or '\n', so if the input is "1 2 3 4 5" the program will return 5 but I can't understand why.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
double max_value(istream& in) // can be called with 'infile' or 'cin'
{
double highest;
double next;
if (in >> next)
highest = next;
else
return 0;
while (in >> next)
{
if (next > highest)
highest = next;
}
return highest;
}
int main()
{
double max;
string input; cout << "Do you want to read from a file? (y/n) ";
cin >> input;
if (input == "y")
{
string filename;
cout << "Please enter the data file name: ";
cin >> filename;
ifstream infile; infile.open(filename);
if (infile.fail())
{
cerr << "Error opening " << filename << "\n";
return 1;
}
max = max_value(infile);
infile.close();
}
else
{
cout << "Insert the numbers. End with CTRL-Z." << endl; max = max_value(cin);
}
cout << "The maximum value is " << max << "\n";
return 0;
}
Why does cin keep prompting even if I hit enter?
#include <iostream>
using namespace std;
int main(){
string name = "";
cout << "What's your name?";
cin >> name;
cout << "Hello "";
if (name == "")
cout << "World!";
else
cout << name + "!";
return 0;
}
I want cin >> to stop when I hit enter without typing any words so if the user enters nothing it will show a default Hollow World message and otherwise customized message.
cin >> name;keeps reading until you've entered something. You want a function that just reads a single line. That function is called getline.
#include <iostream>
#include <string>
using namespace std;
int main(){
string name = "";
cout << "What's your name?";
getline(cin, name);
cout << "Hello "";
if (name == ""){
cout << "World!";
else
cout << name + "!";
return 0;
}
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
char hold;
string name;
char num1;
char num2;
int main() {
cout << "Hello!\n";
cout << "Tell me your name?: ";
cin >> name;
cout << "Well well well, if it isn't "<< name << "!\n";
cout << "Enter a NUMBER " << name << ": ";
cin >> num1;
while(!isdigit(num1)) {
cout << "Enter a NUMBER " << name << ": ";
cin >> num1;
}
cin >> hold;
system("pause");
return 0;
}
The problem is, it is overlooping the cout. How do I fix it?
Thanks.
A better way is to use std::stringstream (note: include sstream)
int getNumber()
{
std::string line;
int i;
while (std::getline(std::cin, line))
{
std::stringstream ss(line);
if (ss >> i)
{
if (ss.eof())
{
break;
}
}
std::cout << "Please re-enter your input as a number" << std::endl;
}
return i;
}
This replaces your while loop, and you make the call after asking for a number as you already figured out how to do.
The following is a shortened version of the original attempt. However, as with the original, it only checks a single character.
If I changed num1 to be an int then i'd need to check whether the input was valid as #Dieter Lucking mentioned.
#include <iostream>
using namespace std;
int main() {
char num1;
do {
cout << "\nEnter a number: ";
cin >> num1
} while(!isdigit(num1));
}
A bit of a variation on staticx's solution, which will pass Dieter Lücking's echo "" | test line.
I use an istringstream and get input until no more standard input or I get valid input. I pushed it all into a templated Get function that can be used for any type; you just need to give it a prompt for the user:
Get() function
template<typename T>
void Get(T& toSet, std::string prompt) // read from cin
{
std::string nextIn;
cout << prompt;
getline(cin >> std::ws, nextIn);
istringstream inStream(nextIn);
while(cin && !(inStream >> toSet))
{
inStream.clear();
cout << "Invalid Input. Try again.\n" << prompt;
getline(cin >> std::ws, nextIn);
inStream.str(nextIn);
}
if (!cin)
{
cerr << "Failed to get proper input. Exiting";
exit(1);
}
}
And you'd use it like so:
int myNumber = 0;
Get(myNumber, "Please input a number:");
Full code:
Live Demo
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
template<typename T>
void Get(T& toSet, std::string prompt) // read from cin
{
std::string nextIn;
cout << prompt;
getline(cin >> std::ws, nextIn);
istringstream inStream(nextIn);
while(cin && !(inStream >> toSet))
{
inStream.clear();
cout << "Invalid Input. Try again.\n" << prompt;
getline(cin >> std::ws, nextIn);
inStream.str(nextIn);
}
if (!cin)
{
cerr << "\nFailed to get proper input. Exiting\n";
exit(1);
}
}
int main()
{
string name;
int num1 = -1;
cout << "\nHello!\n";
Get(name, "\nTell me your name?:");
cout << "\nWell well well, if it isn't "<< name << "!\n";
Get(num1, std::string("\nEnter a NUMBER, ") + name + ": ");
cout << "\nYou entered number: " << num1 << std::endl;
return 0;
}