C矩阵字符无法正确打印

时间:2018-04-15 12:37:09

标签: c matrix char printf

我正在尝试在C中填充char矩阵并在C中打印它,但我只得到奇怪的字符。我在Windows上运行此程序,因此class Login extends CI_Controller { public function __construct() { parent::__construct(); $this->load->helper('url'); $this->load->library('session'); } /** * Check if the user is logged in, if he's not, * send him to the login page * @return void */ public function index() { if ($this->session->userdata('is_logged_in')== TRUE) { $refered_from = $this->session->userdata('refered_from'); redirect($refered_from,'refresh'); } else { $this->load->view('login_page'); } } /** * check the username and the password with the database * @return void */ function validate_credentials() { $this->load->model('Login_model'); $user_name = $this->input->post('uname'); $password = $this->input->post('password'); $is_valid = $this->Login_model->validate($user_name, $password); if ($is_valid) { $data = array( 'uname' => $user_name, 'is_logged_in' => TRUE ); $this->session->set_userdata($data); redirect('/Dashboard'); } else { // incorrect username or password $data['message_error'] = TRUE; $this->load->view('login_page', $data); } } }

class Dashboard extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('session');
    }

    public function index() {
        if ($this->session->userdata('is_logged_in') == TRUE) {
            $data['page_title'] = "Dashboard";
            $data['main_content'] = 'Dashboard';
            $this->load->view('includes/template', $data);
            $this->session->set_userdata('refered_from', current_url());
        } else {
            redirect('/Login', 'refresh');
        }
    }

}

我尝试使用system(cls);#include <stdio.h> #include <stdlib.h> #include <string.h> void paint(char tab[10][10], int lignes, int colonnes) { system("cls"); for (int i = 0; i < lignes; i++) { for (int j = 0; j < colonnes; j++) { printf("%c", tab[i][j]); } printf("\n"); } } void create(char tab[10][10], int lignes, int colonnes) { for (int i = 0; i < lignes; i++) { for (int j = 0; j < colonnes; j++) { tab[i][j] = ' '; if (i == 0 || i == lignes--) tab[i][j] = 205; if (j == 0 || j == colonnes--) tab[i][j] = 186; if (i == 0 && j == 0) tab[i][j] = 201; if (i == 0 && j == colonnes--) tab[i][j] = 187; if (i == lignes-- && j == 0) tab[i][j] = 200; if (i == lignes-- && j == colonnes--) tab[i][j] = 188; if (i == 50 && j == 50) tab[i][j] = 248; } } } int main() { char tab[10][10]; create(tab, 10, 10); paint(tab, 10, 10); char i; while(scanf(" %c", &i) != 'q') { } } 更改printf中的输出类型,如此处的其他答案所示,但%d显示随机数,%s使程序崩溃,我不知道是不是因为%d某个地方。我也尝试使用简单的字符来填充我的矩阵,而不是它们的ascii值。

不介意无效的scanf,我现在仍然试图找出键盘输入而不使用我自己的输入。

1 个答案:

答案 0 :(得分:0)

您的代码存在一些问题,例如colonnes--在检查colonnes声明时更改if的值

我相信你想画一个矩形

试试这个(它是你自己的代码,修改了一下)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


char tab[10][10];

void paint(int lignes, int colonnes)
{
    system("cls");
    for (int i = 0; i < lignes; i++)
    {
        for (int j = 0; j < colonnes; j++)
        {
            printf("%c", tab[i][j]);
        }
        printf("\n");
    }
}

void create(int lignes, int colonnes)
{
    for (int i = 0; i < lignes; i++)
    {
        for (int j = 0; j < colonnes; j++)
        {
            tab[i][j] = ' ';
            if (i == 0 || i == lignes-1)
                tab[i][j] = 205;
            if (j == 0 || j == colonnes-1)
                tab[i][j] = 186;
            if (i == 0 && j == 0)
                tab[i][j] = 201;
            if (i == 0 && j == colonnes-1)
                tab[i][j] = 187;
            if (i == lignes-1 && j == 0)
                tab[i][j] = 200;
            if (i == lignes-1 && j == colonnes-1)
                tab[i][j] = 188;
            if (i == 50 && j == 50)
                tab[i][j] = 248;
        }
    }
}

int main()
{
    create(10, 10);
    paint(10, 10);

}