English 中文(简体)
我的法典有什么错误? 我怎么能够确定这一点? C++
原标题:What s wrong with my code? How can I fix it? C++
  • 时间:2011-11-10 09:18:05
  •  标签:
  • c++
    #include <iostream>
    #include <vector>
    using namespace std;

    int main(){

    vector<string> row(7, "0");
    char input;
    int pos;

    cin >> input;
    cin >> pos;

    if(input ==  X )
        row[pos] =  1 ;

    //ascii value of 1 is 49

    if(row[pos] - 49 == 0)
        cout << "Correct";

    return 0;
    }

我在上一份<代码>if说明中留下了一个错误。 它为什么不发挥作用,我如何加以纠正? 这样,<代码>rowhas就成为一种扼杀性病媒,我就能够将其变成任何其他类型。

参看row[pos],以在/code>上加入

问题回答

你们可以绕开 in和扼杀。

1. 使用特性和改为:

char ch = row[pos][0];
int asciiCode = (int)ch;
if (asciiCode == 49) {
   cout << "Correct";
}

我猜想你想比较案件的第一封信,在这种情况下,你有两个选择:

if(row[pos][0] - 49 == 0)
        cout << "Correct";

if(*row[pos].c_str() - 49 == 0)
        cout << "Correct";

试图从<代码>std:string<>/code>中添加“<>t

row[pos] is a string because row is a vector of strings. If you want row to be a string and row[pos] to be a char, declare row as a string. Or perhaps you meant f或row to be a vect或of chars.

最后一个缩略语的问题是:row[pos]string。 因此,你可以从中减去49个。 你们所希望的是:

if(row[pos][0] - 49 == 0)

更可论证的是,这样做是:

if(row[pos][0] -  1  == 0)




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?