下面的程序很容易编译,但不会执行 if
和 else 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;
最佳答案
这些是八进制文字:01001
、01002
,不是十进制。在编译期间,它们将被解释为十进制基数系统中的 513
和 514
。
当您使用 cin >> product;
读取以 0
开头的数字时,它会将输入数字视为十进制,除非您添加 std::oct
修饰符。
也许您应该为这些标识符使用 std::string
。
关于c++ - 编译容易但有运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34064936/