编译容易,但存在运行时错误
Compiles easily but has run-time errors
下面的程序很容易编译,但不执行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 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;
这些是八进制文字:01001
、01002
,而不是十进制。在编译过程中,它们将被解释为十进制中的513
和514
。
当您读取以0
和cin >> product;
开头的数字时,除非您添加std::oct
修饰符,否则它会将输入数字视为十进制。
也许您应该将std::string
用于这些标识符。
尝试将int product
更改为string product
,然后使用字符串比较。您的"0"前缀将导致数字文字被解释为八进制(https://en.wikipedia.org/wiki/Octal)
这很有趣。
如果一个数字以零0
开头,则该数字应为八进制表示。
例如,01001
(八进制)是513
(十进制)。
相关文章:
- 删除指向指针的指针是运行时错误吗
- c++中的指针和运行时错误
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- 对单向链表进行排序时出现运行时错误
- 为什么此代码存在运行时错误?
- 你能解释一下什么运行时错误是如何解决它的吗?
- 为什么会出现 gettnig 运行时错误:加载类型为"_Bit_type"(stl_bvector.h) 的空指针?
- 为什么程序在 c++ 中迭代 emtpy 向量时会抛出运行时错误
- 运行时错误:引用绑定到类型为"int"的空指针
- 为什么当 vector 为空时会显示运行时错误?
- C++运行时错误与快速排序算法抛出堆栈转储错误
- 运行时错误:矢量下标超出范围:正在检查空集
- 分配给gslice_array会导致运行时错误
- cout 新创建的对象引发运行时错误
- 运行时错误 -f1.exe 不存在或不是可执行文件
- 以下代码中存在运行时-sigsegv错误
- 代码中存在无法预测的SIGSEGV(运行时错误)
- 编译容易,但存在运行时错误
- 此代码中存在运行时错误