d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help C++ > After A Dowhile Loop
Add Reply New Topic New Poll
Member
Posts: 625
Joined: Nov 18 2009
Gold: 23.95
Jan 28 2013 04:22pm
When i enter yes to do my program again with a do while loop, it just couts the text i have and doesn't even ask for the user to put his input in
but rather asks it on the next cout instead.

Thanks.

//Start of the loop
do
{


//Asks the user their business name

cout << "\nPlease enter the name of your business: "; <-- Skips this after answering yes at the end but shows it on cmd
getline(cin,business_name);

double gallon_size;

//Asks for the gallon size

cout << "\nPlease enter the required tank size in gallons: "; <--goes to this instead of waiting at previous cout statement
cin >> gallon_size;

-------------------------------------------------------------

cout << "\nWould you like to start over (yes/no): ";
cin >> answer;
} <--- closes my various if else else if statements at the end of a else statement

//Keeps going until user enters something besides yes
while(answer == "yes");

return 0;
}
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jan 28 2013 04:53pm
code tags please. what is business_name defined as.

i think youre just having newline problems thats skipping inputs. try googling how to flush the stdin buffer in c++

edit most likely a logic error but you are recreating your gallon_size variable inside your loop. this is almost allows a logic error and can screw your application up.

This post was edited by AbDuCt on Jan 28 2013 04:55pm
Member
Posts: 625
Joined: Nov 18 2009
Gold: 23.95
Jan 28 2013 06:46pm
i have business_name as a string
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jan 28 2013 07:52pm
almost seems as if you didnt bother to research what i said.
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Jan 28 2013 07:54pm
Code
std::string business_name;
std::string answer;
double gallon_size;

do
{
std::cout << "Enter the name of  your business: ";
std::cin >> business_name;

std::cout << "Enter the required tank size in gallons: ";
std::cin >> gallon_size;

std::cout << "Start over?("yes / no");
std::cin >> answer;

}while(answer.compare('"yes") == 0);
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jan 28 2013 08:40pm
Quote (SelfTaught @ Jan 28 2013 09:54pm)
Code
std::string business_name;
std::string answer;
double gallon_size;

do
{
std::cout << "Enter the name of  your business: ";
std::cin >> business_name;

std::cout << "Enter the required tank size in gallons: ";
std::cin >> gallon_size;

std::cout << "Start over?("yes / no");
std::cin >> answer;

}while(answer.compare('"yes") == 0);



thank you for using a function that actually handles newlines instead of leaving them in the buffer which has to be cleared manually.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll