Unable to exit out of do-while loop? - c++

I can't figure out why the do-while loop isn't being terminated upon user entering -1 in input. Just ignore everything after the internal while loop. I know that the problem is somewhere in the while loop. I just don't see it.
int main()
{
srand(time(0));
int input;
std::string pass, company, timeS;
do
{
std::cout << "Enter password length: ";
std::cin >> input;
while(input < 8 || input > 16)
{
if(!std::cin)
{
std::cin.clear();
std::cin.ignore(100, '\n');
}
std::cout << "Password length must be between 8 and 16.\nEnter password length: ";
std::cin >> input;
}
std::cout << "Enter company name: ";
std::getline(std::cin, company);
pass = passGen(input);
time_t now = time(0);
auto time = *std::localtime(&now);
std::stringstream ss;
ss << std::put_time(&time, "%Y %b %d %H:%M:%S %a");
timeS = ss.str();
std::cout << "You passoword: " << pass << std::endl;
writeFile(pass, company, timeS);
}while(input != -1);
return 0;
}
Thanks in advance!

In this loop you wait for number between 8 and 16:
while(input < 8 || input > 16)
{
if(!std::cin)
{
std::cin.clear();
std::cin.ignore(100, '\n');
}
std::cout << "Password length must be between 8 and 16.\nEnter password length: ";
std::cin >> input;
}
Guess, what never comes out of that loop? Right, -1 !! =)

Hope this will answer your question:
int main()
{
srand(time(0));
int input;
std::string pass, company, timeS;
// do
// {
while(true){
std::cout << "Enter password length: ";
std::cin >> input;
if(input == -1){
break;
}
while(input < 8 || input > 16)
{
if(!std::cin)
{
std::cin.clear();
std::cin.ignore(100, '\n');
}
std::cout<< "=====>>>" << std::endl;
std::cout << "Password length must be between 8 and 16.\nEnter password length: ";
std::cin >> input;
}
std::cout << "Enter company name: ";
std::getline(std::cin, company);
pass = passGen(input);
time_t now = time(0);
auto time = *std::localtime(&now);
std::stringstream ss;
ss << std::put_time(&time, "%Y %b %d %H:%M:%S %a");
timeS = ss.str();
std::cout << "You passoword: " << pass << std::endl;
writeFile(pass, company, timeS);
}
// }while(input != -1);
return 0;
}
Just use
while(true){
//getting length of password
if(input == -1){
break;
}
//rest of the logic
}

The internal while loop will never terminate as the condition is will always be true for -1.
int main()
{
srand(time(0));
int input;
std::string pass, company, timeS;
do
{
std::cout << "Enter password length: ";
std::cin >> input;
while((input < 8 && input>=0)|| input > 16)
{
if(!std::cin)
{
std::cin.clear();
std::cin.ignore(100, '\n');
}
std::cout << "Password length must be between 8 and 16.\nEnter password length: ";
std::cin >> input;
}
if (input>0){
std::cout << "Enter company name: ";
std::getline(std::cin, company);
pass = passGen(input);
time_t now = time(0);
auto time = *std::localtime(&now);
std::stringstream ss;
ss << std::put_time(&time, "%Y %b %d %H:%M:%S %a");
timeS = ss.str();
std::cout << "You passoword: " << pass << std::endl;
writeFile(pass, company, timeS);
}
}while(input != -1);
return 0;
}

Just ignore everything after the internal while loop.
Ok. Let's do that...
int main()
{
int input;
do
{
std::cout << "Enter password length: ";
std::cin >> input;
while(input < 8 || input > 16)
{
std::cin >> input;
}
}while(input != -1);
return 0;
}
I know that the problem is somewhere in the while loop. I just don't see it.
I hope you see it now. Your inner while ensures that input is never -1.
I think your current problem is a symptom of not structuring your code. Hence, to fix your code I propose to use functions:
void do_something(int input);
bool read_and_validate(int& input) {
input = 0;
while(input < 8 || input > 16) {
std::cin >> input;
if (input == -1) return false;
}
return true;
}
int main() {
int input;
while( read_and_validate(input) ) {
do_something(input);
}
}

Related

How can I store input after a comma as an integer?

I've been working on a program to display user input in a table and a histogram, I've got those down, but I can't figure out how to take the user input and separate it by a comma and save the first part as a string and the second as an integer. I used streams but it separates it by spaces and the string might need to have spaces in it, so that's not reliable. I also tried substrings but I need the second half to be an int. Any tips are appreciated. Thank you.
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
#include <ctype.h>
using namespace std;
int main() {
string title, col1, col2, userInput, one, two;
istringstream inSS;
string lineString;
string author;
int books;
vector<string> vecAuthors;
vector<int> vecBooks;
bool inputDone;
cout<<"Enter a title for the data:"<<endl;
getline(cin, title);
cout<<"You entered: "<<title<<endl;
cout<<"Enter the column 1 header:"<<endl;
getline(cin, col1);
cout<<"You entered: "<<col1<<endl;
cout<<"Enter the column 2 header:"<<endl;
getline(cin, col2);
cout<<"You entered: "<<col2<<endl;
while (!inputDone) {
cout<<"Enter a data point (-1 to stop input):"<<endl;
getline(cin, lineString);
while (lineString.find(',') == string::npos) {
if (lineString == "-1") {
break;
}
cout << "Error: No comma in string.\n" << endl;
cout << "Enter a data point (-1 to stop input):" << endl;
getline(cin, lineString);
}
string::size_type position = lineString.find (',');
if (position != string::npos)
{
while (lineString.find (',', position+1) != string::npos) {
if (lineString == "-1") {
break;
}
cout << "Error: Too many commas in input." << endl;
cout << "Enter a data point (-1 to stop input):" << endl;
getline(cin, lineString);
}
}
one = lineString.substr(0, lineString.find(','));
two = lineString.substr(lineString.find(',') + 1, lineString.size() - 1);
inSS.clear();
inSS.str(lineString);
inSS >> author;
inSS >> books;
if (inSS.fail()) {
if (lineString == "-1") {
break;
}
cerr << "Error: Comma not followed by an integer." << endl << endl;
cout << "Enter a data point (-1 to stop input):" << endl;
getline(cin, lineString);
}
inSS.clear();
inSS.str(lineString);
inSS >> author;
if (author == "-1") {
cout<<"Finished."<<endl;
inputDone = true;
}
else {
inSS >> books;
author.pop_back();
vecAuthors.push_back(author);
vecBooks.push_back(books);
cout << "Data string: " << author << endl;
cout << "Data integer: " << books << endl;
}
}
cout<<setw(33)<<right<<title<<endl;
cout<<setw(20)<<left<<col1<<"|";
cout<<setw(23)<<right<<col2<<endl;
cout<<setfill('-')<<setw(43)<<""<<endl;
cout<<setfill(' ');
for(int i=0; i<vecAuthors.size(); ++i){
cout<<setw(20)<<left<<vecAuthors[i]<<"|"<<setw(23)<<right<<vecBooks[i]<<endl;
}
cout<<endl;
for(int i=0; i<vecAuthors.size(); ++i){
cout<<setw(20)<<right<<vecAuthors[i];
for(int k = 0; k<vecBooks[i]; ++k) {
cout<<left<<"*";
}
cout<<endl;
}
return 0;
}
Code to get the first part to a string and second part to an int:
#include <iostream>
#include <string>
int main()
{
std::string input{};
std::cin >> input;
int commaSlot{};
for (int i = 0; i < input.length(); i++) {
if (input[i] == ',') {
commaSlot = i;
break;
}
}
std::string firstPart = input.substr(0,commaSlot);
int secondPart = std::stoi(input.substr(commaSlot+1));
std::cout << firstPart << " " << secondPart;
}
because what you need is quite custom (you need comma as a separator and not space ) you can get a line and parse it as you want with the getline function of std afterwards you can separate the string on the comma (the simplest way I can think off is a simple for loop but you can use std's algorithm's also) and then you can use the stoi function to convert a string to an int ,all of them together:
std::string row{};
unsigned positionofcomma{};
std::getline(std::cin,row);
for (unsigned i=0;i<row.length();i++)
if (row[i]==','){
positionofcomma=i;
break;
}
std::string numberstring=row.substr(positionofcomma+1, row.length());
int number=std::stoi(numberstring);

Asking for user input more than once in a while loop

I am currently writing a program that translates normal text to whale talk (in case you want to know what whale talk is, it is the statement ripped of consonants with u's and e's doubled)
I successfully wrote the program. But I want it to be repeated again and go through the same process until we manually exit it.
For this I used a while loop and used exit as variable. But it didn't work as expected as on second time around it did not ask for input but displayed the prompt text and skipped to the exit/no exit line.
Can you tell me what is wrong with my program? Here is my code (you can try to compile and execute it yourself):
#include <iostream>
#include <string>
#include <vector>
int main(){
bool exit = false;
//Open
std::cout<<"==========\n";
std::cout<<"WHALE TALK\n";
std::cout<<"==========\n";
std::cout<<"\n";
//main loop
while (!exit){
//Variables
std::vector<char> whaletalk;
//input
std::string input;
std::cout<<"Type the text you want to translate to whale language: ";
getline(std::cin,input);
std::cout<<"\n\nWhale talk: ";
//vowels
std::vector <char> vowels = {'a','e','i','o','u'};
//sorter
//iterating through string
for (int i = 0; i < input.size(); i++){
//iterating through vowels
for (int j = 0; j < vowels.size(); j++){
//case of vowels
if(input[i] == vowels[j]){
//case of u and e
if ((input[i] == 'u') or (input[i] == 'e')){
whaletalk.push_back(input[i]);
whaletalk.push_back(input[i]);
}
//case of vowels other than u and e
else {whaletalk.push_back(input[i]);}
}
}
}
//Output
for (int k = 0; k < whaletalk.size(); k++ ){
std::cout<<whaletalk[k];
}
std::cout<<"\n";
// exit/no exit
std::string response;
std::cout<<'\n';
std::cout<<"Do you have more to translate?(yes/no)\n\nYour response: ";
std::cin>>response;
if (response == "NO" or response == "no" or response == "No"){
exit = true;
}
}
}
Here is an image of the bug
When you use the >> operator to read a string, it only reads the next word (up to a whitespace character), and leaves everything else on the input stream, but std::cin still blocks until a new line (enter is pressed).
std::string response;
std::cout << '\n';
std::cout << "Do you have more to translate?(yes/no)\n\nYour response: ";
std::cin >> response;
if (response == "NO" or response == "no" or response == "No") {
exit = true;
This leaves just the newline in the buffer if you only typed one word (if you did say "a b" then it would leave "b\n" and response would be "a", not "a b"), and so the next std::getline will give you what was left, in this case an empty line.
int main()
{
std::string str;
std::cout << "Enter word: ";
std::cin >> str;
std::cout << "Entered '" << str << "'" << std::endl;
std::cout << "Enter line: ";
std::getline(std::cin, str);
std::cout << "Entered '" << str << "'" << std::endl;
}
Enter word: a
Entered 'a'
Enter line: Entered ''
Or if you have a space character:
Enter word: aa bb
Entered 'aa'
Enter line: Entered ' bb'
You could just always use std::getline when reading just string responses to avoid missing anything by accident (like that " bb").
std::string response;
std::cout << '\n';
std::cout << "Do you have more to translate?(yes/no)\n\nYour response: ";
std::getline(std::cin, response);
if (response == "NO" or response == "no" or response == "No") {
exit = true;
Or after any other >> operations to get whatever is "remaining" and proceed to the next line.
Another possibility is to use std::istream.ignore() to ignore remaining input up to a maxmium length and delimiter:
std::string response;
std::cout << '\n';
std::cout << "Do you have more to translate?(yes/no)\n\nYour response: ";
std::cin >> response;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (response == "NO" or response == "no" or response == "No") {
exit = true;
In this case std::numeric_limits<std::streamsize>::max() means any amount, up to the next line \n. If the user entered say "no abc", then "abc" would be lost.
You should put these function to clear input and ignore all character except break line character:
std::cin.clear();
std::cin.ignore(32767, '\n');
below this function:
std::cin >> ...;
Here is the loop check for response:
while (true) {
if (response == "NO" or response == "no" or response == "No") {
exit = true;
break;
}
else if (response == "YES" or response == "yes" or response == "Yes")
{
break;
}
else
{
std::cout << "\nPlease only input yes or no: ";
std::cin >> response;
// Clear input
std::cin.clear();
std::cin.ignore(32767, '\n');
}
}
Full code:
#include <iostream>
#include <string>
#include <vector>
int main() {
bool exit = false;
//Open
std::cout << "==========\n";
std::cout << "WHALE TALK\n";
std::cout << "==========\n";
std::cout << "\n";
//main loop
while (!exit) {
//Variables
std::vector<char> whaletalk;
std::cout << "Type the text you want to translate to whale language: ";
//input
std::string input;
std::getline(std::cin, input);
std::cout << "\n\nWhale talk: ";
//vowels
std::vector <char> vowels = { 'a','e','i','o','u' };
//sorter
//iterating through string
for (int i = 0; i < input.size(); i++) {
//iterating through vowels
for (int j = 0; j < vowels.size(); j++) {
//case of vowels
if (input[i] == vowels[j]) {
//case of u and e
if ((input[i] == 'u') or (input[i] == 'e')) {
whaletalk.push_back(input[i]);
whaletalk.push_back(input[i]);
}
//case of vowels other than u and e
else { whaletalk.push_back(input[i]); }
}
}
}
//Output
for (int k = 0; k < whaletalk.size(); k++) {
std::cout << whaletalk[k];
}
std::cout << "\n";
// exit/no exit
std::string response;
std::cout << '\n';
std::cout << "Do you have more to translate?(yes/no)\n\nYour response: ";
std::cin >> response;
// Clear input
std::cin.clear();
std::cin.ignore(32767, '\n');
while (true) {
if (response == "NO" or response == "no" or response == "No") {
exit = true;
break;
}
else if (response == "YES" or response == "yes" or response == "Yes")
{
break;
}
else
{
std::cout << "\nPlease only input yes or no: ";
std::cin >> response;
// Clear input
std::cin.clear();
std::cin.ignore(32767, '\n');
}
}
}
return 0;
}

C++ user input restriction with proper retry without "goto"

I have the following code:
qstn:
cout << "Input customer's lastname: ";
getline(cin, lname);
if (lname.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ") != string::npos) {
cout << "You can only input alpha here!\n";
cin.clear();
goto qstn;
} else if (lname.empty()) {
cout << "Please enter your firstname!\n";
cin.clear();
goto qstn;
}
int lnamel = lname.length();
int strl = str.length();
int is = 0;
for (int i = 1; i < strl;) {
i++;
is++;
if (lname[i] == lname[is] && lname[i] == ' ' || lname[0] == ' ') {
cin.clear();
cout << "Please input your lastname properly!\n";
goto qstn;
}
}
// next question here
I'm having a hard time on thinking what will be the proper logic to avoid the
goto statement, since I was college I was using it but someone here said that
it's not good to use it at all cause it might ruin my code.
I tried using the do while loop but it's not smooth as goto.
Please help!
Here's an idiom I like to use:
int i;
if (std::cin >> prompt("enter an integer: ", i))
{
std::cout << "Read user input: " << i << "\n";
} else {
std::cout << "Input failed (too many attempts). Eof? " << std::boolalpha << std::cin.eof() << "\n";
}
Here, prompt is a smart input manipulator, that takes care of handling parse errors or stream failures and retrying.
It's quite generic so actually do many things, but you don't need to indicate all the options. When the manipulator is inserted into the stream it relays to the do_manip member:
template <typename Char, typename CharT>
friend std::basic_istream<Char, CharT>& operator>>(std::basic_istream<Char, CharT>& is, checked_input<T, Prompter>& manip) {
return manip.do_manip(is);
}
The do_manip handles all the logic without any gotos :) :
std::istream& do_manip(std::istream& is) {
auto attempt = [this] { return infinite() || retries_ > 0; };
while (attempt()) {
if (!infinite())
retries_ -= 1;
prompter_(out_);
if (is >> value_) {
if (!run_validators(out_))
is.setstate(is.rdstate() | std::ios::failbit);
else
break;
} else {
out_.get() << format_error_ << "\n";
}
if (attempt()) {
is.clear();
if (flush_on_error_)
is.ignore(1024, '\n');
}
}
return is;
}
You can see that there is a possibility to have validations run before accepting the input.
Here's a somewhat full-blown demo:
Live On Coliru
int main() {
using namespace inputmagic;
int i;
if (std::cin >> prompt("enter an integer: ", i)
.retries(3)
.flush_on_error(false)
.format_error("I couldn't read that (Numbers look like 123)")
.output(std::cerr)
.validate([](int v) { return v > 3 && v < 88; }, "value not in range (3,88)")
.validate([](int v) { return 0 == v % 2; })
.validate([](int v) { return v != 42; }, "The Answer Is Forbidden")
.multiple_diagnostics())
{
std::cout << "Read user input: " << i << "\n";
} else {
std::cout << "Input failed (too many attempts). Eof? " << std::boolalpha << std::cin.eof() << "\n";
}
}
You can see it will only accept valid integers
that are >3 and <88,
that are even
except 42 (forbidden number)
When entering the numbers 21, 42 and 10 on subsequent retries, you get: live
enter an integer: 21
Value not valid
enter an integer: 42
The Answer Is Forbidden
enter an integer: 10
Read user input: 10
However, if you enter 1 all the time you get this: live
enter an integer: 1
value not in range (3,88)
Value not valid
enter an integer: 1
value not in range (3,88)
Value not valid
enter an integer: 1
value not in range (3,88)
Value not valid
Input failed (too many attempts). Eof? false
Or if you read from a single line file: live
enter an integer: value not in range (3,88)
Value not valid
enter an integer: I couldn't read that (Numbers look like 123)
enter an integer: I couldn't read that (Numbers look like 123)
Input failed (too many attempts). Eof? true
Use a function:
bool getLastName(string & lname,
string & str)
{
cout << "Input customer's lastname: ";
getline(cin, lname);
if (lname.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ")
!= string::npos)
{
cout << "You can only input alpha here!\n";
cin.clear();
return false;
}
else if (lname.empty())
{
cout << "Please enter your firstname!\n";
cin.clear();
return false;
}
int lnamel = lname.length();
int strl = str.length();
int is = 0;
for (int i = 1; i < strl;)
{
i++;
is++;
if (lname[i] == lname[is] && lname[i] == ' ' || lname[0] == ' ')
{
cin.clear();
cout << "Please input your lastname properly!\n";
return false;
}
}
return true;
}
All I've done here is replace the gotos with return false. If the program makes it to the end of the function, return true. Make the function call in a while loop:
while (!getLastName(lname, str))
{
// do nothing
}
Not only does this de-spaghettify the code, but it breaks it up into nice, small, easy to manage pieces. This is called procedural programming.
A While loop looks like your best bet. You can redo the loop with the continue keyword.
int incorrect = 0;
while(!incorrect) {
cout<<"Input customer's lastname: ";
getline(cin,lname);
if(lname.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ")!=string::npos)
{
cout<<"You can only input alpha here!\n";
cin.clear();
continue;
}
else if(lname.empty())
{
cout<<"Please enter your firstname!\n";
cin.clear();
continue;
}
int lnamel=lname.length();
int strl=str.length();
int is=0;
for(int i=1; i<strl;)
{
i++;
is++;
if(lname[i]==lname[is]&&lname[i]==' '||lname[0]==' ')
{
cin.clear();
cout<<"Please input your lastname properly!\n";
continue;
}
incorrect = 1;
}
You need to use some do while loops
for instance this:
qstn:
cout<<"Input customer's lastname: ";
getline(cin,lname);
if(lname.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ")!=string::npos)
{
cout<<"You can only input alpha here!\n";
cin.clear();
goto qstn;
}
else if(lname.empty())
{
cout<<"Please enter your firstname!\n";
cin.clear();
goto qstn;
}
could be re-written as this:
int flag;
do{
flag = 1;
cout<<"Input customer's lastname: ";
getline(cin,lname);
if(lname.find_first_not_of( "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ")!=string::npos)
{
flag = 0;
cout<<"You can only input alpha here!\n";
}
else if(lname.empty())
{
flag = 0;
cout<<"Please enter your firstname!\n";
}
cin.clear();
} while( flag !=1 );
feel free to use a boolean type flag, it doesn't really matter
It seems to me that your code suffers from lack of clarity of purpose.
You apparently don't want the string that's entered to include a leading space, nor multiple consecutive spaces. Other than that, only alphabetic characters should be accepted.
If the user does enter multiple consecutive spaces, I'd probably just ignore all but the first. I'd probably write the code something like this:
#include <string>
#include <iostream>
#include <algorithm>
#include <cctype>
#include <sstream>
bool non_alpha(std::string const &s) {
return !std::all_of(s.begin(), s.end(), [](unsigned char c) { return std::isalpha(c) || std::isspace(c); });
}
std::string get_name(std::string const &prompt) {
std::string result;
std::string line;
do {
std::cout << prompt;
std::getline(std::cin, line);
} while (non_alpha(line));
std::istringstream words(line);
std::string word;
while (words >> word)
result += word + ' ';
return result;
}
int main() {
auto res = get_name("Please enter last name, alpha-only\n");
if (res.empty())
std::cout << "Oh well, maybe some other time";
else
std::cout << "Thanks Mr(s). " << res << "\n";
}
I'd be tempted to consider doing roughly the same for non-alphabetic characters--rather than asking the user to re-enter from the beginning, assume it's a mistake and just ignore it.

Reading string with spaces in c++

How can I read input line(type string) with whitespace? I tried getline but it goes into infinite loop. Following is my code.
#include <iostream>
#include <cstring>
#define MAX 50 //size of array
//Used G++ 4.6.3 compiler
using namespace std;
int main() {
struct Manager {
string name;
int age;
int working_years;
string phone;
int salary;
}info[MAX];
char inp; //To choose options
int array_pos = 0; //Current position in array of Manager structure
string search_name; //Manager name you want to search
cout << "Press 'i' to insert manager information or 's' to search for manager information by name or 'a' to abort: ";
cin >> inp;
while(inp != 'a') {
int search_num = 0; //array position at which search result is found
int found = 0;
if (inp == 'i' || inp == 's') {
if (inp == 'i') {
int k = array_pos;
cout << "Enter the information of the manager no "<<k+1<<" is : ";
cout << "Enter the Name : ";
//infinte loop occurs
getline(info[array_pos].name, '\n');
//cin >> info[array_pos].name;
cout<<"Enter manager age : ";
cin >> info[array_pos].age;
cout << "Enter manage working years : ";
cin >> info[array_pos].working_years;
cout << "Enter manager phone no. : ";
cin >> info[array_pos].phone;
cout << "Enter manager salary : ";
cin >> info[array_pos].salary;
array_pos++;
}
if (inp == 's') {
cout << "Enter the manager name you want to search : ";
cin >> search_name;
for(int i = 0; i < array_pos; i++) {
//using str1.compare(str2) to compare manager name
if(info[i].name.compare(search_name) == 0) { //manager name found in array of structure
found = 1;
search_num = i;
cout << "Name : " << info[search_num].name << "\n";
cout << "Age: " << info[search_num].age << "\n";
cout << "Working Years: " << info[search_num].working_years << "\n";
cout << "Phone No. : " << info[search_num].phone << "\n";
cout << "Salary : " << info[search_num].salary << "\n";
} //end of if loop for comparing string
} //end of for loop for searching
if(found == 0)
cout << "No Manager by this name exist in record" << "\n";
} //end of if loop
} //end of if loop for searching or insertion
if(inp == 'a')
break;
cout << "Press 'i' to insert manager information or 's' to search for manager information by name or 'a' to abort: ";
cin >> inp;
} //end of while loop
return 0;
}
How can I read input line(type string) with whitespace?
std::string line;
if (std::getline(std::cin, line)) {
...
}
Note that apart from checking the return value of std:getline call, you should also avoid mixing >> operator with std::getline calls. Once you decide reading the file line by line, it seems to be cleaner and more reasonable to just make one huge loop and do the additional parsing while using string stream object, e.g.:
std::string line;
while (std::getline(std::cin, line)) {
if (line.empty()) continue;
std::istringstream is(line);
if (is >> ...) {
...
}
...
}
Simplest way to read string with spaces without bothering about std namespace is as follows
#include <iostream>
#include <string>
using namespace std;
int main(){
string str;
getline(cin,str);
cout<<str;
return 0;
}
Solution #1:
char c;
cin >> noskipws; // Stops all further whitespace skipping
while (cin >> c) { // Reads whitespace chars now.
count++;
}
Solution #2:
char c;
while (cin.get(c)) { // Always reads whitespace chars.
count++;
}

Vaildator for a string on user input

Hey i want to stop the user from entering integers when i ask the user to input a name. I have achieved this for an integer and a char. Can anyone help me adapt my code for a string.
int getNum()
{
int num;
std::cout << "\nWhat is your age? ";
while (!(std::cin >> num))
{
// reset the status of the stream
std::cin.clear();
// ignore remaining characters in the stream
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Enter an *integer*: ";
}
std::cout << "You entered: " << num << std::endl;
return num;
}
char getChar(string q)
{
char input;
do
{
cout << q.c_str() << endl;
cin >> input;
}
while(!isalpha(input));
return input;
}
string q = "This is a test123";
for(string::iterator i = q.begin(); i != q.end(); i++)
{
if((*i < 'A' || *i > 'z') && (*i != ' '))
{
return false;
}
}
would also be an option, if you allow for spaces and other characters.
Edit: updated for checking a single char:
char c;
bool finished = false;
printf("Please enter your sex, M/F?\n");
while(!finished)
{
cin >> c;
if(!(c == 'm' || c == 'M' || c== 'f' || c=='F'))
{
printf("Please try again...\n");
}
else
{
finished = true;
}
}
Note that c is only input, char by char, when Enter is pressed, before that the line feed does not happen.
If you plan on using std:string, then you can use this to find if the entered string has any digits or not:
if (std::string::npos != s.find_first_of("0123456789"))
{
std::cout << "digit(s)found!" << std::endl;
}