简体   繁体   English

C ++开关访问冲突

[英]c++ switch access violation

what i'm writing is simple, well, it should be, but i'm getting this error and i don't know what else to do, my code look like this 我写的内容很简单,应该是,但是我遇到了这个错误,而且我不知道该怎么办,我的代码看起来像这样

int main()
{
    char *option;

    while(strcmp(option,"exit")!=0){

        int opt = GetSystemDefaultUILanguage();
        std::string lang;
        switch(opt)
        {
            case 3082:
                    lang = "number 3082";
                    break;
            case 1033:
                    lang = "number 1033";
                    break;
        }
        std::cout<<lang<<'\n';
        std::cin>>option;
    }

}

when i compile it there isn't errors, but when i run it, i get a this error 当我编译它时没有错误,但是当我运行它时,我得到了这个错误
Project xxxx raised exception class EAccessViolation with message 'Access violation at address zzzzz'.Process stopped. 项目xxxx引发异常类EAccessViolation,消息为``地址zzzzz处的访问冲突'',进程已停止。 Use Step or Run to continue. 使用“步骤”或“运行”继续。

EDITED: 编辑:
This is my full code, now is more simple, but still the same result. 这是我的完整代码,现在更简单,但结果仍然相同。
even if i try with an if/else statement it wont work, need some help here, thanks 即使我尝试使用if / else语句也行不通,在这里也需要一些帮助,谢谢

I can't tell you the cause of the specific run-time error you're seeing, but I call tell you what's wrong with your program: hardcoded paths to user directories . 我无法告诉您所看到的特定运行时错误的原因,但我打电话告诉您程序有什么问题:硬编码的用户目录路径 Localized names are just one of a myriad of things that can go wrong with trying to guess the paths yourself. 本地化名称只是尝试自己猜测路径可能会出错的众多事物之一。

DON'T DO THAT. 不要那样做。 Instead, read environment variables or call Shell APIs to find out where this particular user wants temporary data stored (or documents, pictures, desktop icons, etc). 相反,请读取环境变量或调用Shell API以找出该特定用户希望在哪里存储临时数据(或文档,图片,桌面图标等)。

Have a look at getenv("TEMP") and ShGetSpecialFolderPath 看看getenv("TEMP")ShGetSpecialFolderPath

Your program will always get an access violation because of the following lines: 由于以下几行,您的程序将始终会出现访问冲突:

char *option;

while(strcmp(option,"exit")!=0){

std::cin>>option;

You never initialize the pointer option , but then try to use it. 您永远不会初始化指标option ,但是尝试使用它。 Change your code to this: 将代码更改为此:

int main()
{
    std::string option;

    while(option != "exit")
    {
        int opt = GetSystemDefaultUILanguage();
        std::string lang;
        switch(opt)
        {
            case 3082:
                    lang = "number 3082";
                    break;
            case 1033:
                    lang = "number 1033";
                    break;
        }
        std::cout<<lang<<std::endl;
        std::cin>>option;
    }
}

Your problem is this line: 您的问题是这一行:

   std::cin>>option;

The variable option is declared as an uninitialized pointer to a character. 变量option被声明为指向字符的未初始化指针。 Thus in the above statement, you are reading data into an unknown location. 因此,在上面的语句中,您正在将数据读取到未知位置。

Why do you use C style strings ( char * ) and C++ std::string ? 为什么使用C样式字符串( char * C ++ std::string You should get rid of C style strings ( unless they are constant ). 您应该摆脱C风格的字符串( 除非它们是常量 )。 Try this: 尝试这个:

#include <iostream>
#include <string>

int main(void)
{
  std::string option;
  do
  {
     std::cout << "Type exit to end program." << std::endl; // endl will flush output buffer
     std::getline(cin, option);  // Input a text line into "option".
  } while (option != "exit");  // C-style string, used as a constant.
  return 0;
}

You wrote 你写了

BlockquoteProject xxxx raised exception class EAccessViolation with message 'Access violation at address zzzzz'.Process stopped. BlockquoteProject xxxx引发异常类EAccessViolation,消息为“地址zzzzz处的访问冲突”。进程已停止。 Use Step or Run to continue. 使用“步骤”或“运行”继续。

So why don't you pause your program before crash, go to the location and put a breakpoint? 那么,为什么不在崩溃前暂停程序,转到该位置并放置一个断点呢? If you still can't cope with that than upload your code to a filesharing server and give us the link ;) 如果您仍然无法解决问题,可以将代码上传到文件共享服务器并给我们链接;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM