查找等于输入的字符

时间:2018-12-09 18:28:08

标签: c string char

在下面的代码中,我从用户处获取输入,然后通过将其与其他数字进行比较来找到引入的数字。


这是我的代码:

//sudo g++ -o sdl main.cpp -lSDL2_image -lGL -lGLU -lglut -lX11 -lGLEW `sdl2-config --cflags --libs`
#include <iostream>
#include <GL/glew.h>
#include <glm/glm.hpp>
#include "glm/gtc/matrix_transform.hpp"
#include <glm/gtc/type_ptr.hpp>
#include <SDL2/SDL.h>
#include <string>
#include <GL/gl.h>



std::string programName = "SDL2/OpenGL";
SDL_Window *mainWindow;
SDL_GLContext mainContext;

void Calculate()
{
    float radius = 2.0f;
    float camX = sin(SDL_GetTicks()*0.001) * radius;
    float camZ = cos(SDL_GetTicks()*0.001) * radius;

    glm::mat4 perspecive_mat = glm::perspective(
        45.0f, 1.0f / 1.0f, 0.1f, 100.0f );

    glm::mat4 view_mat = glm::lookAt(
        glm::vec3(camX, 0.0, camZ), glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0) );

    glMatrixMode(GL_PROJECTION);
    glLoadMatrixf(glm::value_ptr(perspecive_mat));

    glMatrixMode(GL_MODELVIEW);
    glLoadMatrixf(glm::value_ptr(view_mat));
}


void Render()
{
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    float vertexCoords[24] = {  // Coordinates for the vertices of a cube.
               1,1,1,   1,1,-1,   1,-1,-1,   1,-1,1,
              -1,1,1,  -1,1,-1,  -1,-1,-1,  -1,-1,1  };

    float vertexColors[24] = {  // An RGB color value for each vertex
               1,1,1,   1,0,0,   1,1,0,   0,1,0,
               0,0,1,   1,0,1,   0,0,0,   0,1,1  };

    int elementArray[24] = {  // Vertex numbers for the six faces.
              0,1,2,3, 0,3,7,4, 0,4,5,1,
              6,2,1,5, 6,5,4,7, 6,7,3,2  };

    glVertexPointer( 3, GL_FLOAT, 0, vertexCoords );
    glColorPointer( 3, GL_FLOAT, 0, vertexColors );

    glEnableClientState( GL_VERTEX_ARRAY );
    glEnableClientState( GL_COLOR_ARRAY );

    glDrawElements( GL_QUADS, 24, GL_UNSIGNED_INT, elementArray );


    SDL_GL_SwapWindow(mainWindow);
}

bool Loop()
{
    while (true ){
        SDL_Event event;
        while ( SDL_PollEvent( &event ) ){
            switch ( event.type ){
                case SDL_QUIT :
                SDL_Quit();
                return 0;
                case SDL_KEYDOWN : 
                    std::cout<<"Key Down"<<std::endl;
                    break;
                case SDL_KEYUP :
                    std::cout<<"Key Up"<<std::endl;
                    break;
                case SDL_MOUSEBUTTONDOWN :
                case SDL_MOUSEBUTTONUP :
                case SDL_MOUSEMOTION :
                default :
                    break;
            }
        }

        Calculate();

        Render();
    }
}


void CheckSDLError(int line = -1){
    std::string error = SDL_GetError();

    if (error != "")
    {
        std::cout << "SLD Error : " << error << std::endl;

        if (line != -1)
            std::cout << "\nLine : " << line << std::endl;

        SDL_ClearError();
    }
}


void Cleanup(){
    SDL_GL_DeleteContext(mainContext);
    SDL_DestroyWindow(mainWindow );
    SDL_Quit();
}

int main(int argc, char *argv[]){
    if (SDL_Init(SDL_INIT_VIDEO) < 0){
        std::cout << "Failed to init SDL\n";
        return false;
    }
    mainWindow = SDL_CreateWindow(programName.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,512, 512, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
    if (!mainWindow )
    {
        std::cout << "Unable to create window\n"<< std::endl;;
        CheckSDLError(__LINE__);
        return false;
    }

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    //SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    mainContext = SDL_GL_CreateContext(mainWindow );

    // This makes our buffer swap syncronized with the monitor's vertical refresh
    //      ( which means it enables v-sync)
    // Setting this to 0 will disable V-sync
    //      Which means our application could run at unlimited fps
    SDL_GL_SetSwapInterval(1);
    // Init GLEW
    glewExperimental = GL_TRUE; 
    glewInit();
    // Enable blending so that we can have transparanet object
    glEnable(GL_BLEND ) ;
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    // Enable depth testing so that closer triangles will hide the triangles farther away
    glEnable( GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    Loop();
    Cleanup();

    return 0;
}

我该如何检查字母?假设用户输入字母G,然后我将其循环进行比较以将其与其他每个字符进行比较,直到找到等于它的那个字符为止。

1 个答案:

答案 0 :(得分:1)

您可以使用几乎相同的代码,只需扫描char

char input;
scanf("%c", &input);

要检查输入,请使用最符合您期望的内容。 switch可能是一个不错的选择:

switch (input) {
case 'G':
    printf("G was received\n");
    break;
default:
    printf("Uninteresting character %c was received\n", input);
}