944,155 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2457
  • C++ RSS
Oct 14th, 2004
0

While and Do While Help

Expand Post »
I have to use a while loop to display everything 3 times and then i have to use a do while loop to do the same thing
when i added my while and do while loop it keeps on repeating like a millions times and i cant stop it can someone please help me


// Description: Display person name, address, calculated and
// actual cost of car and color of car

#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <iomanip.h>


int x;
char st_name[25];
char st_address[75];
char st_color[25];
float f_ccost;
float f_acost;


void getdata();
void processdata();
void putdata();


int main()
{
for(x=1;x<=3;x++)
{
getdata();
processdata();
putdata();
}
}
void getdata()
{
cout << "\n\nPlease enter your name: ";
cin.getline(st_name,25);

cout << "\n\nPlease enter your address: ";
cin.getline(st_address,75);

cout << "\n\nPlease enter the amount you paid for your car: ";
cin >> f_acost;

cin.get();

cout << "\n\nPlease enter your car color: ";
cin.getline(st_color,25);
}
void processdata()
{
cout << "\n\nCalculated amount of your car is: ";

if(f_acost < 1200)
{
if(strcmp(st_color,"blue")==0)
{
f_ccost = f_acost + 10.0/100;
cout << f_ccost;
}
else
{
cout << f_acost;
}
}
else if(f_acost > 1000.00 && f_acost < 1500.00)
{
if(strcmp(st_color,"black")==0)
{
f_ccost = f_acost + 2.0/100;
cout << f_ccost;
}
else
{
cout << f_acost;
}
}
else if(f_acost <= 2000.00)
{
if(strcmp(st_color,"white")==0)
{
f_ccost = f_acost - 500.00;
cout << f_ccost;
}
else
{
cout << f_acost;
}
}
else
{
cout << f_acost;
}

}
void putdata()
{
cout << "\n\nPerson Name: " << st_name;
cout << "\n\nAddress: " << st_address;

cout << setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
<< setprecision(2);
cout << "\n\nActual Cost of Car: $ " << f_acost;
cout << "\n\nCalculated Cost of Car: $ " << f_ccost;
cout << "\n\nColor of Car: " << st_color;
}
Reputation Points: 11
Solved Threads: 0
Light Poster
hopeolicious is offline Offline
43 posts
since Oct 2004
Oct 14th, 2004
0

Re: While and Do While Help

Quote originally posted by hopeolicious ...
I have to use a while loop to display everything 3 times and then i have to use a do while loop to do the same thing
when i added my while and do while loop it keeps on repeating like a millions times and i cant stop it can someone please help me
Can you possibly provide the code for the while(){} and do {} while() code blocks? You're looping infinitely because you're not meeting the condition that breaks you out of the loop, most likely
Reputation Points: 44
Solved Threads: 1
Junior Poster
subtronic is offline Offline
117 posts
since Aug 2003
Oct 14th, 2004
0

Re: While and Do While Help

This is my new code to my program


#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <iomanip.h>

int x;
char st_name[25];
char st_address[75];
char st_color[25];
float f_ccost;
float f_acost;


void getdata();
void processdata();
void putdata();


int main()
{
while(x = 0 && x < 3)
{
getdata();
processdata();
putdata();
}
}
void getdata()
{
cout << "\n\nPlease enter your name: ";
cin.getline(st_name,25);

cout << "\n\nPlease enter your address: ";
cin.getline(st_address,75);

cout << "\n\nPlease enter the amount you paid for your car: ";
cin >> f_acost;

cin.get();

cout << "\n\nPlease enter your car color: ";
cin.getline(st_color,25);
}
void processdata()
{
cout << "\n\nCalculated amount of your car is: ";

if(f_acost < 1200)
{
if(strcmp(st_color,"blue")==0)
{
f_ccost = f_acost + f_acost*10/100;
cout << f_ccost;
}
else
{
cout << f_acost;
}
}
else if(f_acost > 1000 && f_acost < 1500)
{
if(strcmp(st_color,"black")==0)
{
f_ccost = f_acost + f_acost*20/100;
cout << f_ccost;
}
else
{
f_ccost = f_acost;
cout << f_ccost;
}
}
else if(f_acost <= 2000)
{
if(strcmp(st_color,"white")==0)
{
f_ccost = f_acost - 500;
cout << f_ccost;
}
else
{
f_ccost = f_acost;
cout << f_ccost;
}
}
else
{
f_ccost = f_acost;
cout << f_ccost;
}

}
void putdata()
{
cout << "\nPerson Name: " << st_name;
cout << "\nAddress: " << st_address;

cout << setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
<< setprecision(2);
cout << "\nActual Cost of Car: $ " << f_acost;
cout << "\nCalculated Cost of Car: $ " << f_ccost;
cout << "\nColor of Car: " << st_color;


}
Reputation Points: 11
Solved Threads: 0
Light Poster
hopeolicious is offline Offline
43 posts
since Oct 2004
Oct 14th, 2004
0

Re: While and Do While Help

Greetings,

The while loop operates as follows:
while (expression)
	statement
The condition in parenthesis is tested. If it is true, the body of the loop is executed. Then the condition is re-tested, and if true, the body is executed again. When the test becomes false the loop ends, and the execution continues at the statment that follows the loop.

By contrast the do-while loop tests at the bottom after making each pass through the loop body. The body is always executed at least once. Here are the following syntax:
do {
	statement
while (expression);
The statment is executed, then expression is evaluated. If it is true, statment is evaluaged again, and so on. When the expression becomes false, the loop terminates.

Using a while loop in our case would not be difficult. It seems like you are wanting to do a while loop inside a do-while loop. The infinite loop for while would contain 1. 1 usually means true, and 0 determines false. Calling something like:
while (1)
	statement
Will never end, unless broken by other means, such as break and return.

In this case, we could do somewhat the following:
int i=0;

// Infinite loop
while (1) {
	// Loop three times [i < 3]
	do {
		// Do stuff
		i++;	// Be sure to increment, or loop will never terminate.
	} while (i < 3);

	break;	// Break infinite loop
}
The reason this works is because we infinitely call our do-while loop, though it only goes by once. Our while(1) calls on break once our do-while loop is finished. i increments 3 times before the do-while is over, hence (i < 3), and once that is over it continues to resume after our loop statement; which is break.


Hope this helps,
- Stack Overflow
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004
Oct 14th, 2004
0

Re: While and Do While Help

Hopeolicious,

C++ Syntax (Toggle Plain Text)
  1. while(x = 0 && x < 3)
  2. {
  3. getdata();
  4. processdata();
  5. putdata();
  6. }

you are infinitely looping because first you are setting "x equal to 0" which is a TRUE statement and then you are ANDing it to the condition "x less then 3" which is also a TRUE statement because you've set "x equal to 0." This has put you into the infinite loop because the condition for looping is always TRUE

x = 0 && x < 3 will resolve to TRUE in boolean algebra

What you conceptually have is,

C++ Syntax (Toggle Plain Text)
  1. while(true)
  2. {
  3. getdata();
  4. processdata();
  5. putdata()
  6. }
Reputation Points: 44
Solved Threads: 1
Junior Poster
subtronic is offline Offline
117 posts
since Aug 2003

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with Calculation
Next Thread in C++ Forum Timeline: help with this program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC