Click to See Complete Forum and Search --> : loop issues
Vegas/Newbie
03-28-2003, 03:01 AM
I am working on a simple for/while loop in class which requires the loop to end after 10 entries. I am having trouble engaging the count to stop the loop. Where am I going wrong? Any suggestions? Thanks in advance...
#include <iostream>
#include <string>
#pragma hdrstop
int main()
{
string lastname;
float count, rate, num, sales, pay;
count = 1;
do
{
rate = 300;
count = 0;
cout << "Enter employee last name: ";
cin >> lastname;
cout << "Enter employee number: ";
cin >> num;
cout << "Enter employee weekly sales: ";
cin >> sales;
if (sales <= 500)
{
pay = rate + (0.1 * sales);
cout << "Employee: "<< lastname << ", Number: "<< num << ", Made $" << pay << " \n";
cout << count << endl;
}
else
{
if (sales > 500)
{
pay = ((sales - 500) * 0.15) + 350;
cout << "Employee: "<< lastname << ", Number: "<< num << ", Made $" << pay << " \n";
cout << count << endl;
}
}
}
while (count <= 10);
{count ++;}
cin.get();
cin.get();
return 0;
}
EscapeCharacter
03-28-2003, 03:36 AM
everytime your loop runs count is being set to zero, and even if you were incrementing in the loop(which you arent) it would still equal zero everytime it loops
Vegas/Newbie
03-28-2003, 02:12 PM
everytime your loop runs count is being set to zero, and even if you were incrementing in the loop(which you arent) it would still equal zero everytime it loops
My original question was where was I going wrong? I realize that the counter is not working. I am unsure of how the program has to be manipulated so that the counter does work...
Thanks
AdaHacker
03-28-2003, 06:10 PM
Um, maybe increment count instead of setting it to zero? If you set count to zero at the beginning of every iteration and never increase it, then it will obviously never be greater than 10. In fact, it will never even be anything other than zero.
Vegas/Newbie
03-28-2003, 10:57 PM
Thanks alot. Duh! Basic math rules. I feel much dumber after posting that code now. But I have another if thou art interested. hehe.
Riddle me this:
A loop that sums the odd numbers between 1 and 100. I can get them to increment in odd numbers, but cannot get the sum of them. I will probably feel stupid posting this one too, but we all started somewhere right?
#include <iostream.h>
#pragma hdrstop
int main()
{
int x, sum;
x = 1;
do
{
cout << x << endl;
x = x + 2;
sum = ?? /* Here is the problem. Do I need
another variable to make this work? */
}
while (x < 100);
cin.get();
cin.get();
return 0;
}
sharth
03-28-2003, 11:10 PM
set sum = to 0 outside the loop.
sum += x as your sum = ?
and do the sum += x before the x += 2
Vegas/Newbie
03-29-2003, 12:41 AM
I tried your suggestion sharth, and I still get incrementing values with no sum (total to be exact).
Can I make an equation to output the sum of all odd numbers between 1 and 100 with only 2 variables?
sharth
03-29-2003, 01:10 AM
#include <iostream.h>
int main() {
int x=1, sum=0;
do {
cout << x << endl;
sum += x;
x += 2; // increases x to next odd number.
} while (x <= 100);
cout << "\n\nSum: " << sum;
cout << endl;
return(0);
}
sharth
03-29-2003, 01:10 AM
what happened, was that you simply forgot to output what sum equaled at the end of the code.
Vegas/Newbie
03-29-2003, 04:06 AM
sharth, thanks for the much appreciated help.
Being new to C++, it is rather frustrating at times.
I enjoy absorbing as much as possible. It helps to know that these forums offer much insight for those like myself who aren't as efficient programmers as others.
Just one silly question though:
what, in lamens terms, does += stand for? Is it the same as saying blah = blah + yada? I see it as being plus equal to. Is that right, like <= is less than or equal to?
Thanks,
A sponge :)
sharth
03-29-2003, 01:33 PM
x += y is the same as x = x + y :)