My question is to both:
Allow the user to enter a name and get custom messages based upon the name entered, and, when you collect the name from the user, convert the collected name to lowercase.
Thus, in my 'void test_user_details()' function, I made a custom message if the user puts in either Tim or John, but when I test the code it doesnt work? It skips the custom message. Also, I used "to_lowercase(name)" but the output isnt lowercase, it stays the same.
It seems like my if and else if functions arnt running at all and I dont know why.
Here is the full code of the program:
#include "splashkit.h"
#include <string>
using namespace std;
string read_string(string prompt)
{
string result;
write(prompt);
result=read_line();
return result;
}
int read_integer(string prompt)
{
string line;
line = read_string(prompt);
return convert_to_integer(line);
}
***************
void test_user_details()
{
string name;
string line;
int age;
name = read_string("What is your name: ");
line = read_string("What is your age: ");
age = convert_to_integer(line);
write_line("Hello, " + name);
if (to_lowercase(name) == "Tim")
{
write_line("\nWelcome Tim, creator of this program");
}
else if (to_lowercase(name) == "John")
{
write_line("\nWelcome John, tester of this program");
}
else
{
write_line("Are you Tim or John?");
}
write("Age: ");
write_line(age);
}
void play_game()
{
string line;
int guess;
int target;
target = rnd(100) + 1;
write_line("Guess a number between 1 and 100 (inclusive)");
while(guess != target)
{
write("Enter guess: ");
line = read_line();
guess = convert_to_integer(line);
if (guess < target)
{
write_line("Sorry, the number is greater than " +
to_string(guess));
}
else if (guess > target)
{
write_line("No, the number is less than " +
to_string(guess));
}
}
write_line("You guessed correctly! The number was " +
to_string(target));
}
int main()
{
string line;
int option;
do
{
write_line("1. Play Game");
write_line("2. Quit");
write("Choosen option: ");
line = read_line();
option = convert_to_integer(line);
switch(option) //switch function
{
case 1:
test_user_details();
play_game();
break;
case 2:
write_line("Game ends.");
break;
default:
write_line("Please enter an option from the menu");
}
} while (option!= 2);
return 0;
}
assuming your to_lowercase returns a string in lower case it will never match:
"Tim" or "John"
Related
It is possible to do next:
Let say I have a string "input" (that will be a input), I will cut this input in 2 parts, next, I will find if I entered first part only letters, and second part only digits? the code work only for letters but not for digits(remove comments, to see that all entered will be valid)
#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>
using namespace std;
int main ()
{
while (true)
{
bool flag = false; // to check for numeric entry
string input; // not req to initialize
string input1;
cout << "Enter the string like ABC 123: ";
getline (cin, input);
if (input == "")
{
flag = true;
}
if (string::size_type pos = input.find (' '))//spliting the input in 2 if it will find a space
{
if (input.npos != pos)
{
input1 = input.substr (pos + 1);
input = input.substr (0, pos);
}
}
for (int i = 0; i < input.size (); i++)
{
// for (int n = 0; i < input1.size (); i++)
// {
int uppercaseCHar = toupper (input[i]);//checking if input(first part) contains only letters
if (!std::isalpha (uppercaseCHar))
{
// if(isdigit(input1[n]) == 0)//checing if input1(second part) contains only digits
// {
flag = true;
break;
// }
}
// }
}
if (input.compare ("1") == 0) break;//This will end program
{
flag = false;
}
if (flag)
{
cout << "Invalid!\n";
cout << endl;
} else
{
cout << "The string is valid! \n";
cout << endl;
}
}
}
Enter the string like ABC 123: QWE 123
The string is valid!
Enter the string like ABC 123: QW1 123
Invalid!
I don't have rights to comment yet, but if your first and second part of string don't have to be of same length, you can use the for loop two times for each sub string and then compare the values by one character at a time.
Also after checking for termination condition by
if (input.compare ("1") == 0) break;
you are adding a statement
{
flag = false;
}
this will set the result to false even if you have compared in your loop and found it TRUE,so take a look at following code below, i have commented that block of code out.
#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>
using namespace std;
int main ()
{
while (true)
{
bool flag = false; // to check for numeric entry
string input; // not req to initialize
string input1;
cout << "Enter the string like ABC 123: ";
getline (cin, input);
if (input == "")
{
flag = true;
}
if (string::size_type pos = input.find (' '))//spliting the input in 2 if it will find a space
{
if (input.npos != pos)
{
input1 = input.substr (pos + 1);
input = input.substr (0, pos);
}
}
//cout<<"\n"<<input1;
//cout<<"\n"<<input;
//First check the letter part (first part) if it contains digits
for(int i=0;i<input.size();i++){
if(!std::isalpha(input[i])){
flag=true;
break;
}
}
//second check if the numeric part (second part) only contains digits
for(int i=0;i<input1.size();i++){
if(!std::isdigit(input1[i])){
flag=true;
break;
}
}
/*
for (int i = 0; i < input.size (); i++)
{
for (int n = 0; i < input1.size (); i++)
{
int uppercaseCHar = toupper (input[i]);//checking if input(first part) contains only letters
if (!std::isalpha (uppercaseCHar))
{
if(isdigit(input1[n]) == 0)//checing if input1(second part) contains only digits
{
flag = true;
break;
}
}
}
}
*/
if (input.compare ("1") == 0) break;//This will end program
//after checking for break, if you add flag=false, it will automatically ignore whatever flag you set, and you will always find FLAG=FALSE when compairing in upcoming lines
//{
// flag = false;
//}
if (flag)
{
cout << "Invalid!\n";
cout << endl;
} else
{
cout << "The string is valid! \n";
cout << endl;
}
}
}
I have this program that i took it out from: https://intcpp.tech-academy.co.uk/input-validation/ and it works fine, i did some changes because i need the program to keep asking the user to enter a valid input, so that why it has the while in there however it only asks 4 times after that 4th time the input will be valid it does not matter if it right or not, Does any one know how i can fix this. Thank you
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main () {
cout << "Please enter name:" << endl;
string userName;
getline(cin, userName);
bool rejected = false;
while (rejected == false)
{
for (unsigned int i = 0; i < userName.length() && !rejected; i++)
{
if (isalpha(userName[i]))
continue;
else if (userName[i] == ' ')
continue;
else
{
cout << "Error, Please enter Patient's name again, First Name: ";
getline(cin, userName);
rejected = false;
}
}
rejected = true;
}
system("pause");
return 0;
}
Personally I would do something like
bool is_valid_username(std::string const& username)
{
// First trim the string of all leading and trailing white-space
trim(username);
if (username.length() == 0)
return false; // Input was empty or all spaces
return std::all_of(begin(username), end(username), [](char const ch)
{
return std::isalpha(ch) || ch == ' '; // Only letters and spaces are allowed
});
}
std::string get_username()
{
std::string username;
do
{
std::cout << "Please enter username: ";
std::getline(std::cin, username);
} while (!is_valid_username(username));
return username;
}
[For the trim function please see this old answer]
The get_username function will continue to ask for a username forever if the input is either empty, all spaces, or contains non-letters or not a space.
Here's a reference for std::all_of.
Here's a reference about lambda expressions.
if (isalpha(userName[i]) || (userName[i] == ' '))
continue;
else
{
cout << "Error, Please enter Patient's name again, First Name: ";
getline(cin, userName);
i = -1; //Reset check name
}
Try it!
Change unsigned int to int
I have a program that does three things. Asks you how many variables you wan't, ask you to input each variable, then stores it in a vector. I have put some code that checks if your input is correct, and if it isn't, re-loops the code asking for your variable. The problem I am having is that when you type anything in around the second variable, it asks you to try again infinitely.
For instance, if I typed these values into the input:
Variable amount: 5
Please input variable 1: 8
Please input variable 2: 8
ERROR, PLEASE ENTER ONLY VALID SYMBOLS
---------------------
Please input variable 2:
It would keep outputting ERROR, PLEASE ENTER ONLY VALID SYMBOLS over and over again no matter what you typed. The code is down below, and if you have a better name for this question please let me know. (I'm not really sure what to call this)
#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;
int inputErrorMessage()
{
cout << "\n ERROR, PLEASE ENTER ONLY VALID SYMBOLS \n";
cout << "--------------------- \n";
return 0;
}
int main()
{
// Declare the variables, vectors, etc.
int varNum = 1;
int totVar = 0;
int choice = 0;
vector<int> userNums;
double input = 0;
string checktotVar = "";
string checkInput = "";
string sym = "";
bool valid = false;
stringstream sstotVar;
stringstream ssinput;
if (choice != 6) {
while (!valid) {
valid = true;
// Ask user for how many variables they want then record it
cout << "Variable amount: ";
getline(cin, checktotVar);
sstotVar << checktotVar;
sstotVar >> totVar;
if (sstotVar.fail() || totVar <= 0) {
inputErrorMessage();
valid = false;
sstotVar.clear();
sstotVar.ignore();
}
}
valid = false;
while (!valid) {
valid = true;
// Ask the user for each variable, then record it into the array
for (int i = 0; i < totVar; ++i) {
cout << "Please input variable " << varNum << ": ";
getline(cin, checkInput);
ssinput << checkInput;
ssinput >> input;
if (ssinput.fail()) {
inputErrorMessage();
valid = false;
ssinput.clear();
ssinput.ignore();
}
if (valid == true) {
userNums.push_back(input);
varNum++;
}
}
}
}
}
ssinput >> input;
reads the one thing in ssinput right to the end of the stream while leaving the read valid. The next time around
ssinput << checkInput;
can't write into the stream because the stream hit the stream's end. That means the read also fails and
if (ssinput.fail()) {
enters the body of the if where the program clears the error
ssinput.clear();
and then promptly reads off the end of the stream with
ssinput.ignore();
causing the error all over again.
Quickest solution:
Recreate
stringstream ssinput;
on each loop iteration. So
stringstream sstotVar;
//stringstream ssinput; gone from here
and
getline(cin, checkInput);
stringstream ssinput(checkInput); // and now tighter scope recreated each loop.
ssinput >> input;
Also by keeping the stream around without emptying it out it can get very., very big.
You can also simplify your logic around
while (!valid) {
and eliminate some repeated code by moving the read validation into it's own function
int getMeANumber(const std::string & message, int min)
that loops until it gets a number and then returns that number. For example:
int getMeANumber(const std::string & message, int min)
{
while (true)
{
cout << message;
string checktotVar;
getline(cin, checktotVar);
stringstream sstotVar(checktotVar);
int totVar;
sstotVar >> totVar;
if (!sstotVar || totVar <= min)
{
inputErrorMessage();
}
else
{
return totVar;
}
}
}
Now main is this itty-bitty tiny lil' thing.
int main()
{
int choice = 0;
vector<int> userNums;
if (choice != 6)
{
int totVar = getMeANumber("Variable amount: ", 0);
for (int i = 0; i < totVar; ++i)
{
stringstream varname;
varname << "Please input variable " << i+1 << ": ";
userNums.push_back(getMeANumber(varname.str(), numeric_limits<int>::min()));
// numeric_limits<int>::min requires #include <limits>
}
}
}
Here are the issues with this code.
In this part:
if (valid == true) {
userNums.push_back(input);
varNum++;
}
you forgot to add an ssinput.clear(). This will reset the stream state (clear the error flags), otherwise you cannot use it again. That is why it stops working at the second input.
In addition, even though this works, you are pushing back a variable that you declared as double into a vector of ints. That is bound to cause issues if this was intended to store double variables, instead of truncating them and storing them as ints.
It should be:
#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;
int inputErrorMessage()
{
cout << "\n ERROR, PLEASE ENTER ONLY VALID SYMBOLS \n";
cout << "--------------------- \n";
return 0;
}
int main()
{
// Declare the variables, vectors, etc.
int varNum = 1;
int totVar = 0;
int choice = 0;
vector<int> userNums;
double input = 0;
string checktotVar = "";
string checkInput = "";
string sym = "";
bool valid = false;
stringstream sstotVar;
stringstream ssinput;
if (choice != 6) {
while (!valid) {
valid = true;
// Ask user for how many variables they want then record it
cout << "Variable amount: ";
getline(cin, checktotVar);
sstotVar << checktotVar;
sstotVar >> totVar;
if (sstotVar.fail() || totVar <= 0) {
inputErrorMessage();
valid = false;
sstotVar.clear();
sstotVar.ignore();
}
}
valid = false;
while (!valid) {
valid = true;
// Ask the user for each variable, then record it into the array
for (int i = 0; i < totVar; ++i) {
cout << "Please input variable " << varNum << ": ";
getline(cin, checkInput);
ssinput << checkInput;
ssinput >> input;
if (ssinput.fail()) {
inputErrorMessage();
valid = false;
}
if (valid == true) {
userNums.push_back(input);
varNum++;
}
ssinput.clear();
}
}
}
}
EDIT: You need to clear the stringstream on each iteration of the loop, otherwise you're not writing to an empty stream when you grab the next input from the user, which is what's causing the .fail() method to return true after the first iteration of the loop.
Im sorry about posting a super long code, but when I run this code all I see is this-
Heap size: 1638652
Getting int:
Getting int:
Getting int:
Getting int:
Getting int:
Heap size: 1638653
and it keeps going in a loop with the heapsize being incremented by one.
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <exception>
#ifndef WX_REPORT_H
#define WX_REPORT_H
#include <string>
#include <sstream>
using std::string;
using std::stringstream;
typedef struct WX_REPORT
{
string unitType;
string stationName;
string time;
string gpsLoc;
int pressure;
int windSpeed;
int temperature;
int humidity;
int windDirection;
string toString()
{
stringstream str;
str << stationName << ": " << time << "\t" << gpsLoc << "\n";
str << pressure << "\n" << windSpeed << "\n" << temperature << "\n";
str << humidity << "\n" << windDirection;
return str.str();
}
}
WXReport;
#endif
/*
* Reports must be in the following format:
* M or I // Metric or imperial units
*/
using namespace std;
vector<WXReport*> heap;
bool compTime(const WXReport* a, const WXReport* b) {
if(a->time < b->time) { // timing
return false;
} else {
return true; // commands to return true
}
}
void heapAdd(WXReport* wx) {
heap.push_back(wx);
push_heap(heap.begin(), heap.end());
}
WXReport* heapPop() { // header popup
pop_heap(heap.begin(), heap.end());
WXReport* rep = heap.back();
heap.pop_back();
return rep;
}
void getInt(istream &input, int &i) {
string temp;
input>>temp;
cout<<"Getting int: "<<temp<<endl;
i = atoi(temp.c_str());
}
void readInFile(string filename) {
ifstream input(filename);
WXReport *report;
while(!input.eof()) {
report = new WXReport();
getline(input, report->unitType);
getline(input, report->stationName);
getline(input, report->time);
getline(input, report->gpsLoc);
getInt(input, report->pressure);
getInt(input, report->windSpeed);
getInt(input, report->temperature);
getInt(input, report->humidity);
getInt(input, report->windDirection);
heapAdd(report);
cout<<"Heap size: "<<heap.size()<<endl;
}
}
int menu() {
cout<<"\n\nPlease select one: "<<endl;
cout<<"1) Read in another file"<<endl;
cout<<"2) Display the fastest wind speed"<<endl;
cout<<"3) Display weather stations by name"<<endl;
cout<<"4) Display all weather reports"<<endl;
cout<<"5) Remove a weather report"<<endl;
cout<<"6) Write weather reports to file"<<endl;
cout<<"0) Exit"<<endl;
int choice;
cin>>choice;
return choice;
}
void printAllReports() {
cout<<"Printing all reports"<<endl;
for(WXReport* rep: heap) {
cout<<rep->toString()<<endl;
}
cout<<"Done printing reports"<<endl;
}
int main(int argc, char* argv[]) {
string filename = "report.txt";
readInFile(filename);
int choice = menu();
while(choice != 0) {
switch(choice) {
case 1:
cout<<"What file would you like to read in?"<<endl;
cin>>filename;
readInFile(filename);
break;
case 2:
cout<<"Has not been implemented"<<endl;
break;
case 3:
cout<<"Has not been implemented"<<endl;
break;
case 4:
printAllReports();
break;
case 5:
cout<<"Has not been implemented"<<endl;
break;
case 6:
cout<<"Has not been implemented"<<endl;
break;
default:
cout<<"Invalid choice, please try again."<<endl;
}
choice = menu();
}
cout<<"Thank you!"<<endl;
return 0;
}
Important part. If you read nothing else, read this: Always check the error codes and return values.
After ifstream input(filename); you have no idea if the file opened. Testing with input.is_open() gets past that.
If the file isn't open, all those calls to getline fail as does eof(). File not open, can't read end of file and can't exit loop. Even if the the file is open, if you don't check the output of getline, how do you know you read a line?
One of the fun parts of streams is if you test the stream, it tells you if it is in a bad state, so you can write code that looks like
if (getline(...) && getline(...) && ...)
So you don't have to make a massive block of if-else-if or a sea of nested ifs. First bad read and you are out.
The problem with if eof() is covered in the comments to the question. The basic is you don't know if you got the end of the file until you start reading. Also, what happen if you hit the end of the file in the middle of a bunch of reads?
So read a line. If it's good, read the next line, etc... until done.
getInt isn't necessary.
int val;
input >> val;
loads an integer into val, if the stream can be parsed into an int. If it can't, input is marked bad and you can check why. Could be unparsable. Could be end of file.
int val;
if (input >> val)
{
//do stuff
}
else
{
//no int here. Do other stuff
}
Just like above, you can chain the instructions and get
if (input >> val >> anotherval >> ...)
I'm a beginner to C++. We are doing a project where we input firstName, lastName, and SSN for employee. Here what I have done:
#include <iostream>
#include <stdexcept>
#include "Employee.h"
using namespace std;
Employee::Employee(const string &first, const string &last, const string &ssn)
{
firstName = first;
lastName = last;
SSN = ssn;
}
void Employee::setFirstName(const string &first)
{
firstName = first;
}
string Employee::getFirstName() const
{
return firstName;
}
void Employee::setLastName(const string &last)
{
lastName = last;
}
string Employee::getLastName() const
{
return lastName;
}
void Employee::setSSN(const string &ssn)
{
if (ssn.length() == 9)
{
SSN = ssn;
}
else
{
cout << "Please enter SSN again: " << endl;
}
}
string Employee::getSSN() const
{
return SSN;
}
void Employee::print() const
{
cout << "Employee: " << getFirstName() << ' ' << getLastName()
<< "\nSocial Security Number: " << getSSN();
}
My instructor wants us to check the length of the SSN (simplest way) to make sure it 9 digits, and if it's more or less, ask users to input again. I do not know how to validate the input for SSN. Can anyone help me please?
std::string::length() is the function to be used to find length and std::string::empty() to check weather string is empty or not.
if(SSN.length() < 9 && !SSN.empty())
{
//Need C++11 and above for this kind for loop
for(auto &x: SSN)
{
if(std::isdigit(x))
{
//valid SSN
}
}
}
A compact solution to this problem:
bool ValidSSN(const std::string& ssn) {
if (ssn.size() != 9) return false;
return ssn.find_first_not_of("0123456789") == ssn.npos;
}
There is no simple and easy solution to checking the string for 9 digits.
My recommendation is a two step process:
Verify length is 9.
Verify string is an integral number.
Note: The code in the else clause does not work for cases such as "123Apple4". Thanks to #Steephen for the enlightenment. Leaving the code in the answer to synchronize with comments.
In code this would be:
bool is_valid = true;
if (ssn.length() != 9)
{
is_valid = false;
}
else
{
std::istringstream ssn_stream;
ssn_stream.str(ssn);
unsigned int value;
if (!(ssn_stream >> value))
{
is_valid = false;
}
}
Another alternative is to use a loop:
if (ssn.length() != 9)
{
is_valid = false;
}
else
{
is_valid = true;
for (unsigned int i = 0U; i < 9; ++i)
{
if (!isdigit(ssn[i]))
{
is_valid = false;
break;
}
}
}
Edit 1: Regular Expressions
If your compiler supports C++ regular expression, you could come up with a regular expression to define the SSN. You would then check if the contents of the string matched the regular expression.