Infinite loop...

// prac04-6.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
string name="";
char ch='\0'; //to read the '\n' character.
double sales=0.0;
double total=0.0;


cout <<"Pls enter your name : "<<endl;
getline(cin, name);
cin.get(ch);
transform(name.begin(),name.end(),name.begin(),tolower);

while(name!="quit")
{
cout << "Please enter the sales : " <<endl;
cin >> sales;

while(sales<0)
{
cout << "Invalid input"<<endl;
cout << "Please re-enter the sales: "<<endl;
cin >> sales;
}
total+=sales;

cout <<"Pls enter your name : "<<endl;
getline(cin, name);
cin.get(ch);
transform(name.begin(),name.end(),name.begin(),tolower);

}


cout <<fixed<<setprecision(2);
cout <<"The total sales is: $"<<total<<endl;


return 0;
}

The while loop is an infinite loop which may be caused by the bold part of this program.

I tried to find the solution and I found that if I used cin>>name; to be instead of the bold part of the program, the infinited loop was solved,but I can not input a string with space.

Can anybody help with that?

Thanks a lot.
Topic archived. No new replies allowed.