代码没有表达意图 [英] Code not performing intention

查看:70
本文介绍了代码没有表达意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  //   ConsoleApplication5.cpp:定义控制台应用程序的入口点。  
//
#include stdafx.h
#include < windows.h >
#include < iostream < span class =code-keyword>>
#include < span class =code-keyword>< string.h >
#include < string >
#include < cstdlib >


// aray variables
char ri_ght;
char bottom;
char le_ft;

// 临时变量存储
// 临时变量存储

// 数组变量的结尾

使用 命名空间标准;

字符串映射[ 6 ] [ 6 ];

int printmap(){

string top = X;

// 顶部边框
map [ 0 ] [ 0 ] = top;
map [ 1 ] [ 0 ] = top;
map [ 2 ] [ 0 ] = top;
map [ 3 ] [ 0 ] = top;
map [ 4 ] [ 0 ] = top;
map [ 5 ] [ 0 ] = top;
map [ 6 ] [ 0 ] = top;
// 上边框

// 右侧
map [ 6 ] [< span class =code-digit> 1 ] = ri_ght;
map [ 6 ] [ 2 ] = ri_ght;
map [ 6 ] [ 3 ] = ri_ght;
map [ 6 ] [ 4 ] = ri_ght;
map [ 6 ] [ 5 ] = ri_ght;
map [ 6 ] [ 6 ] = ri_ght;
// 右侧

// bottom
map [ 5 ] [ 6 ] = bottom;
map [ 4 ] [ 6 ] = bottom;
map [ 3 ] [ 6 ] = bottom;
map [ 2 ] [ 6 ] = bottom;
map [ 1 ] [ 6 ] = bottom;
map [ 0 ] [ 6 ] = bottom;
// bottom

// left
map [ 0 ] [ 5 ] = le_ft;
map [ 0 ] [ 4 ] = le_ft;
map [ 0 ] [ 3 ] = le_ft;
map [ 0 ] [ 2 ] = le_ft;
map [ 0 ] [ 1 ] = le_ft;
// left

// 打印部分

cout<< map [ 0 ] [ 0 ],map [ 1 ] [ 0 ],map [ 2 ] [ 0 ],map [ 3 ] [ 0 ],map [ 4 ] [ 0 ],map [ 5 ] [< span class =code-digit> 0 ],map [ 6 ] [ 0 ]。
cout<< << endl;

return 0 ;
}

int main()
{
string test;
string end1;

cout<< 测试打印<< endl;
cin>>测试;

if (test == ){
printmap;
}

cin>> END1;

return 0 ;
}





 





___________________________________________



对不起,我对编程有点新意。



我的意图是,如果用户键入yes,它将调用函数printmap并将遵循该代码并打印出它被告知的数组中的值,但事实并非如此。没有错误,当你输入是的时候控制台没有做任何事情。



我很感激任何建议。

解决方案

C ++ 函数调用中必须使用圆括号。

更改自

引用:



if(test ==yes){

printmap;

}

to

  if ( test ==   yes){
printmap();
}







您还应该更改

< blockquote class =FQ>

Quote:

string map [6] [6];

to

 string map [7] [7]; 



以避免缓冲区溢出。





最后这一行,可能不是你真正想写的,它是

 cout<< map [0] [0],map [1] [0],map [2] [0],map [3] [0],map [4] [0],map [5] [0],map [ 6] [0]; 



输出最后一项( map [6] [0] )(参见维基百科的逗号运算符 [ ^ ])。

您可能打算:

 COUT<< map [ 0 ] [ 0 ]<<  << map [ 1 ] [ 0 ]<<  < < map [ 2 ] [ 0 ]<<  << map [ 3 ] [ 0 ]<<  << map [ 4 ] [ 0 ]<<  << map [ 5 ] [ 0 ]<<  << map [ 6 ] [ 0 ]; 

你看看你的警告了!

几乎可以肯定的是:

警告C4551:函数调用缺少参数列表

在这一行:

 printmap; 

Beasue你没有执行方法,你只是指方法名称。

改为:

  if (test ==  ){
printmap();
}

警告将消失,您的代码将正常运行。好吧,它会更好一点......它还没有!



我强烈建议您在编译选项中添加/ WX选项直到你对语言更熟悉 - 它会让你在它们成为运行时错误之前修复这样的问题!


// ConsoleApplication5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <string.h>
#include <string>
#include <cstdlib>


//aray variables
char ri_ght;
char bottom;
char le_ft;

//temp variable storage
//temp variable storage

//ending of array variables

using namespace std;

string map[6][6];

int printmap(){

    string top = "X";

    // top border
    map[0][0] = top;
    map[1][0] = top;
    map[2][0] = top;
    map[3][0] = top;
    map[4][0] = top;
    map[5][0] = top;
    map[6][0] = top;
    //top border

    //right side
    map[6][1] = ri_ght;
    map[6][2] = ri_ght;
    map[6][3] = ri_ght;
    map[6][4] = ri_ght;
    map[6][5] = ri_ght;
    map[6][6] = ri_ght;
    //right side

    //bottom
    map[5][6] = bottom;
    map[4][6] = bottom;
    map[3][6] = bottom;
    map[2][6] = bottom;
    map[1][6] = bottom;
    map[0][6] = bottom;
    //bottom

    //left
    map[0][5] = le_ft;
    map[0][4] = le_ft;
    map[0][3] = le_ft;
    map[0][2] = le_ft;
    map[0][1] = le_ft;
    //left

    //print part

    cout<< map[0][0], map[1][0], map[2][0], map[3][0], map[4][0], map[5][0], map[6][0];
    cout<< "" <<endl;

    return 0;
}

int main()
{
    string test;
    string end1;

    cout<< "Test Print"<<endl;
    cin>> test;

    if (test=="yes"){
        printmap;
    }

    cin>> end1;

    return 0;
}





___________________________________________

Sorry, I'm somewhat new to programming.

My intentions were if the user typed yes that it would call the function printmap and would follow that code and print out the values in the array it was told but it is not. There are no errors and the console does not doing anything when you type in yes.

I appreciate any advice.

解决方案

In C++ round braces are mandatory in function calls.
Change from

Quote:


if (test=="yes"){
printmap;
}

to

if (test=="yes"){
    printmap();
}




You should also change from

Quote:

string map[6][6];

to

string map[7][7];


in order to avoid buffer overrun.


Finally this line, probably is not what you actually wanted to write, it

cout<< map[0][0], map[1][0], map[2][0], map[3][0], map[4][0], map[5][0], map[6][0];


only outputs the last item (map[6][0]) (see comma operator at Wikipedia[^]).
You possibly intended:

cout<< map[0][0] << " " << map[1][0] << " " < < map[2][0] << " " << map[3][0] << " " << map[4][0] << " " << map[5][0] << " " << map[6][0];


You have look at your warnings!
You have almost certainly got something like:

warning C4551: function call missing argument list

on this line:

printmap;

Beasue you are not executing the method, you are just referring to the method name.
Change it to:

if (test=="yes"){
    printmap();
}

And the warning will go away, and your code will work. Well, it'll work a little better...it's not there yet!

I would very strongly suggest you add the "/WX" option to your compilation options until you are a lot more familiar with the language - it will make you fix problems like this before they become a runtime error!


这篇关于代码没有表达意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