c++ - C++中的运行时错误字符串声明

标签 c++ gcc compiler-construction compilation compiler-errors

您好我有这样的代码:

#include <iostream>
#include <cstdio>
using namespace std;  
int main () {
    std::string s="fawwaz"; 
  ...
}

然后我使用从gcc.gnu.org下载的gnu gcc在线安装程序,用G++对其进行了编译,该编译运行没有任何错误和警告,但是当我运行时,出现错误“程序a.exe已停止工作”。
并且程序运行无任何错误。然后,我尝试编译原始文件(在字符串声明的前面没有双反斜杠),程序已编译并成功运行。

有什么解决方案?哪里出问题了?他们有什么办法解决我的问题,以便我可以通过命令行而不是Microsoft Visual C++来编译程序,因为通过命令行编译会更快? :D

谢谢

这是完整的代码:
     #include <cstdio>
     #include <iostream>
     #include <fstream>
     #include <vector>
     #include <string>

     using namespace std;

     void Cetak_Puzzle_Start(){

     }

     int main(int argc, char const *argv[])
     {
        string s;
        ifstream file("input.txt");
        vector<vector<int> > Puzzle_Start;
        vector<vector<int> > Puzzle_Finish;
        int Puzzle_size=0;

        /*
        * RETRIEVE PUZZLE SIZE
        **/
        getline(file,s);
        for (int i = 0; i < s.length(); ++i)
            Puzzle_size= (Puzzle_size*10) + (int) (s[i]-'0');

        /*
        * Set Zero ukuran 3x3 vector Puzzle start dan Puzzle finish
        **/
        vector<int> vtemp(Puzzle_size,0); 
            for (int i = 0; i < Puzzle_size; ++i)
            {
            Puzzle_Start.push_back(vtemp);
            Puzzle_Finish.push_back(vtemp);
            }

            /*
            * RETRIEVE START STATE 
        **/
        getline(file,s);
        int m=0,n=0; // dummy var for looping only m:pointer baris, n:pointer kolom, 
        for (int i = 0; i < s.length(); ++i)
            if (n<Puzzle_size){
                if (s[i]=',')
                    n++;
                else if (s[i] >= '0' && s[i] <='9')
                    Puzzle_Start[m][n]= (Puzzle_Start[m][n] * 10) +(int) (s[i]-'0');
                else if (s[i] ='B')
                    Puzzle_Start[m][n]=-1;
             }else{
                 n=0; // Ganti baris
                 m++;
            }

        fclose(stdin);


        /*
        * CETAK PUZZLE
        **/
        // for (int i = 0; i < Puzzle_Start.size(); ++i){
        //  for (int j = 0; j < Puzzle_Start[i].size(); ++j)
        //      printf("%d ",Puzzle_Start[i][j]);
        //  printf("\n");
        // }
        return 0;

     }

最佳答案

这是错误,

if (s[i]=',')

应该
if (s[i]==',')


else if (s[i]='B')

应该
else if (s[i]=='B')

混淆=(分配)和==(相等)是一个很常见的错误

关于c++ - C++中的运行时错误字符串声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19134035/

相关文章:

c - 在 linux 中模块化编程和编译 C 程序

c++ - 在 C 或 C++ 中实现 Prolog

c++ - Matlab 和 c++ 矩阵乘积 : same input, 不同的输出

c++ - 丢弃理论上使用但技术上未使用的功能

我可以在编译 C 代码期间看到定义的宏吗?

c - 这样写 ptr = &i; 是否合法?在C语言中?

C++: double 、精度、虚拟机和 GCC

c++ - 为什么允许此模板代码违反 C++ 的私有(private)访问说明符?

c++ - 预期 '(' 跟随 'WINGDIAPI'

c++ - 在整个范围内均匀生成随机数