NEWB!! Error "Undeclared Identifier"

Write your question here.

[code]#include <string>
using namespace std;

int main()

{

string name;

int minsbadminton, minsrunning, minswalking, minstraining, weight,totalhrs, totalmins;
totalmins = minsbadminton + minsrunning + minswalking + minstraining;

double TTLCALBADMINTON = BADMINTON * weight, TTLCALRUNNING = RUNNING * weight, TTLCALWALKING = WALKING * weight, TTLCALTRAINING = TRAINING * weight;
double TTLCALSBURNED = TTLCALBADMINTON + TTLCALRUNNING + TTLCALWALKING + TTLCALTRAINING;
const double BADMINTON = .044, RUNNING = .087, WALKING = .036, TRAINING = .042;


cout << "Welcome to Tina's workout calculator." << endl;
cout << endl;
cout << "Please enter your name: " << endl;
getline(cin, name);
cout << " Please enter your weight: " << endl;
cin >> weight;
cout << "Please enter the minutes spent playing badminton; " << endl;
cin >> minsbadminton;
cout << "Please enter the minutes spent running: " << endl;
cin >> minsrunning;
cout << "Please enter the minutes spent walking: " << endl;
cin >> minswalking;
cout << "Please enter the minutes spent weight training: " << endl;
cin >> minstraining;

cout << "You have spent " << totalmins << " minutes working out" << endl;
cout << "You burned " << TTLCALBADMINTON << " calories playing badminton. " << endl;
cout << "You burned " << TTLCALRUNNING << " calories running. " << endl;
cout << "You burned " << TTLCALWALKING << " calories walking, " << endl;
cout << "You burned " << TTLCALTRAINING << " calories weight training. " << endl;
cout << "You burned " << TTLCALSBURNED << " calories in all. " << endl;


system("PAUSE");
return 0;

}




What am I doing wrong??!!! I'm getting BADMINTON, RUNNING, WALKING and TRAINING are undeclared identifiers. This assignment has caused me so much grief for two weeks. I'm extremely confused!!!!

Here is the assignment:



CSCI 1010Programming Assignment 4Expressions and InteractivityLearning OutcomesFormat output so that
oating point numbers have xed precision.Format output so that text aligns properly.Use the string data type andgetlineto read text with spaces in it.Required ReadingGaddis - Chapter 3InstructionsIn your last assignment you wrote a calorie calculator. For this assignment you will rewrite it so it displaysthe results using the output formatting we discussed in class.Here are the changes you must make:Before prompting for the user's weight, it should also prompt them for their name. It should be ableto handle input of a single line containing spaces.The output should be formatted in columns consisting of the activities participated in, the hours andminutes spent on each activity, and the number of calories burned for each activity.In addition the total times and calories should be formatted into columns as well.Time should be displayed inH:MMformat. The tricky part here is dealing with what happens whenthe minutes are less than 10. You can handle this by using thesetfillstream manipulator:cout << setfill('0') << minutes << setfill(' ');This sets the character used to ll whitespace to a0before displaying the minutes, then sets it back toa space afterwards.Calories should be displayed using xedprecision with exactlythreedigits after the decimal point.The user's name should also be incorporated into the output (see the next section for examples.)1
Example Input and OutputHere is an example run of the program:Please enter your name: Joe CoolPlease enter your weight: 183Please enter the minutes spent playing badminton: 73Please enter the minutes spent running: 64Please enter the minutes spent walking: 99Please enter the minutes spent lifting weights: 33Here are the results for Joe Cool:Activity Time Calories-----------------------------------Badminton 1:13 587.796Running 1:04 1018.944Walking 1:39 652.212Weights 0:33 253.638-----------------------------------Totals 4:29 2512.590Here is another example run:Welcome to Nicholas Coleman's Workout Calculator!Please enter your name: Red BaronPlease enter your weight: 135Please enter the minutes spent playing badminton: 43Please enter the minutes spent running: 62Please enter the minutes spent walking: 14Please enter the minutes spent lifting weights: 2Here are the results for Red Baron:Activity Time Calories-----------------------------------Badminton 0:43 255.420Running 1:02 728.190Walking 0:14 68.040Weights 0:02 11.340-----------------------------------Totals 2:01 1062.990



