CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2017
    Location
    sc
    Posts
    5

    Tinkering on verifying a stream of spaces, letter and punctuations

    Code:
    #include <iostream>
    #include <cctype>
    #include <string>
    
    std::string getLetters()
    {
        std::string input;
        bool valid;
    
        do
        {
    std::cout<<"\n Enter you name of the experiment:";       
    std::getline(std::cin,input);       // get a line of input
    
            valid = true;                       // assume it's valid
            for(auto& i : input)                // check each character in the input string
            {
                if(!std::isalpha(i))            // is it an alphabetical character?
                {
                    valid = false;              // if not, mark it as invalid
                                        // print an error to the user
                    std::cout << "Invalid input.  Please input only alphabetical characters." << std::endl;
                    break;              // break out of the for() loop, as we have already established the input is invalid
                }
            }
        }while(!valid);     // keep going until we get input that's valid
    
        return input;       // once we have valid input, return it
    }
    The code above is not working when I run it. It terminates with a screen shot below. I am using code::block13.12 for compiler.
    Attached Images Attached Images   

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Tinkering on verifying a stream of spaces, letter and punctuations

    [Thread moved as not c++/cli]
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Tinkering on verifying a stream of spaces, letter and punctuations

    How are calling/using getLetters() as this works OK with VS2017.
    Code:
    #include <iostream>
    #include <cctype>
    #include <string>
    
    std::string getLetters()
    {
    	std::string input;
    	bool valid;
    
    	do
    	{
    		std::cout << "\n Enter you name of the experiment:";
    		std::getline(std::cin, input);       // get a line of input
    
    		valid = true;                       // assume it's valid
    		for (auto& i : input)                // check each character in the input string
    		{
    			if (!std::isalpha(i))            // is it an alphabetical character?
    			{
    				valid = false;              // if not, mark it as invalid
    											// print an error to the user
    				std::cout << "Invalid input.  Please input only alphabetical characters." << std::endl;
    				break;              // break out of the for() loop, as we have already established the input is invalid
    			}
    		}
    	} while (!valid);     // keep going until we get input that's valid
    
    	return input;       // once we have valid input, return it
    }
    
    int main()
    {
    	std::cout << getLetters() << std::endl;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured