Validate string input in c++ for letters and spaces only [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I haven't got to know a lot about functions yet since I'm just at chapter 4 (which is decision making using if, else) of Starting Out with C++ book.
Here my problem asks me to enter the inputs of runner names and their timer so I could rank them in 1st 2nd and 3rd place.
I'm having trouble to validate the input for the string for runners's names as letters and spaces only.
I'm thinking about using loop or bool but I don't know what is the proper way to put it.
Can somebody show me what function I can use best in this case without repeating step into multiple lines of codes.
I'm sorry in advance for my long version code, I'm also looking for the way to shorten it.
#include <iostream>
#include <string>
#include <iomanip> // for setw, setpresicion, control output
#include <cctype> // for getline, cin.
using namespace std;
int main()
{
string runner1, runner2, runner3;
double time1, time2, time3;
cout << "Enter Runner 1 name: ";
getline(cin, runner1);
cout << "Enter Runner 2 name: ";
getline(cin, runner2);
cout << "Enter Runner 3 name: ";
getline(cin, runner3);
cout << runner1 << "'s finishing time: ";
cin >> time1;
while (!cin || (time1 < 0)){ //the # is negative
cout << "Please enter a non-negative number!\n";
cin >> time1;
}
cout << runner2 << "'s finishing time: ";
cin >> time2;
while (!cin || (time2 < 0)){ //the # is negative
cout << "Please enter a non-negative number!\n";
cin >> time2;
}
cout << runner3 << "'s finishing time: ";
cin >> time3;
while (!cin || (time3 < 0)){
cout << "Please enter a non-negative number!\n";
cin >> time3;
}
// This is for Rank1----------------------------
cout << "1st place : ";
if((time1 < time2)&&(time1 < time3))
{
cout << left << setw(5) << runner1 << " " << right
<< setw(5) << time1 << endl;
}
else if (time2 < time3){
cout << left << setw(5) << runner2 << " " << right
<< setw(5) << time2 << endl;
}
else {
cout << left << setw(5) << runner3 << " " << right
<< setw(5) << time3 << endl;
}
//Rank2------------------------------------
cout << "2nd place : ";
if ((time1 < time2)&&(time3 < time1)){
cout << left << setw(5) << runner1 << " " << right
<< setw(5) << time1 << endl;
}
else if ((time3 < time2)&&(time1 < time3)){
cout << left << setw(5) << runner3 << " " << right
<< setw(5) << time3 << endl;
}
else {
cout << left << setw(5) << runner2 << " " << right
<<setw(5) << time2 << endl;
}
// RANK 3-----------------------------------
cout << "3rd place : ";
if ((time1 > time2)&&(time1 > time3)){
cout << left << setw(5) << runner1 << " " << right
<< setw(5) << time1 << endl;
}
else if ((time1 < time3)&&(time3 > time2)){
cout << left << setw(5) << runner3 << " " << right
<< setw(5) << time3 << endl;
}
else {
cout << left << setw(5) << runner2 << " " << right
<<setw(5) << time2 << endl;
}
return 0;
}

This routine will do the validation:
bool validateString(const std::string& s)
{
for (const char c : s) {
if (!isalpha(c) && !isspace(c))
return false;
}
return true;
}

You might want to use something like
#include <cctype>
#include <string>
#include <iostream>
int main()
{
std::string foo;
bool valid;
do {
std::getline(std::cin, foo);
valid = true;
for (std::size_t i{}; i < foo.length() && valid; ++i) {
if (!(std::isalpha(static_cast<unsigned char>(foo[i])) ||
std::isspace(static_cast<unsigned char>(foo[i]))))
valid = false;
}
} while (!valid);
}
which checks every character for being a letter or a space.

Here is my proposal for a solution:
#include <iostream>
#include <string>
bool validateString(std::string toCheck) {
bool correct = false;
for (int i = 0; i<toCheck.length(); i++) {
if ((toCheck[i] >= 65 && toCheck[i] <= 90) || (toCheck[i] >= 97 && toCheck[i] <= 122) || toCheck[i] == 32) {
correct = true;
}
else {
return false;
}
}
return correct;
}
int main()
{
std::cout << validateString("Whats up peeps") << std::endl;
std::cout << validateString("234235") << std::endl;
system("pause");
return 0;
}
Using ASCII Table.
Note: Might not be the best way to do it but it works.

Related

While loop seems to be passing variables inversely from the Boolean condition

I've tested so many scenarios and it works without the while loop; but I just cant seem to figure out whats messing it up. if I pick an int in the target range 1-3 it passes and then freezes and i have to ctrl c the program out.
And if I pick a number outside that range it lets it pass in to the while loop and call the function. I'm very confused and this is the first program I've written with classes; so I feel like that probably is the issue.
Thanks.
#include <iostream>
using namespace std;
class Elevator
{
public:
void floorControl();
// Outputs floor actions and lets a user pick a floor number
int status();
// outputs floor positon
private:
int floorPosition = 1;
};
int main()
{
Elevator building[3];
int elevatorSelect;
cout << "\tElevator Status\n\tA\tB\tC\n"
<< "\t" << building[0].status() << "\t" << building[1].status()
<< "\t" << building[2].status() << endl
<< "\tWhich elevator do you want (1=A, 2=B, 3=C, or other to exit) ? ";
cin >> elevatorSelect;
cout << "\t" << elevatorSelect << endl;
while((elevatorSelect <= 3) && (elevatorSelect >=1));
{
building[elevatorSelect-1].floorControl();
cout << "\tElevator Status\n\tA\tB\tC\n"
<< "\t" << building[0].status() << "\t" << building[1].status()
<< "\t" << building[2].status() << endl
<< "\tWhich elevator do you want (1=A, 2=B, 3=C, or other to exit) ? ";
cin >> elevatorSelect;
cout << "\t" << elevatorSelect << endl;
}
return 0;
}
void Elevator::floorControl()
{
int floorSelect;
if(floorPosition > 1)
{
cout << "\tStarting at floor " << floorPosition << endl;
for(int i=1; i>floorPosition; floorPosition--)
cout << "\t Going down - now at floor " << floorPosition-1 << endl;
cout << "\tStopping at floor " << floorPosition << endl;
}
cout << "\tWhich floor do you want? ";
cin >> floorSelect;
if ((floorSelect < 1 )|| (floorSelect > 10))
cout << "\t**You pick up some dust off the wall; you missed**\n";
else
{
cout << "\tStarting at floor " << floorPosition << endl;
for (floorSelect; floorSelect > floorPosition; floorPosition++)
cout << "\t Going up - now at floor " << floorPosition+1 << endl;
cout << "\tStopping at floor " << floorPosition << endl;
}
}
int Elevator::status()
{
return floorPosition;
}

Why does program complain of too many commas?

Here is my code, I have attached the screenshot of what output Zybooks expects, and what my output is. I am trying to get it to output exactly what Zybooks is asking, however something seams to be wrong. It is compiling though. Or maybe Zybooks is just being stupid?
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
#include <cstring>
using namespace std;
int main() {
string title;
string col1;
string col2;
string val;
int numCommas = 0;
vector<string> stringData;
vector<int> intData;
cout << "Enter a title for the data:" << endl;
getline(cin, title);
cout << "You entered: " << title << endl << endl;
cout << "Enter the column 1 header:" << endl;
getline(cin, col1);
cout << "You entered: " << col1 << endl << endl;
cout << "Enter the column 2 header:" << endl;
getline(cin, col2);
cout << "You entered: " << col2 << endl << endl;
while (1) {
cout << "Enter a data point (-1 to stop input):" << endl;
getline(cin, val);
if (val == "-1") {
break;
}
if (val.find(',') == -1) {
cout << "Error: No comma in string." << endl << endl;
}
else {
for (int i = 0; i < val.length(); i++) {
if (val.at(i) == ',') {
numCommas++;
if (numCommas > 1){
break;
}
}
}
if (numCommas == 1) {
stringData.push_back(val.substr(0, val.find(',')));
intData.push_back(stoi(val.substr(val.find(',') + 1, val.length() - 1)));
cout << "Data string: " << val.substr(0, val.find(',')) << endl;
cout << "Data integer: " << stoi(val.substr(val.find(',') + 1, val.length() - 1)) << endl;
}
else {
cout << "Error: Too many commas in input." << endl << endl;
}
}
}
return 0;
}
Thanks.
Thanks.
Your problem is that you initialise numCommas to zero at the start of the program rather than at the start of each author input. That means, once it exceeds one, it will stay that high at least(a), meaning future inputs will always be seen as having too many commas.
You just need to set it to zero immediately before checking each input.
(a) Well, until it wraps around (if it wraps around). But that will be an awful lot of commas you need to input :-)

Add a countdown timer to a math program quiz

I am trying to add a countdown timer to this program. I would like the timer to start when the first math fact question is asked and upon expiration i want the program to give the grade. What's the code to do this in c++ if possible?
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
int main(int args, char* argv[])
{
int i;
int result;
int solution;
char fact;
bool done = false;
int correct = 0;
int count = 0;
do {
try {
cout << "Enter (m)ultiplication or "
<< "(a)ddition." << endl; /*or (s)ubstraction. */
cin >> fact;
while (!cin)
throw fact;
if (fact != 'A')
if (fact != 'a')
if (fact != 'M')
if (fact != 'm')
while (!cin)
throw fact;
cout << "Now, enter the number of the fact that
you would like to do." << endl;
cin >> i;
int wrong = 0;
int score = 0;
int j = 0;
while (!cin)
throw i;
switch (fact) {
case 'm':
case 'M':
while (j < 13) {
cout << "What's " << i << " x " << j << "?" << endl;
cin >> result;
while (!cin)
throw result;
solution = i * j;
if (result == solution) {
cout << "Great Job! That is the correct answer for the problem "
<< i << " x " << j << "." << endl;
cout << endl;
cout << endl;
cout << endl;
score++;
j++;
cout << endl;
}
if (result != solution) {
cout << "Oh no! " << result << " is NOT the correct answer for "
<< i << " x " << j << "." << endl;
wrong = wrong + 1;
count++;
}
if (count == 3) {
cout << "The correct answer is " << i * j << "." << endl;
j++;
wrong = wrong - 3;
count = 0;
}
if (count == 1) {
cout << endl;
count--;
wrong = wrong - 1;
}
if (count == 2) {
cout << endl;
count--;
wrong = wrong - 2;
}
}
case 'a':
case 'A':
while (j < 13) {
cout << "What's " << i << " + " << j << "?" << endl;
cin >> result;
while (!cin)
throw result;
solution = i + j;
if (result == solution) {
cout << "Great Job! That is the correct answer for the problem "
<< i << " + " << j << "." << endl;
cout << endl;
cout << endl;
cout << endl;
score++;
j++;
cout << endl;
}
if (result != solution) {
cout << "Oh no! " << result << " is NOT the correct answer for "
<< i << " + " << j << "." << endl;
wrong = wrong + 1;
count++;
}
if (count == 3) {
cout << "The correct answer is " << i + j << "." << endl;
j++;
wrong = wrong - 3;
count = 0;
}
if (count == 1) {
cout << endl;
count--;
wrong = wrong - 1;
}
if (count == 2) {
cout << endl;
count--;
wrong = wrong - 2;
}
}
if (j == 13) {
system("pause");
correct = score - wrong;
score = (correct * 100) / 13;
}
if (score >= 80) {
cout << "Excellent!!!!!" << endl;
cout << "You scored " << score << "%." << endl;
cout << "You got " << correct << " out of 13 correct." << endl;
cout << "Keep up the good work." << endl;
} else if (score >= 70) {
cout << "Congratulations!!!!!" << endl
cout << "You scored " << score << "%." << endl;
cout << "You got " << correct << " out of 13 correct." << endl;
cout << "Let's see if we can score even higher next time." << endl;
} else {
cout << "You scored below 70 which means that you may need some"
<< " more practice." << endl;
cout << "You scored " << score << "%." << endl;
cout << "You got " << correct << " out of 13 correct." << endl;
cout << "You might want to try the " << i << " facts again."
<< " Goodluck!!!!!" << endl;
}
}
} catch (char fact) {
cout << "Invalid input. You can only enter (m)ultiplication or"
<< " (a)ddition. Please try again." << endl;
cin.clear();
cin.ignore(100, '\n');
} catch (int i) {
cout << "Invalid input0. You can only enter a
number here. Please try again." << endl;
cin.clear();
cin.ignore(100, '\n');
} catch (...) {
cout << "Invalid input2. You can only enter a number here.
Please try again." << endl;
cin.clear();
cin.ignore(100, '\n');
}
} while (!done);
return 0;
}
The task is quite hard, but if you dare trying, I suggest doing it in two steps:
Implement inaccurate solution: timer expiration is checked between queries to user.
If there is some time left, next question is asked, otherwise statistics is shown. So program always waits for user input on the last question despite timer has run out. Not what exactly quizzes look like, but good move to start with.
Method: before starting quiz save current time, before each question take delta between saved time and current one and compare with time limit. Example with chrono (starting from C++11), example with oldschool clock
Add middle-question interruption
This part requires function, which will wait for user input not longer, than specified amount of time. So instead of using std::cin() you'll need to calculate amount of time left (time limit minus delta between cur time and start time) and call some sort of cin_with_timeout(time_left).
The hardest thing is implementing cin_with_timeout(), which requires solid knowledge of multithreading and thread synchronization. Great inspiration can be found here, but it is direction to start thinking rather than complete solution.

C++ Program need help to debug

#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
struct football_game
{
string visit_team;
int home_score;
int visit_score;
};
void printMenu();
int main()
{
int i, totalValues = 0;
ifstream inputFile;
string temp = "";
inputFile.open("games.txt");
if (!inputFile)
{
cout << "Error opening Input file!" << endl;
exit(101);
}
inputFile >> totalValues;
getline(inputFile, temp);
cout << " *** Football Game Scores *** " << endl << endl;
cout << " * Total Number of teams : " << totalValues << endl << endl;
football_game* records = new football_game[totalValues];
// while (!inputFile.eof())
// {// == NULL) {
for (i = 0; i < totalValues; i++)
{
getline(inputFile, records[i].visit_team);
cout << records[i].visit_team << endl;
inputFile >> records[i].home_score >> records[i].visit_score;
cout << records[i].home_score << " " << records[i].visit_score << endl;
getline(inputFile, temp);
}
//}
cout << endl;
int choice = 0;
int avg_home_Score = 0;
int avg_visit_Score = 0;
printMenu(); // prints menu
cout << "Please Enter a choice from the Menu : ";
cin >> choice;
cout << endl << endl;
while (true)
{
switch (choice)
{
case 1:
cout << " Score Table " << endl;
cout << " ***********************" << endl << endl;
cout << " VISIT_TEAM"
<< " "
<< " HIGH_SCORE"
<< " "
<< "VISIT_SCORE " << endl;
cout << " -----------"
<< " "
<< "-----------"
<< " "
<< "------------" << endl;
for (int i = 0; i < totalValues; i++)
{
cout << '|' << setw(18) << left << records[i].visit_team << " " << '|'
<< setw(7) << right << records[i].home_score << " " << '|' << setw(7)
<< right << records[i].visit_score << " " << '|' << endl;
}
cout << endl << endl << endl;
break;
case 2:
{
string team_name;
cout << "Enter the Team Name : ";
cin >> team_name;
for (int i = 0; i < totalValues; i++)
{
if (records[i].visit_team == team_name)
{
cout << " VISIT_TEAM"
<< " "
<< " HIGH_SCORE"
<< " "
<< "VISIT_SCORE " << endl;
cout << " -----------"
<< " "
<< "-----------"
<< " "
<< "------------" << endl;
cout << '|' << setw(18) << left << records[i].visit_team << " " << '|'
<< setw(7) << right << records[i].home_score << " " << '|'
<< setw(7) << right << records[i].visit_score << " " << '|'
<< endl;
}
}
cout << endl;
break;
}
case 3:
{
for (int i = 0; i < totalValues; i++)
avg_home_Score += records[i].home_score;
cout << "Average home_score: " << (avg_home_Score / totalValues) << endl << endl;
break;
}
case 4:
{
for (int i = 0; i < totalValues; i++)
avg_visit_Score += records[i].visit_score;
cout << "Average visit_score: " << (avg_visit_Score / totalValues) << endl << endl;
break;
}
default:
{
cout << "Please enter valid input !!" << endl;
break;
}
}
printMenu();
cin >> choice;
}
return 0;
}
void printMenu()
{
cout << " Menu Options " << endl;
cout << " ================ " << endl;
cout << " 1. Print Information of all Games[Table Form] " << endl;
cout << " 2. Print Information of a Specific Game " << endl;
cout << " 3. Print Average points scored by the Home Team during season" << endl;
cout << " 4. Print Average points scored against the Home Team" << endl << endl << endl;
}
Here is the input file i am using
games.txt
5
SD Mines
21 17
Northern State
10 3
BYU
10 21
Creighton
14 7
Sam Houston State
14 24
When i am using the 2nd option (Print Information of a Specific Game) from the output screen,
it ask me to enter the team name and when i enter the team-name.
For example: SD Mines it gives me an error, but when I enter the team-name with no space like: BYU it works fine for me.
cin >> team_name;
Takes the input only upto space.
You might want to use cin.getline() for taking space separated strings as input.
A small program demonstrating the same :
#include <iostream>
#include <string>
int main ()
{
std::string name;
std::cout << "Please, enter your full name: ";
std::getline (std::cin,name);
std::cout << "Name is : , " << name << "!\n";
return 0;
}
std::cin ignores whitespaces by default.
To include spaces in your input try :
getline(cin, team_name);
This would pick up all the characters in a line until you press enter. This is available in
#include<string>
You need to flush the std::cin buffer after reading the choice:
#include <limits>
//...
cin >> choice;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
Refer to this question for detailed explanation.
Also, if you want to read strings with spaces from the standard input, replace this:
cin >> team_name;
with this:
getline(cin, team_name);
as already mentioned in other answers. No need to flush std::cin this time, since you have already read the full line.
Finally, remove extra newlines from your games.txt:
5
SD Mines
21 17
Northern State
...

Why won't my code switch player names correctly after 3 rounds of play?

So in my class I had to make a Numberwang simulation game. Everything works fine except for the fact that after 2 rounds the names don't correlate correctly. It supposed to say "Round 3, Player1 to play first." which it does however player2 comes up as the one to play first.
# include <iostream>
# include <ctime>
# include <cstdlib>
using namespace std;
bool numberwang(int n)
{
if(n < 100 ){
return 1;
} else {
return 0;
}
}
int main()
{
string Firstplayer, Otherplayer;
int rounds;
int counter = 1;
int number;
int win = 18;
int lose= 1;
cout << "Hello, and welcome to Numberwang, the maths quiz that simply everyone is talking about!" << endl;
cout << "What is player 1's name? ";
cin >> Firstplayer;
cout << "What is player 2's name? ";
cin >> Otherplayer;
cout << "How many rounds? ";
cin >> rounds;
cout << "Well, if you're ready, lets play Numberwang!" << endl;
while(counter <= rounds){
cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
while(true){
cout << Firstplayer << ": ";
cin >> number;
if(numberwang(number)){
counter++;
if(counter > rounds){
cout << "That's Numberwang!" << endl;
cout << "Final scores: " << Firstplayer << " pulls ahead with " << win << ", and " << Otherplayer << " finishes with " << lose << endl;
break;
}
cout << "That's Numberwang!" << endl;
swap(Firstplayer, Otherplayer);
cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
}
cout << Otherplayer << ": ";
cin >> number;
if(numberwang(number)){
counter++;
if(counter > rounds){
cout << "That's Numberwang!" << endl;
cout << "Final scores: " << Firstplayer << " pulls ahead with " << win << ", and " << Otherplayer << " finishes with " << lose << endl;
break;
}
cout << "That's Numberwang!" << endl;
swap(Firstplayer, Otherplayer);
cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
}
}
}
return 0;
}
After your if-statement (line 61) you say 'Firstplayer' and then you output the 'Otherplayer'. The names do not match.
Blockquote
cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
}
cout << Otherplayer << ": ";
cin >> number;