[RESOLVED] Char array variables in if staments ---- a beginner's experience
Dear all,
I'm a beginner learning C++ and I'm using the book Jumping into C++ - which by the way is really good. I'm currently doing the exercises for the fifth chapter (Loops) and I've done all of them, but I wanted to go the extra mile on the last program I'm supposed to design. The program is a poll and all the input from the user will be with numbers. However when a letter is pressed then of course you get wrong behaviour from the program, it keeps looping endlessly.
Here is a fragment of what I think is the way of doing it - but of course it's not working :(
Code:
int p = 0
char anyLetter[]={"abcde"}; //Initializing char variable
char a = anyLetter[p];
else if (userAnswer[n] == a) //if statement where char needs to be used.
{
cout << "Pressing a letter maybe? It's only with numbers. Try again." << endl;
continue;
}
Any ideas? suggestions? Thanks guys :D
Re: Char array variables in if staments ---- a beginner's experience
How are you inputting numbers? Are you using cin? If you are, then you can do something this like to test if input is valid or not
Code:
int i;
string t;
if (!(cin >> i)) {
cout << "Not a number!" << endl;
cin.clear();
cin >> t;
} else {
cout <<"A number" << endl;
}
If the input stream is expecting a number but gets something else then it sets the state of the stream to 'fail' which is tested with the if statement around the cin. If the input stream has 'failed', then the clear method is used to reset the input stream to 'good' ready for the next input and the 'bad ' input is read into t.
Hope this helps. If you're not using cin for input, post the code you are using and we'll provide more advice.
Re: Char array variables in if staments ---- a beginner's experience
Thanks 2kaud for your soon reply :)
That's what I needed! but since I already had an if statement withing a while loop then what I did was to include your solution as part of that statement, so the program stops with your advice :D
this is how it is at the moment:
if (userAnswer [n] == 0 || userAnswer[n] >= 4 || (!(cin>>userAnswer[n])))
break;
Thank you so much again
:D
Re: Char array variables in if staments ---- a beginner's experience
Quote:
Originally Posted by
FelixCast
Here is a fragment of what I think is the way of doing it
Please post self-contained fragments only. You have an else-if with no if and a continue outside of a loop. Nobody can make sense of a 'fragment' such as this.
Quote:
Originally Posted by
FelixCast
this is how it is at the moment:
if (userAnswer [n] == 0 || userAnswer[n] >= 4 || (!(cin>>userAnswer[n])))
break;
That doesn't make a whole lot of sense. Why are you testing the number before it is inputted by the user?
Parsing user input directly with std::cin is a bit awkward. It's easiest to always read an entire line into a string using std::getline and then parse the line using a std::istringstream.
Also see http://www.parashift.com/c++-faq/str...t-failure.html (and subsequent sections).
Re: Char array variables in if staments ---- a beginner's experience
Thanks D Drmmr for your sound advise.
As I said before, I'm just a beginner who wants to learn the right ways and the best ways of doing things. And you're absolutely right, giving you fragments of code without the real 'context' of the program makes things worse.
It won't happen again. I've just read your link and that solved all the issues I had with my loop and the program itself. Thanks a lot :).
It can't be denied that forums allow us neophytes to improve and learn.
Thanks again.
Re: Char array variables in if staments ---- a beginner's experience
Quote:
Originally Posted by
FelixCast
As I said before, I'm just a beginner who wants to learn the right ways and the best ways of doing things.
Well, IMO using std::cin is never "the best way", it's just a convenient way to start learning to program C++ without having to get overwhelmed with things like a GUI framework. It's been a long time since I've written a program that used std::cin, except in small toy/testing programs (and then mostly only to pause the program). Real-world programs either get all their input from the command line and/or file(s), or if they need user interaction, get a full-blown GUI.
So, my advice concerning std::cin would be to not sweat the small stuff. It's good to know its quirks, just in case, but I wouldn't spend to much time trying to use it in "the best way".
Quote:
Originally Posted by
FelixCast
And you're absolutely right, giving you fragments of code without the real 'context' of the program makes things worse.
It won't happen again.
No worries. Creating a small, but complete example of the issue you have is not only useful to those reading your post, it's also a useful exercise for yourself, as you'll often notice things about your own code you didn't notice before. Sometimes it'll even help you answer your own question.