Hi,

First up you are missing the closing code tag, here is an article on them:

http://www.cplusplus.com/articles/z13hAqkS/


1
2
3
double TTLCALBADMINTON = BADMINTON * weight, TTLCALRUNNING = RUNNING * weight, TTLCALWALKING = WALKING * weight, TTLCALTRAINING = TRAINING * weight;
double TTLCALSBURNED = TTLCALBADMINTON + TTLCALRUNNING + TTLCALWALKING + TTLCALTRAINING;
const double BADMINTON = .044, RUNNING = .087, WALKING = .036, TRAINING = .042;


Things happen sequentially in a program, one can't do an illogical order.

Several things: You are doing calculations before the variables are initialised with any value, and before you ask the user for those values.

I rearranged everything here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream> // need this for cin cout etc
#include <string>

using namespace std;

int main()

{

cout << "Welcome to Tina's workout calculator.\n\n"; // prefer \n rather than endl

cout << "Please enter your name: \n";
string name; // defer declaration of variables until you  need them
getline(cin, name);

cout << " Please enter your weight: \n";
unsigned int weight;  // unsigned for a positive value
cin >> weight;
cin.ignore(100 , '\n') ;  // use this to discard the newline

cout << "Please enter the minutes spent playing badminton; \n";
unsigned int minsbadminton;
cin >> minsbadminton;
cin.ignore(100 , '\n');

cout << "Please enter the minutes spent running: \n";
unsigned int minsrunning;
cin >> minsrunning;
cin.ignore(100 , '\n');

cout << "Please enter the minutes spent walking: \n";
unsigned int minswalking;
cin >> minswalking;
cin.ignore(100 , '\n');

cout << "Please enter the minutes spent weight training: \n";
unsigned int minstraining;
cin >> minstraining;
cin.ignore(100 , '\n');

unsigned int totalmins = minsbadminton + minsrunning + minswalking + minstraining;

const double BADMINTON = .044; 
const double RUNNING = 0.087;
const double WALKING = 0.036; 
const double TRAINING = 0.042;

double TTLCALBADMINTON = BADMINTON * weight; // are these formulae correct?
double TTLCALRUNNING = RUNNING * weight ;         // do they need the time in them also?
double TTLCALWALKING = WALKING * weight ;
double TTLCALTRAINING = TRAINING * weight;
double TTLCALSBURNED = TTLCALBADMINTON + TTLCALRUNNING + TTLCALWALKING + TTLCALTRAINING;


cout << "You have spent " << totalmins << " minutes working out\n";
cout << "You burned " << TTLCALBADMINTON << " calories playing badminton. \n";
cout << "You burned " << TTLCALRUNNING << " calories running. \n";
cout << "You burned " << TTLCALWALKING << " calories walking, \n";
cout << "You burned " << TTLCALTRAINING << " calories weight training. \n";
cout << "You burned " << TTLCALSBURNED << " calories in all. \n";


system("PAUSE");
return 0;

}


I haven't tested this yet, but it is a big improvement.

Good Luck !!



Last edited on
Also:

What are the units for the weight? Pounds, I gather. Reflect that in the prompt and the variable name WeightPounds

The output should be formatted in columns consisting of the activities participated in, the hours and minutes spent on each activity, and the number of calories burned for each activity.In addition the total times and calories should be formatted into columns as well.Time should be displayed inH:MMformat.


So, something to work on there :+)

Consider using functions to do things.
Thank you all! I appreciate the help. I'm so new to this. You helped a lot!!
Topic archived. No new replies allowed.