编译容易,但存在运行时错误

Compiles easily but has run-time errors

本文关键字:存在 运行时错误 编译      更新时间:2023-10-16

下面的程序很容易编译,但不执行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 juicen";
cout<<"01002. Medium mango juicen";
cout<<"01003. Small mango juicen"<<"Enter the product key given before the product namen";
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

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

也许您应该将std::string用于这些标识符。

尝试将int product更改为string product,然后使用字符串比较。您的"0"前缀将导致数字文字被解释为八进制(https://en.wikipedia.org/wiki/Octal)

这很有趣。

如果一个数字以零0开头,则该数字应为八进制表示。

例如,01001(八进制)是513(十进制)。