?
Solved

Do while loop

Posted on 2005-05-04
6
Medium Priority
?
237 Views
Last Modified: 2010-04-01
Trying to code a do while loop not even sure if I am on the right track.  Receiving errors that Answer is undeclared identifier

#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

int main()
{
     //****PROGRAM TITLE*****
     cout << "\t\t\t*************"<<endl;
       cout << "\t\t\tSTOCK PROGRAM      "<<endl;
       cout << "\t\t\t*************\n\n"<<endl;

do{

     //****DEFINE VARIABLES****
     int        Shares = 0; //# OF STOCK SHARES OWNED
     double     MarketPrice = 0.00;//PRICE PER SHARE OF STOCK
     double     CommissionFee = 0.02;//COMMISSION FEE PER STOCK
     double     CommissionMinimumFee = 32.95; //MINIMUM COMMISSION FEE
       double     MarketValue; //TOTAL VALUE
       double     CommissionPaid; //COST OF COMMISSION
       char            Answer;
      
      
       //****SET 2 DECIMAL PLACES*****
       cout << setprecision(2);
       cout.setf(ios::fixed);
       cout.setf(ios::right);
             
     //****USER INPUT FOR SHARES*****
       while (Shares <= 0){
             cout << endl;
             cout<< "Please enter number of shares: ";
             cin >> Shares;
     
       //****ERROR CHECK*****
             if (Shares <= 0)
                   cout << endl << "You entered invalid information, Please try again!" <<endl;
       }

       //****USER INPUT FOR MARKET PRICE*****
       while (MarketPrice <= 0.00){
             cout << endl;
             cout<< "Please enter Market Price Per Share: $";
             cin >> MarketPrice;
     
       //****ERROR CHECK*****
             if (MarketPrice <= 0)
                   cout << endl << "You entered invalid information, Please try again!" <<endl;
       }
      
     //****CALCULATION*****
     MarketValue      = MarketPrice * Shares;//CALCULATION TO DETERMINE VALUE OF STOCKS
     CommissionPaid      = CommissionFee * Shares;//CALCULATION TO DETERMINE COMMISSION PER STOCK

       //****CONDITIONAL STATEMENT*****
     if (CommissionPaid < CommissionMinimumFee) {
     
          cout << "\nCommission Fee is:\t$" <<CommissionMinimumFee << endl;
     }
     else if (CommissionPaid > CommissionMinimumFee) {
         
          cout <<"\nCommission Paid:\t$" <<CommissionPaid<< endl;      
     }

     //****DISPLAY*****
     cout << "\n\nShares:\t\t\t " << Shares << endl;
     cout << "Market Price Per Share:\t $" << MarketPrice << endl;
     cout<< "\nMarket Value:\t\t $" << MarketValue << endl;

       cout << "\n\nDo you want to continue (Y/N)?\n";
       cout<< "You must type a 'Y' or an 'N'.\n";
     cin >> Answer;
}
while ((Answer == 'Y') || (Answer == 'y'));



      
      
        
cin.ignore();
cin.ignore();
return 0;
//*****END PROGRAM*****
}
0
Comment
Question by:mmorse0971
  • 3
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 13932282
You need to declare 'Answer' outside the loop, e.g.

#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

int main()
{
    //****PROGRAM TITLE*****
    cout << "\t\t\t*************"<<endl;
     cout << "\t\t\tSTOCK PROGRAM     "<<endl;
     cout << "\t\t\t*************\n\n"<<endl;

    char          Answer; // declaration outsie the loop


do{

    //****DEFINE VARIABLES****
    int        Shares = 0; //# OF STOCK SHARES OWNED
    double     MarketPrice = 0.00;//PRICE PER SHARE OF STOCK
    double     CommissionFee = 0.02;//COMMISSION FEE PER STOCK
    double     CommissionMinimumFee = 32.95; //MINIMUM COMMISSION FEE
      double     MarketValue; //TOTAL VALUE
      double     CommissionPaid; //COST OF COMMISSION
 
     
   
      //****SET 2 DECIMAL PLACES*****
     cout << setprecision(2);
     cout.setf(ios::fixed);
     cout.setf(ios::right);
         
     //****USER INPUT FOR SHARES*****
      while (Shares <= 0){
          cout << endl;
          cout<< "Please enter number of shares: ";
          cin >> Shares;
   
      //****ERROR CHECK*****
          if (Shares <= 0)
               cout << endl << "You entered invalid information, Please try again!" <<endl;
     }

     //****USER INPUT FOR MARKET PRICE*****
      while (MarketPrice <= 0.00){
          cout << endl;
          cout<< "Please enter Market Price Per Share: $";
          cin >> MarketPrice;
   
      //****ERROR CHECK*****
          if (MarketPrice <= 0)
               cout << endl << "You entered invalid information, Please try again!" <<endl;
     }
     
    //****CALCULATION*****
    MarketValue     = MarketPrice * Shares;//CALCULATION TO DETERMINE VALUE OF STOCKS
    CommissionPaid     = CommissionFee * Shares;//CALCULATION TO DETERMINE COMMISSION PER STOCK

     //****CONDITIONAL STATEMENT*****
    if (CommissionPaid < CommissionMinimumFee) {
   
          cout << "\nCommission Fee is:\t$" <<CommissionMinimumFee << endl;
    }
    else if (CommissionPaid > CommissionMinimumFee) {
         
         cout <<"\nCommission Paid:\t$" <<CommissionPaid<< endl;      
    }

    //****DISPLAY*****
    cout << "\n\nShares:\t\t\t " << Shares << endl;
    cout << "Market Price Per Share:\t $" << MarketPrice << endl;
    cout<< "\nMarket Value:\t\t $" << MarketValue << endl;

     cout << "\n\nDo you want to continue (Y/N)?\n";
     cout<< "You must type a 'Y' or an 'N'.\n";
    cin >> Answer;
}
while ((Answer == 'Y') || (Answer == 'y'));



   
     
       
cin.ignore();
cin.ignore();
return 0;
//*****END PROGRAM*****
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 13932291
BTW, I see that you're not using input functions to validate the data entered as described in http:Q_21411754.html
0
 

Expert Comment

by:yuvaprakash
ID: 13934385
hi mmorse,

Do not declare variables inside a loop..! it wud be reinited everytime the loop is running..!
Wht can i say is..declaring variables inside a loop..is not a gud programming..
just declare all the variables outside the loop..and as soon as you retrive the result just delete the created variables...!


0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 13965956
>>>> Receiving errors that Answer is undeclared identifier

In C/C++ variables are valid within the 'scope' - determined by curly brackets -they are defined in.

    do
    {
    }
    while (Answer == 'Y');

That's why Answer has to be defined prior to the do-while statement.

Note, in a for statement you could define a loop variable that is valid inside a loop.

    for (int i = 0; i < 100; ++i)
    {
           int j = i;   // i is valid
    }

However, on some platforms (compilers) 'i' is valid outside the loop as well (e. g. Visual C++/Windows) while on other platforms (e. g. GNU/UNIX), the loop variable isn't valid:

    for (int i = 0; i < 100; ++i)
    {
           int j = i;   // i is valid
    }

    if (i >= 100)   // error: variable 'i' isn't defined
         ....


>>>> Do not declare variables inside a loop.

That statement is not generally valid. All temporary variables which contents must not keep longer than one run (of the loop), of course could be defined inside a loop. You would make clear that the variables are used temporarily only by assigning an initial value as you had done for 'Shares', 'MarketPrice', 'CommissionFee' and 'CommissionMinimumFee'. 'MarketValue' and 'CommissionPaid' are temporary as well and you should initialize them.

>>>> cout <<"\nCommission Paid:\t$" <<CommissionPaid<< endl;

'\n' has the same meaning than 'endl', but you shouldn't mix them:

       cout << endl << "Commission Paid:\t$" <<CommissionPaid<< endl;

Instead of '\t' you may consider to use setw(...) what could make the output more precise cause '\t' is dependent on the current position.

     cout << endl << endl << "Shares:" << setw(20) << " " << Shares << endl;

Regards, Alex
0
 
LVL 86

Expert Comment

by:jkr
ID: 13969087
>>In C/C++ variables are valid within the 'scope'

Um, Alex, that was already cleared up 5 days earlier...
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 13969538
>>>> Um, Alex, that was already cleared up 5 days earlier...

Actually, there was no comment that used the expressions 'scope' or 'curly brackets'. The asker seems to be a beginner and we got  no feedback to the (valid) solutions, what makes it doubtful whether the issue was cleared for him/her. The explanation where to define variables and where they are valid is an information that wasn't given before.

Furthermore, if I decide to give an answer to an older question, it is difficult or impossible to *not* repeating anything already told. I mostly try to add something like "as jkr already told you", when refering to a previous comment. But I am not perfect and didn't find a comment in that thread that needed to be refered to.

Regards, Alex


 
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question