第一个C ++程序出错

时间:2012-02-06 18:08:36

标签: c++ compiler-errors

好的,对于作业,我们必须制作第一个程序。我的目标是制作一个计算剧院每个座位区门票收入的计划。问题是我不断得到这个未声明的标识符或标识符未定义错误以及其他23个错误。我正在使用的程序是Visual Studio 2010 Premium。

这是我的代码。

// 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 \ chapter4hw \ chp4hw.cpp 28 1 Chapter4HW   错误9错误C2065:'balconyTotal':未声明   标识符c:\ users \ bryce \ desktop \ cpp6 \ chap04 \ chapter4hw \ chapter4hw \ chp4hw.cpp 29 1 Chapter4HW   错误13错误C2065:'balconyTotal':未声明   标识符c:\ users \ bryce \ desktop \ cpp6 \ chap04 \ chapter4hw \ chapter4hw \ chp4hw.cpp 34 1 Chapter4HW   错误5错误C2065:'mainTotal':未声明   标识符c:\ users \ bryce \ desktop \ cpp6 \ chap04 \ chapter4hw \ chapter4hw \ chp4hw.cpp 27 1 Chapter4HW   错误8错误C2065:'mainTotal':未声明   标识符c:\ users \ bryce \ desktop \ cpp6 \ chap04 \ chapter4hw \ chapter4hw \ chp4hw.cpp 29 1 Chapter4HW   错误12错误C2065:'mainTotal':未声明   标识符c:\ users \ bryce \ desktop \ cpp6 \ chap04 \ chapter4hw \ chapter4hw \ chp4hw.cpp 33 1 Chapter4HW   错误4错误C2065:'orchestraTotal':未声明   标识符c:\ users \ bryce \ desktop \ cpp6 \ chap04 \ chapter4hw \ chapter4hw \ chp4hw.cpp 26 1 Chapter4HW   错误10错误C2065:'orchestraTotal':未声明   标识符c:\ users \ bryce \ desktop \ cpp6 \ chap04 \ chapter4hw \ chapter4hw \ chp4hw.cpp 29 1 Chapter4HW   错误11错误C2065:'orchestraTotal':未声明   标识符c:\ users \ bryce \ desktop \ cpp6 \ chap04 \ chapter4hw \ chapter4hw \ chp4hw.cpp 32 1 Chapter4HW   错误7错误C2065:'overallTotal':未声明   标识符c:\ users \ bryce \ desktop \ cpp6 \ chap04 \ chapter4hw \ chapter4hw \ chp4hw.cpp 29 1 Chapter4HW   错误14错误C2065:'overallTotal':未声明   标识符c:\ users \ bryce \ desktop \ cpp6 \ chap04 \ chapter4hw \ chapter4hw \ chp4hw.cpp 35 1 Chapter4HW   错误1错误C4430:缺少类型说明符 - 假定为int。注意:C ++   不支持   default-int c:\ users \ bryce \ desktop \ cpp6 \ chap04 \ chapter4hw \ chapter4hw \ chp4hw.cpp 13 1 Chapter4HW   错误2错误C4430:缺少类型说明符 - 假定为int。注意:C ++   不支持   default-int c:\ users \ bryce \ desktop \ cpp6 \ chap04 \ chapter4hw \ chapter4hw \ chp4hw.cpp 14 1 Chapter4HW   错误3错误C4430:缺少类型说明符 - 假定为int。注意:C ++   不支持   default-int c:\ users \ bryce \ desktop \ cpp6 \ chap04 \ chapter4hw \ chapter4hw \ chp4hw.cpp 15 1 Chapter4HW     15 IntelliSense:缺少显式类型('int'   假设)c:\ users \ bryce \ desktop \ cpp6 \ chap04 \ chapter4hw \ chapter4hw \ chp4hw.cpp 13 7 Chapter4HW     16 IntelliSense:缺少显式类型('int'   假设)c:\ users \ bryce \ desktop \ cpp6 \ chap04 \ chapter4hw \ chapter4hw \ chp4hw.cpp 14 7 Chapter4HW     17 IntelliSense:缺少显式类型('int'   假设)c:\ users \ bryce \ desktop \ cpp6 \ chap04 \ chapter4hw \ chapter4hw \ chp4hw.cpp 15 7 Chapter4HW     20智能感知:标识符“balconyTotal”是   undefined c:\ users \ bryce \ desktop \ cpp6 \ chap04 \ chapter4hw \ chapter4hw \ chp4hw.cpp 28 1 Chapter4HW     19 IntelliSense:标识符“mainTotal”是   undefined c:\ users \ bryce \ desktop \ cpp6 \ chap04 \ chapter4hw \ chapter4hw \ chp4hw.cpp 27 1 Chapter4HW     18智能感知:标识符“orchestraTotal”是   undefined c:\ users \ bryce \ desktop \ cpp6 \ chap04 \ chapter4hw \ chapter4hw \ chp4hw.cpp 26 1 Chapter4HW     21智能感知:标识符“overallTotal”是   undefined c:\ users \ bryce \ desktop \ cpp6 \ chap04 \ chapter4hw \ chapter4hw \ chp4hw.cpp 29 1 Chapter4HW

格拉西亚斯回答了所有人。我一直认真地盯着这个,我无法弄明白。这就像学习一门全新的语言一样令人困惑!

5 个答案:

答案 0 :(得分:3)

您需要使用类型声明常量和总变量:

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;

答案 1 :(得分:2)

在练习时,您将很快了解编译器错误!

const不是类型,它是修饰符。应声明orchestraPrice

const int orchestraPrice = 25;

你有三条相似的问题。

在使用之前,您尚未声明orchestraTotal。试试这个:

const int orchestraTotal = orchestraNum * orchestraPrice;

同样,你有三条相似的问题。

我建议您在C ++上阅读一两本书。请参阅The Definitive C++ Book Guide and List

祝你好运!

答案 2 :(得分:0)

您需要在使用之前或初始化变量时声明变量。

int orchestraTotal = orchestraNum * orchestraPrice;
int mainTotal = mainNum * mainPrice;
int balconyTotal = balconyNum * balconyPrice;
int overallTotal = mainTotal + balconyTotal + orchestraTotal;

答案 3 :(得分:0)

您尚未声明4个变量:

mainTotal, bacolnyTotal, orchestraTotal, overallTotal

答案 4 :(得分:0)

orchestraTotal = orchestraNum * orchestraPrice;
mainTotal = mainNum * mainPrice;
balconyTotal = balconyNum * balconyPrice;
overallTotal = mainTotal + balconyTotal + orchestraTotal;

将这些变成定义,你应该没事。在使用它们之前,不要声明或定义任何这些变量,这就是编译器打印错误的原因。