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;
}

Recommended Answers

All 4 Replies

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

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;


}

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

Hopeolicious,

while(x = 0 && x < 3)
{
   getdata();
   processdata();
   putdata();
}

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,

while(true) 
{
  getdata();
  processdata();
  putdata()
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.