user1170392
user1170392

Reputation:

First C++ Program Getting Errors

Ok so for an assignment we have to make our first program. Mine is to make a program that calculates the ticket revenue for each seating section at a theater. The problem is that I keep getting this undeclared identifier or identifier undefined error along with like 23 other errors. The program I am using is Visual Studio 2010 Premium.

Here is my code.

// 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 

Here are my errors:

Error 6 error C2065: 'balconyTotal' : undeclared identifier c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 28 1 Chapter4HW Error 9 error C2065: 'balconyTotal' : undeclared identifier c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 29 1 Chapter4HW Error 13 error C2065: 'balconyTotal' : undeclared identifier c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 34 1 Chapter4HW Error 5 error C2065: 'mainTotal' : undeclared identifier c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 27 1 Chapter4HW Error 8 error C2065: 'mainTotal' : undeclared identifier c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 29 1 Chapter4HW Error 12 error C2065: 'mainTotal' : undeclared identifier c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 33 1 Chapter4HW Error 4 error C2065: 'orchestraTotal' : undeclared identifier c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 26 1 Chapter4HW Error 10 error C2065: 'orchestraTotal' : undeclared identifier c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 29 1 Chapter4HW Error 11 error C2065: 'orchestraTotal' : undeclared identifier c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 32 1 Chapter4HW Error 7 error C2065: 'overallTotal' : undeclared identifier c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 29 1 Chapter4HW Error 14 error C2065: 'overallTotal' : undeclared identifier c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 35 1 Chapter4HW Error 1 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 13 1 Chapter4HW Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 14 1 Chapter4HW Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 15 1 Chapter4HW 15 IntelliSense: explicit type is missing ('int' assumed) c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 13 7 Chapter4HW 16 IntelliSense: explicit type is missing ('int' assumed) c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 14 7 Chapter4HW 17 IntelliSense: explicit type is missing ('int' assumed) c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 15 7 Chapter4HW 20 IntelliSense: identifier "balconyTotal" is undefined c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 28 1 Chapter4HW 19 IntelliSense: identifier "mainTotal" is undefined c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 27 1 Chapter4HW 18 IntelliSense: identifier "orchestraTotal" is undefined c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 26 1 Chapter4HW 21 IntelliSense: identifier "overallTotal" is undefined c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 29 1 Chapter4HW

Gracias to everyone that answered. I seriously stared at this forever and I couldnt figure it out. It's like learning a whole new language and it's a bit confusing!

Upvotes: 0

Views: 2506

Answers (5)

Skyrel
Skyrel

Reputation: 226

you have not declared the 4 variables:

mainTotal, bacolnyTotal, orchestraTotal, overallTotal

Upvotes: 0

dreta
dreta

Reputation: 1036

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

Turn these into definitions and you should be fine. You don't declare or define any of these variables before using them, that's why the compiler is printing errors.

Upvotes: 0

johnsyweb
johnsyweb

Reputation: 141790

Understanding compiler errors is something you'll soon learn, as you practise!

const isn't a type, it's a modifier. orchestraPrice should be declared:

const int orchestraPrice = 25;

You have three similar lines with the same problem.

And you haven't declared orchestraTotal before you use it. Try this instead:

const int orchestraTotal = orchestraNum * orchestraPrice;

Again, you have three similar lines with the same problem.

I recommend reading a book or two on C++. See The Definitive C++ Book Guide and List .

Good luck!

Upvotes: 2

Gunther Piez
Gunther Piez

Reputation: 30429

You need to declare variables before you use or while you initialize them.

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

Upvotes: 0

StevieG
StevieG

Reputation: 8709

You need to declare your constants and total variables with types:

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;

Upvotes: 3

Related Questions