| | |
While and Do While Help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 43
Reputation:
Solved Threads: 0
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;
}
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;
}
•
•
•
•
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
•
•
Join Date: Oct 2004
Posts: 43
Reputation:
Solved Threads: 0
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;
}
#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: 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: 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: Will never end, unless broken by other means, such as break and return.
In this case, we could do somewhat the following: 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
The while loop operates as follows:
while (expression)
statementBy 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);
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)
statementIn 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 }
Hope this helps,
- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.
IRC
Channel: irc.daniweb.com
Room: #c, #shell
IRC
Channel: irc.daniweb.com
Room: #c, #shell
Hopeolicious,
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)
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,
C++ Syntax (Toggle Plain Text)
while(true) { getdata(); processdata(); putdata() }
![]() |
Other Threads in the C++ Forum
- Previous Thread: Help with Calculation
- Next Thread: help with this program
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





