c++ - 编译容易但有运行时错误

标签 c++ literals

下面的程序很容易编译,但不会执行 ifelse if 语句中给出的内容。请告诉我哪里出错了。我是 C++ 的新手。

    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    #include <time.h>

using namespace std;
int random(int min, int max) //range : [min, max)
{
   static bool first = true;
   if ( first ) 
   {  

      srand(time(NULL)); //seeding for the first time only!
      first = false;
   }
   return min + rand() % (max - min);
}
class size{
    public:
    int medium;
    int large;
    int small;
};
int main(){
    int min=1000,max=9999;
    int product,ran=random(min,max);
    ofstream myfile ("orders.html");
size fresh;
cout<<"******************************************************************************\n";
cout<<"*                        Fresh fruit juicees                             *\n";
cout<<"******************************************************************************\n";
cout<<"01001. Large mango juice\n";
cout<<"01002. Medium mango juice\n";
cout<<"01003. Small mango juice\n"<<"Enter the product key given before the product name\n";
cin>>product;

if (product==01001){
    cout<<"Thanks for choosing Large one ";
    if (myfile.is_open()){
    myfile<<"<html><head><title>Oreders</title>\n"<<
    "</head><body>Order number "<<ran<<"is pending";
    myfile.close();
}}
    else if(product==01002)
    {
      cout<<"Thanks for choosing medium one";
      myfile<<"<html><head><title>Oreders</title>\n"<<
    "</head><body>Order number "<<ran<<"is pending";
    myfile.close();
}


}
//.....program continues so on;

最佳答案

这些是八进制文字:0100101002,不是十进制。在编译期间,它们将被解释为十进制基数系统中的 513514

当您使用 cin >> product; 读取以 0 开头的数字时,它会将输入数字视为十进制,除非您添加 std::oct 修饰符。

也许您应该为这些标识符使用 std::string

关于c++ - 编译容易但有运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34064936/

相关文章:

c++ - 如何在 C++ 中一次性生成不同的随机数?

c++ 定义静态类函数

rust - 带后缀的数字文字(例如0u8)在Rust中意味着什么?

java - 在 Java 中说某个东西是 "literal variable"是否有意义?

javascript - 使用 Javascript 设置 ASP 文字文本

c - 为什么-32768 在下面的平台上有 signed long 类型?

c++ - ZZ坐标不匹配

C++ 单独的实现和头文件

c++ - 使用导入的 makefile 将 Qt Creator .config 添加到 .h

c++ - 为什么常量有类型修饰符?