pass; //通过验证循环 while(通过!=“y”和通过!=“n”) { std::cout>pass; }//循环结束 }//标签价格循环结束 //投入折扣 而(!(折扣>=0.0&&折扣; //验证输入 如果(!(折扣>=0.0&&折扣,c++,loops,crash,C++,Loops,Crash" /> pass; //通过验证循环 while(通过!=“y”和通过!=“n”) { std::cout>pass; }//循环结束 }//标签价格循环结束 //投入折扣 而(!(折扣>=0.0&&折扣; //验证输入 如果(!(折扣>=0.0&&折扣,c++,loops,crash,C++,Loops,Crash" />

程序编译后崩溃 我正在为我的C++类编写程序,似乎无法找出我的代码有什么问题。代码可以编译,但有什么原因导致程序在第16行之后崩溃,我就是搞不懂 #include <iostream> // Declare Global variables and Prototyping int const TAX = .07; float inputStickerPrice(); int main() { float totalStickerPrice = 0.0, discount = 0.0, totalPrice = 0.0; char* pass = ""; // Calculate the total sticker price while (pass != "n") { totalStickerPrice += inputStickerPrice(); std::cout << "Would you like to enter another item? [y/n] "; std::cin >> pass; // Pass validation Loop while (pass != "y" && pass != "n") { std::cout << "INVALID INPUT. Please enter y for yes or n for no. "; std::cin >> pass; } // End of Pass Loop } // End of Sticker Price Loop // Input Discount while (!(discount >= 0.0 && discount <= 1)) { std::cout << "Please enter the discount: "; std::cin >> discount; // Validate input if (!(discount >= 0.0 && discount <= 1)) { std::cout << "INVALID INPUT. Discount must be between 0.0 and 1.0. "; } // End of validation } // End of Discount Loop totalPrice = totalStickerPrice * discount; // Apply Discount to total Sticker Price std::cout << "The Subtotal is: " << totalPrice << std::endl; totalPrice *= (1+TAX); // Apply Tax to Subtotal std::cout << "The Cost after Tax is: " << totalPrice << std::endl; std::cin.get(); return 0; } //********************** // Input sub program * //********************** float inputStickerPrice() { using namespace std; float sticker = 0.0; cout << "Please input the sticker price of the item: "; cin >> sticker; // Validation Loop while(!(sticker >= 0.0 && sticker <= 9999.99)) { cout << "INVALID INPUT. Please input a value between 0 and 9999.99: "; cin >> sticker; } // End of Validation Loop return sticker; } // End of Input Function #包括 //声明全局变量和原型 国际建筑税=.07; 浮动输入价格(); int main() { 浮动总价=0.0,折扣=0.0,总价=0.0; char*pass=“”; //计算总贴纸价格 while(通过!=“n”) { TotalStickPrice+=InputStickPrice(); std::cout>pass; //通过验证循环 while(通过!=“y”和通过!=“n”) { std::cout>pass; }//循环结束 }//标签价格循环结束 //投入折扣 而(!(折扣>=0.0&&折扣; //验证输入 如果(!(折扣>=0.0&&折扣

程序编译后崩溃 我正在为我的C++类编写程序,似乎无法找出我的代码有什么问题。代码可以编译,但有什么原因导致程序在第16行之后崩溃,我就是搞不懂 #include <iostream> // Declare Global variables and Prototyping int const TAX = .07; float inputStickerPrice(); int main() { float totalStickerPrice = 0.0, discount = 0.0, totalPrice = 0.0; char* pass = ""; // Calculate the total sticker price while (pass != "n") { totalStickerPrice += inputStickerPrice(); std::cout << "Would you like to enter another item? [y/n] "; std::cin >> pass; // Pass validation Loop while (pass != "y" && pass != "n") { std::cout << "INVALID INPUT. Please enter y for yes or n for no. "; std::cin >> pass; } // End of Pass Loop } // End of Sticker Price Loop // Input Discount while (!(discount >= 0.0 && discount <= 1)) { std::cout << "Please enter the discount: "; std::cin >> discount; // Validate input if (!(discount >= 0.0 && discount <= 1)) { std::cout << "INVALID INPUT. Discount must be between 0.0 and 1.0. "; } // End of validation } // End of Discount Loop totalPrice = totalStickerPrice * discount; // Apply Discount to total Sticker Price std::cout << "The Subtotal is: " << totalPrice << std::endl; totalPrice *= (1+TAX); // Apply Tax to Subtotal std::cout << "The Cost after Tax is: " << totalPrice << std::endl; std::cin.get(); return 0; } //********************** // Input sub program * //********************** float inputStickerPrice() { using namespace std; float sticker = 0.0; cout << "Please input the sticker price of the item: "; cin >> sticker; // Validation Loop while(!(sticker >= 0.0 && sticker <= 9999.99)) { cout << "INVALID INPUT. Please input a value between 0 and 9999.99: "; cin >> sticker; } // End of Validation Loop return sticker; } // End of Input Function #包括 //声明全局变量和原型 国际建筑税=.07; 浮动输入价格(); int main() { 浮动总价=0.0,折扣=0.0,总价=0.0; char*pass=“”; //计算总贴纸价格 while(通过!=“n”) { TotalStickPrice+=InputStickPrice(); std::cout>pass; //通过验证循环 while(通过!=“y”和通过!=“n”) { std::cout>pass; }//循环结束 }//标签价格循环结束 //投入折扣 而(!(折扣>=0.0&&折扣; //验证输入 如果(!(折扣>=0.0&&折扣,c++,loops,crash,C++,Loops,Crash,pass是一个指针,因此如果要输入其值,应使用*pass或pass[0] 此外,请看@Borgleader的评论 编辑: 将char-pass*;更改为std::string-pass;-应该可以解决这个问题 char* pass = ""; pass是一个指针,因此如果要输入其值,应使用*pass或pass[0] 此外,请看@Borgleader的评论 编辑: 将char-pass*;更改为std::string-pass;-应该可以解决这个问题 char* pass = ""; pass

pass
是一个指针,因此如果要输入其值,应使用
*pass
pass[0]

此外,请看@Borgleader的评论

编辑:

char-pass*;
更改为
std::string-pass;
-应该可以解决这个问题

char* pass = "";
pass
是一个指针,因此如果要输入其值,应使用
*pass
pass[0]

此外,请看@Borgleader的评论

编辑:

char-pass*;
更改为
std::string-pass;
-应该可以解决这个问题

char* pass = "";
pass
是一个指针,因此如果要输入其值,应使用
*pass
pass[0]

此外,请看@Borgleader的评论

编辑:

char-pass*;
更改为
std::string-pass;
-应该可以解决这个问题

char* pass = "";
pass
是一个指针,因此如果要输入其值,应使用
*pass
pass[0]

此外,请看@Borgleader的评论

编辑:

char-pass*;
更改为
std::string-pass;
-应该可以解决这个问题

char* pass = "";
在这里,您声明了一个指向字符串文字的指针,该字符串文字是一个字符数组,它占用了一个不允许修改的存储区域。最近遵循C++11标准的编译器应该会对此行产生错误,因为字符串文字不再隐式转换为
char*
,而是转换为
const char*取而代之

当您在此行中修改此内存时,
std::cin>>pass;
您的程序有未定义的行为,所有赌注都已取消。崩溃只是可能的结果之一

接下来,您不能像这样比较字符串:

pass != "y"
pass
是一个指针,而
的“y”将衰减为一。这里不是比较字符串的内容,而是比较永远不会相同的指针值

在您准备好处理指针之前,先忘掉指针,改用类。然后比较字符串就像比较str1==str2一样简单

在这里,您声明了一个指向字符串文字的指针,该字符串文字是一个字符数组,它占用了一个不允许修改的存储区域。最近遵循C++11标准的编译器应该会对此行产生错误,因为字符串文字不再隐式转换为
char*
,而是转换为
const char*取而代之

当您在此行中修改此内存时,
std::cin>>pass;
您的程序有未定义的行为,所有赌注都已取消。崩溃只是可能的结果之一

接下来,您不能像这样比较字符串:

pass != "y"
pass
是一个指针,而
的“y”将衰减为一。这里不是比较字符串的内容,而是比较永远不会相同的指针值

在您准备好处理指针之前,先忘掉指针,改用类。然后比较字符串就像比较str1==str2一样简单

在这里,您声明了一个指向字符串文字的指针,该字符串文字是一个字符数组,它占用了一个不允许修改的存储区域。最近遵循C++11标准的编译器应该会对此行产生错误,因为字符串文字不再隐式转换为
char*
,而是转换为
const char*取而代之

当您在此行中修改此内存时,
std::cin>>pass;
您的程序有未定义的行为,所有赌注都已取消。崩溃只是可能的结果之一

接下来,您不能像这样比较字符串:

pass != "y"
pass
是一个指针,而
的“y”将衰减为一。这里不是比较字符串的内容,而是比较永远不会相同的指针值

在您准备好处理指针之前,先忘掉指针,改用类。然后比较字符串就像比较str1==str2一样简单

在这里,您声明了一个指向字符串文字的指针,该字符串文字是一个字符数组,它占用了一个不允许修改的存储区域。最近遵循C++11标准的编译器应该会对此行产生错误,因为字符串文字不再隐式转换为
char*
,而是转换为
const char*取而代之

当您在此行中修改此内存时,
std::cin>>pass;
您的程序有未定义的行为,所有赌注都已取消。崩溃只是可能的结果之一

接下来,您不能像这样比较字符串:

pass != "y"
pass
是一个指针,而
的“y”将衰减为一。这里不是比较字符串的内容,而是比较永远不会相同的指针值


在您准备好处理指针之前,请先忘掉指针,改用类。然后比较字符串就像
str1==str2

您尝试随机读入字符串一样简单。您尝试随机读入字符串。您尝试随机读入字符串。您尝试随机读入字符串。您尝试随机读入字符串。这是不可能的使用程序崩溃。事实上,这将永远是t