Hi all, in a part of a program I have to restrict user to input only letters and digits in a string, so I did this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
string name;
bool test;
do {
test = true;
cout << "\n\n\tEnter name: ";
cin >> name;
for (unsignedshort n = 0; n <= name.length()-1; n++){
if (!(isdigit(name.at(n)) || isalpha(name.at(n))))
test = false;
}
if (!test)
cout << "\n\n\t!ERROR!\n\n";
}
while(!test);
it looks pretty simple and straight forward but unfortunately it accepts space as a valid choice (which I'm not allowed).
And if I stick it into my program (I kept it separately so I don't mess up to much) and I input space it just executes all the loop at once and the program is finished.
So I added this:
1 2
if (isspace(name.at(n)))
test = false;
to see if that will help but it does absolutely nothing..
To finish my way to long post I will just say this:
HELP PLEASE!
Actually it is not. operator>> cannot extract whitespaces on default settings. It will stop as soon as it encounters one. Anything after it will remain in input buffer for subsequent reads. So there is no possibility that there would be spaces in your string, so this test does nothing.
#include<iostream>
#include<iomanip>
usingnamespace std;
int main ()
{
unsignedshort numJudges, numCeleb;
float score, addScore, finalScore = 0, min, max, max2=0;
string name, winner;
do {
cout << "\n\n\tEnter number of judges: ";
cin >> numJudges;
if (numJudges < 3 || numJudges > 6)
cout << "\n\n\tERROR! Number of judges must be between 3 and 6" << endl;
}
while (numJudges < 3 || numJudges > 6);
do {
cout << "\n\n\tEnter number of paricipants: ";
cin >> numCeleb;
if (numCeleb < 10 || numCeleb > 15)
cout << "\n\n\tERROR! Number of participants must be between 10 and 15" << endl;
}
while (numCeleb < 10 || numCeleb > 15);
for (unsignedshort i = 1; i <= numCeleb; i++){
bool test;
do {
test = true;
cout << "\n\n\tEnter name of celebrity " << i << " : ";
getline (cin, name, '\n');
for (unsignedshort n = 0; n <= name.length() - 1; n++){
if(!(isdigit(name.at(n)) || isalpha(name.at(n))))
test = false;
}
if (!test)
cout << "\n\n\tERROR! Name can consist of letters and numbers only.\n";
}
while (!test);
it goes as far as asking for the name and then get : terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::at
any idea why? it works when I keep that part of a program separate so I just copied it to my main program but then it crashes..
First problem (crash): n <= name.length() - 1 should be canonical n < name.length()
Second problem (getline does not get proper input): http://stackoverflow.com/a/21567292
In short: std::getline(std::cin >> std::ws, name);
I think I've gotten this error before. Like an array, if you go pass the amount of elements an array, you're going to go out of bounds. So I recommend you check what's going with one of your strings.
I changed this to n < name.length () but it didn't help.
I also checked the link you gave above but i just don't get it.
I'm all new to this and we just had like 2 classes on string and that was the only way to getline I know.
But anyway I copied what you gave me there and this time I could actually put in the name and then I got the same 'terminate...'
Ok I think I've found the problem. This is a cin and getline problem. Your program is skipping the input(particularly the name input). Here is an example with an output.
Enter integer. 10
Enter string.
10
Process returned 0 (0x0) execution time : 1.565 s
Press any key to continue.
So you see, your program is skipping your input. This means that your string is empty.
There are many ways to fix this.
first try to use cin.ignore.
If that doesn't work,try cin.ignore(numericlimit<streamsize>::max(, '\n'); // Probably spelled this function but it's in the limits header file so you can look it up.
if that doesn't work, then use a dummy variable to eat up the input buffer.
There are more possible courses of action that you can take to fix this problem but these are the ones I mainly use.
Yes I get exactly the same error message. And I did copy your line, but the only think that changed was that this time I was able to input the name and then I got the same error.
deathslice how come str outputs the same as x?
I'm sorry if I'm a bit of a pain, but I am a true beginner and loads of things do not make any sense to me. You would think that if I get assignment, I shouldn't be expected to do anything different than I learned in class, but now they forgot to tell me that there are different ways of getting a line..
ebunca, str doesn't output the same thing as x(a string can't accept an integer unless you parse it). You can test it out if you want to see for your self. Maybe after you do that, you'll understand your problem better.
In essence, once you enter in a value for x, the input buffer for the getline function gets eaten up and skipped; outputting an empty string.
*Important---Why is your for loop missing a curly bracket( } ) on line 29?
MiiNiPaa I did change them but because I see any improvement I changed it back, the reason I didn't see the improvement is (haha I feel so silly) I actually put in std:: newbies right?
I appreciate you putting up with me, it works now. Thank you for putting your time in it.
death I think I know what your saying I did try it I just misinterpreted it. I see in only puts out x and nothing for string. So is the 'cin >> sw' a normal solution, or there is more surprises waiting for me related to strings.
Just as I suspected. I ran your code on c++ shell and as soon as I got to line 36, the program terminated because it skipped that line of code. That is another problem you have beside the one at line 59.
MiiNiiPaa fixed line 36 for me, it is fine now.
I appreciate your input, I would never guess that the program would just skip the line. We only did programs with like 1 input of string and than manipulate it. So that's good to know.