如何获取用户输入并将其转换为switch语句的字符C ++

时间:2018-09-23 22:43:58

标签: c++

我对C ++编程非常陌生,因此为了练习和做笔记,我正在创建一个回答非常基本问题的项目,到目前为止看起来像这样;

#include<iostream> 
#include<string>
#include<cstdlib>


int main()
{
        int n = 1;
        char y;

        while (n < 5)
        {

               std::cout << ("What is your question\n");
            std::string ans;
            std::cin >> ans;
            std::cout << ("----------------------------------------------------------------------------------------------------------------------\n");

            if (ans == "comments")
            {
                y = 1;
            }
            else 
            {
                y = 0;
            }



            switch (y)
            {
                case '1':
                    std::cout << ("\nUse // for single line comments and /* */ for multiple lines\n\n");
                    break;

        case '2':
                    std::cout << ("\nTo start add the library  #include<iostream in most cases, add more if necassary\n\n");
                    break;

        case '3':
                    std::cout << ("\nAfter the libraries are added you must initiate program by using \n int main() \n { \n\n } \n all code must fit in between the curly braces \n\n");
                    break;

        case '4':
                    std::cout << ("\nVariables hold number ");

        default:
                    std::cout << ("\ninvalid word\n\n");

        }
    std::cout << ("----------------------------------------------------------------------------------------------------------------------\n");


    std::cout << ("Type 1 to repeat \nType 15 to end\n\n");
    std::cin >> n;
    std::cout << ("----------------------------------------------------------------------------------------------------------------------\n");

      }
            return 0;
}

if / else语句正在提取您输入的“注释”短语,之前我已经使用cout语句对此进行了测试,问题在于它没有更改char'y'。做成int时,确实会更改它,但是开关不会拾取它,有任何建议吗?

0 个答案:

没有答案