第一个C++程序出现错误
First C++ Program Getting Errors
好的,所以对于一项任务,我们必须制作我们的第一个程序。我的任务是制作一个程序,计算剧院每个座位区的门票收入。问题是,我一直得到这个未声明的标识符或标识符未定义的错误,以及其他23个错误。我正在使用的程序是Visual Studio 2010高级版。
这是我的密码。
// Chp4HWprgm.cpp
// Created by Bryce Easley on 2/6/2012
#include <iostream>
using namespace std;
int main(){
//declare variables
int orchestraNum = 0;
int mainNum = 0;
int balconyNum =0;
const orchestraPrice = 25;
const mainPrice = 30;
const balconyPrice = 15;
//enter input of sales
cout << "Number of Orchestra tickets sold?";
cin >> orchestraNum;
cout << "Number of Main Floor tickets sold?";
cin >> mainNum;
cout << "Number of Balcony tickets sold?";
cin >> balconyNum;
//calculate revenue for each and total revenue
orchestraTotal = orchestraNum * orchestraPrice;
mainTotal = mainNum * mainPrice;
balconyTotal = balconyNum * balconyPrice;
overallTotal = mainTotal + balconyTotal + orchestraTotal;
//display figures
cout <<"Orchestra Revenue: $" << orchestraTotal << endl;
cout <<"Main Floor Revenue: $" << mainTotal << endl;
cout <<"Balcony Revenue: $" << balconyTotal << endl;
cout <<"Overall Revenue: $" << overallTotal << endl;
system("pause");
return 0;}
//end of main function
以下是我的错误:
错误6错误C2065:"balconyTotal":未声明标识符c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4 hw\chp4hw.cpp 28 1 chapter4hw错误9错误C2065:"balconyTotal":未声明标识符c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4 hw\chp4hw.cpp 29 1 chapter4hw错误13错误C2065:"balconyTotal":未声明标识符c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4 hw\chp4hw.cpp 34 1 chapter4hw错误5错误C2065:"mainTotal":未声明标识符c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4 hw\chp4hw.cpp 27 1 chapter4hw错误8错误C2065:"mainTotal":未声明标识符c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4 hw\chp4hw.cpp 29 1 chapter4hw错误12错误C2065:"mainTotal":未声明标识符c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4 hw\chp4hw.cpp 33 1 chapter4hw错误4错误C2065:"orchestraTotal":未声明标识符c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4 hw\chp4hw.cpp 26 1 chapter4hw错误10错误C2065:"orchestraTotal":未声明标识符c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4 hw\chp4hw.cpp 29 1 chapter4hw错误11错误C2065:"orchestraTotal":未声明标识符c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4 hw\chp4hw.cpp 32 1 chapter4hw错误7错误C2065:"overallTotal":未声明标识符c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4 hw\chp4hw.cpp 29 1 chapter4hw错误14错误C2065:"overallTotal":未声明标识符c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4 hw\chp4hw.cpp 35 1 chapter4hw错误1错误C4430:缺少类型说明符-假定为int。注:C++不支持默认int c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4 hw\chp4hw.cpp 13 1 chapter4hw错误2错误C4430:缺少类型说明符-假定为int。注:C++不支持默认int c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4 hw\chp4hw.cpp 14 1 chapter4hw错误3错误C4430:缺少类型说明符-假定为int。注:C++不支持默认int c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4 hw\chp4hw.cpp 15 1 chapter4hw15 IntelliSense:缺少显式类型("t"假定)c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4 hw\chp4 hw.cpp 13 7 chapter4hw16 IntelliSense:缺少显式类型("t"假定)c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4 hw\chp4 hw.cpp 14 7 chapter4hw17 IntelliSense:缺少显式类型("t"假定)c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4 hw\chp4 hw.cpp 15 7 chapter4hw20 IntelliSense:标识符"balconyTotal"为未定义c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4 hw\chp4hw.cpp 28 1 chapter4hw19 IntelliSense:标识符"mainTotal"为未定义c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4 hw\chp4hw.cpp 27 1 chapter4hw18 IntelliSense:标识符"orchestraTotal"为未定义c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4 hw\chp4hw.cpp 26 1 chapter4hw21 IntelliSense:标识符"overallTotal"为未定义c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4 hw\chp4hw.cpp 29 1 chapter4hw
感谢所有回答的人。我认真地盯着这个看了很久,我想不通。这就像学习一门全新的语言,有点令人困惑!
您需要使用以下类型声明常量和总变量:
const int orchestraPrice = 25;
const int mainPrice = 30;
const int balconyPrice = 15;
...
//calculate revenue for each and total revenue
int orchestraTotal = orchestraNum * orchestraPrice;
int mainTotal = mainNum * mainPrice;
int balconyTotal = balconyNum * balconyPrice;
int overallTotal = mainTotal + balconyTotal + orchestraTotal;
通过练习,您很快就会了解编译器错误!
const
不是一个类型,它是一个修饰符。应声明orchestraPrice
:
const int orchestraPrice = 25;
你有三条相似的线路有同样的问题。
在使用orchestraTotal
之前,您还没有声明它。请尝试以下操作:
const int orchestraTotal = orchestraNum * orchestraPrice;
同样,您有三条类似的线路存在相同的问题。
我建议你读一两本关于C++的书。请参阅最终C++图书指南和列表。
祝你好运!
在使用变量之前或初始化变量时,需要声明变量。
int orchestraTotal = orchestraNum * orchestraPrice;
int mainTotal = mainNum * mainPrice;
int balconyTotal = balconyNum * balconyPrice;
int overallTotal = mainTotal + balconyTotal + orchestraTotal;
orchestraTotal = orchestraNum * orchestraPrice;
mainTotal = mainNum * mainPrice;
balconyTotal = balconyNum * balconyPrice;
overallTotal = mainTotal + balconyTotal + orchestraTotal;
把这些变成定义,你就应该没事了。在使用这些变量之前,您没有声明或定义它们,这就是编译器打印错误的原因。
您尚未声明以下4个变量:
mainTotal, bacolnyTotal, orchestraTotal, overallTotal
- 程序崩溃并显示"std::out_of_range"错误
- xmake总是报告:错误:无法获取cxx的程序,为什么
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- 分段错误当我试图运行程序时出错
- 显示错误输出的简单数组排序程序
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- 为什么程序在 c++ 中迭代 emtpy 向量时会抛出运行时错误
- 在气泡排序程序中未声明错误功能
- 我在c ++矩阵计算器程序中找不到错误
- 叮当格式化程序多行格式配置错误
- 全局向量导致 C++ 程序结束时出现段错误
- 运行时间错误:程序跳过提示,以获取第二名和第三个名称
- Glibc 2.7 配置错误程序太旧
- 运行时错误:程序已停止响应
- OpenCV for Android:在 PATH 中找不到错误程序"sh"
- C++中的 scanf 错误 - 程序没有响应
- c++语法错误:程序以非零状态退出
- 运行时错误C++程序:检测到glibc,munmap_chunk(),指针无效
- libxml2错误程序c++