[RESOLVED] Char array variables in if staments ---- a beginner's experience
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [RESOLVED] Char array variables in if staments ---- a beginner's experience

  1. #1
    Join Date
    Mar 2013
    Posts
    6

    [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

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,556

    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.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Mar 2013
    Posts
    6

    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

    this is how it is at the moment:

    if (userAnswer [n] == 0 || userAnswer[n] >= 4 || (!(cin>>userAnswer[n])))
    break;

    Thank you so much again


  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,014

    Re: Char array variables in if staments ---- a beginner's experience

    Quote Originally Posted by FelixCast View Post
    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 View Post
    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).
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  5. #5
    Join Date
    Mar 2013
    Posts
    6

    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.

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,014

    Re: Char array variables in if staments ---- a beginner's experience

    Quote Originally Posted by FelixCast View Post
    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 View Post
    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.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center