C++ 易于编译,但存在运行时错误
下面的程序很容易编译,但不执行C++ 易于编译,但存在运行时错误,c++,literals,C++,Literals,下面的程序很容易编译,但不执行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) { s
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;
#包括
#包括
#包括
#包括
使用名称空间std;
int random(int-min,int-max)//范围:[min,max)
{
静态布尔优先=真;
如果(第一)
{
srand(time(NULL));//仅限第一次播种!
第一个=假;
}
返回最小值+随机数()%(最大值-最小值);
}
班级人数{
公众:
int介质;
int-large;
int小;
};
int main(){
最小积分=1000,最大积分=9999;
int乘积,ran=随机(最小值,最大值);
流myfile(“orders.html”);
大小新鲜;
cout这些是八进制文字:01001
,01002
,而不是十进制。在编译过程中,它们将被解释为十进制的513
和514
当您使用cin>>product;
读取以0
开头的数字时,除非您添加std::oct
修饰符,否则它会将输入的数字视为十进制
也许您应该使用std::string
来代替这些标识符。这些是八进制文字:01001
,01002
,而不是十进制。在编译过程中,它们将被解释为十进制系统中的513
和514
当您使用cin>>product;
读取以0
开头的数字时,除非您添加std::oct
修饰符,否则它会将输入的数字视为十进制
也许您应该使用std::string
作为这些标识符。尝试将int-product
更改为string-product
,然后使用字符串比较。您的“0”前缀将导致数字文本被解释为八进制()尝试将int-product
更改为string-product
,然后使用字符串比较。您的“0”前缀将导致数字文本被解释为八进制()这很有趣
如果一个数字以零开始,则该数字应以八进制表示
例如01001
(八进制)是513
(十进制)。这很有趣
如果一个数字以零开始,则该数字应以八进制表示
例如<代码> 01001 (八进制)是代码> 513 (十进制)。
01001(八进制)与1001(十进制)不一样——请考虑基于字符串的比较。也要注意“返回MIN RAND())(max -min);}可能不返回均匀分布。返回一个值0到150,max -min是100。你会得到0.99的输入,产生0.99和输入100-150,产生0。50,所以不是你所期望的均匀分布。01001(八进制)与1001(十进制)不一样。请考虑基于字符串的比较。也知道“返回min+RAND()%(max -min);}可能不会返回均匀分布。想象一下,rand()返回的值为0到150,max-min为100。输入值为0.99,生成0.99,输入值为100-150,生成0..50,因此不是您所期望的均匀分布。。。。