Re: Some help needed on line 30 of this code?

  • From: Dave <davidct1209@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Wed, 14 Jul 2010 17:39:58 -0700

Here are some specific comments in line:

int main()
{
//declare variables
int regNumber = 0; //counter
int total_Price = 0;
//accumulator //Adds number of registrations until user enters
negative number to stop acumulating.
//The negative number is known as the senteniel value, which is the
value which stops the loop.
cout << "Enter number of registrations.\n Press enter after each
number.\n Enter a negative number to get the total price: " << endl;
cin >> regNumber;

***Dave:  this statement isn't within the do-while loop below.  The
effect of this code is that it will only read in the value once
regardless if it's negative or positive.

do
{while (regNumber >= 0)
cout << "Enter your next entry. Remember, enter negative number to get
total price: " <<endl;

*** Dave:  this is an infinite loop because "while" is a loop in it of
itself.  Check out your C++ book on do-while, while, and for loops.  A
do-while loop has the structure that Joseph wrote.  (the while goes
after the ending brace with a semi-colon immediately after the closing
paren of the condition).

if (regNumber < 0)
regNumber = regNumber + total_Price;

***Dave:  you are saying here that if the entered number is negative,
you will add it to the total price and assign it to reg number.  This
won't make any sense with what you want to accomplish.  You will want
to assign total price to total price plus reg number (assuming that
reg number is positive).  It would be helpful to work through some
basic exercises on how to add variables here.


} //end while
***Dave:  this is where the while belongs.

cout << total_Price;
return 0;
} //end of main function

On 7/14/10, Joseph Lee <joseph.lee22590@xxxxxxxxx> wrote:
> Hi,
>
> I see - glad that you are getting the basics of while loops.
>
> From what I can see, it appears to be a scope error - where the variable in
> one part of the program is not what the program expects it to be. For
> example, suppose if I was to create a simple unit converter program and
> define two integer variables with the name of "length" and "width." Now
> suppose if I work with length in the main function and the width in the
> while loop with the loop inside the brackets. Now suppose if I'm back at the
> main function and ask the computer to print out the width. The computer will
> say to me, "look, I can't find what width is since it's not where it should
> be." In other words, the "width" that the computer is expecting is not the
> same "width" that was located in the while loop.
>
> So, a slightly better variation/suggestion might be to relocate the brackets
> like this:
>
> Do while (regNumber <= 0)
>
> {
>
> // your code.
>
> }
>
> // your output code.
>
> Also, it appears that you might be typing something odd. If you want total
> price, what should the assignment look like? If I were you, I might type:
>
> totalPrice = regNumber+totalPrice;
>
> Another hint: What if the user needs to type in a new value after the
> program accepts the first reg number? You might want to check out:
>
> // A slight variation from Joseph.
>
> Do while (regNumber <= 0)
>
> {
>
> cout <<" Enter your registrationnnnnnnnnnnnnnnnnnn. Remember that typing a
> negative number will print out the results." << endll;
>
> cin >> regNumber;
>
> totalPrice= regNumber+totalPrice;
>
> }
>
> From the code you've provided, it appears that it's a simple logic error.
> Once a while loop is there, you don't have to put a conditional statement
> within it to get out of the loop unless the thing that the user types is the
> very condition that'll end the loop. For instance, on the while loop here,
> if the user keeps typing positive numbers, the program will continue to add
> up registration numbers. However, at any given point, if the user types
> negative number, the while loop will terminate.
>
> I'm sure other folks would be happy to go over while loop in more detail and
> perhaps provide more useful examples for you. Again, good job so far in your
> progress. Keep up the good work.
>
> Cheers,
>
> Joseph
>
>
>
> From: programmingblind-bounce@xxxxxxxxxxxxx
> [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Jes
> Sent: Wednesday, July 14, 2010 3:23 PM
> To: programmingblind@xxxxxxxxxxxxx
> Subject: Some help needed on line 30 of this code?
>
>
>
> Hi all,
>
> Check this code out for me and tell me what I can do to fix this syntax
> error. VS says there's something wrong with my identifier in line 30, but I
> can't figure it out.
>
> Thanks.
>
> Jes
>
> //Ch7AppE04.cpp
>
> //Displays registration information
>
> //Created/revised by <your name> on <current date>
>
>
>
> #include <iostream>
>
> #include <iomanip>
>
>
>
> using std::cout;
>
> using std::cin;
>
> using std::endl;
>
> using std::fixed;
>
> using std::setprecision;
>
>
>
> int main()
>
> {
>
>
>
>                                 //declare variables
>
> int regNumber = 0;   //counter
>
> int total_Price = 0;
>
>                 //accumulator //Adds number of registrations until user
> enters negative number to stop acumulating.
>
> //The negative number is known as the senteniel value, which is the value
> which stops the loop.
>
> cout << "Enter number of registrations.\n Press enter after each number.\n
> Enter a negative number to get the total price: " << endl;
>
> cin >> regNumber;
>
> do
>
> {while (regNumber >= 0)
>
> cout << "Enter your next entry. Remember, enter negative number to get total
> price: " <<endl;
>
> if (regNumber < 0)
>
> regNumber = regNumber + total_Price;
>
> } //end while
>
> cout << total_Price;
>
> return 0;
>
> } //end of main function
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com
> Version: 8.5.441 / Virus Database: 271.1.1/3003 - Release Date: 07/13/10
> 18:36:00
>
>
__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: