C 控制台应用程序因未知原因崩溃
我有一段代码,如下所示C 控制台应用程序因未知原因崩溃,c,C,我有一段代码,如下所示 #include<cstdlib> #include<iostream> #include<stdio.h> #include<stdlib.h> using namespace std; int main() { int a; printf("Please select a choice \n1.Enter New Artist\n2.List All Artist information\n3.
#include<cstdlib>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
int a;
printf("Please select a choice \n1.Enter New Artist\n2.List All Artist information\n3. Show sorted List of Artists\n4.Add Album to existing Artist\n5.Remove Album from existing Artist\n6.Update Artist info\n7.Search for Artist");
scanf("%d,&a");
if(a==1)
{
printf("no");
}
system("PAUSE");
return EXIT_SUCCESS;
}
在运行代码时,它会显示菜单,如果我输入1,它会在几秒钟后崩溃并显示信息
document1.exe已停止工作
如何调试此问题。我使用DEVC++ 4.4.92< p>你的SCANF语句是错误的。您没有将参数指针传递给它
改变
scanf("%d,&a");
到
你的scanf声明是错误的。您没有将参数指针传递给它
改变
scanf("%d,&a");
到
对于C解决方案,我不确定 除了前面提到的 您的编译器本应警告的scanf格式错误, 这会让结果。。天知道堆栈上的什么地方… 我不确定,但我怀疑编译后的代码会将ram中的下一个位置作为地址,并在那里写入。 Borked stack==调试错误非常非常非常困难 <>因为你使用C++编译器,如果你可以使用C++,你可能会考虑使用一些STL来进行评估。 我假设您将使用CR终止输入
int main (int argv, char** argv]
{
int a;
std::string inputString;
std::cout <<"Please select a choice \n""1.Enter New Artist\n2.List All Artist information\n3. Show sorted List of Artists\n4.Add Album to existing Artist\n5.Remove Album from existing Artist\n6.Update Artist info\n7.Search for Artist";
std::getline(std::cin,inputString);
std::stringstream inputStream(inputString);
inputStream >> a; // could also have been parsed with std::stol, or strtol. - my preference due to error checking - what if your user entered 'byte me'?
if (a==1)
{
printf("no");
}
system("PAUSE");
return EXIT_SUCCESS;
}
对于C解决方案,我不确定 除了前面提到的 您的编译器本应警告的scanf格式错误, 这会让结果。。天知道堆栈上的什么地方… 我不确定,但我怀疑编译后的代码会将ram中的下一个位置作为地址,并在那里写入。 Borked stack==调试错误非常非常非常困难 <>因为你使用C++编译器,如果你可以使用C++,你可能会考虑使用一些STL来进行评估。 我假设您将使用CR终止输入
int main (int argv, char** argv]
{
int a;
std::string inputString;
std::cout <<"Please select a choice \n""1.Enter New Artist\n2.List All Artist information\n3. Show sorted List of Artists\n4.Add Album to existing Artist\n5.Remove Album from existing Artist\n6.Update Artist info\n7.Search for Artist";
std::getline(std::cin,inputString);
std::stringstream inputStream(inputString);
inputStream >> a; // could also have been parsed with std::stol, or strtol. - my preference due to error checking - what if your user entered 'byte me'?
if (a==1)
{
printf("no");
}
system("PAUSE");
return EXIT_SUCCESS;
}
谢谢你愚蠢的错误,但它让我如此焦虑,15分钟后会接受对不起!我误解了:谢谢你愚蠢的错误,但它让我如此焦虑,15分钟后会接受对不起!我误解了: