第一个C++程序出现错误

First C++ Program Getting Errors

本文关键字:错误 程序 C++ 第一个      更新时间:2023-10-16

好的,所以对于一项任务,我们必须制作我们的第一个程序。我的任务是制作一个程序,计算剧院每个座位区的门票收入。问题是,我一直得到这个未声明的标识符或标识符未定义的错误,以及其他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