while(x){
int registered;
cout<<"Enter total number of registrants: "<<endl;
cin>>registered;
if(registered<0&®istered <=4){
cout<<"for "<<registered<< " the total price is: "<<registered*100<<endl;
system("pause");
}
else if(registered == 0){
cout<<"Please enter a number greater than 0"<<endl;
}
else if(registered>=5&®istered<=10){
cout<<"for "<<registered<< " the total price is: "<<registered*80<<endl;
system("pause");
}
else if(registered>10){
cout<<"for "<<registered<< " the total price is: "<<registered*60<<endl;
system("pause");
}
else if(registered == NULL){
return x = false;
}
is their anyway i can make this program break the while loop when nothing is entered. The NULL if is my inefficient attempt to make this work. Disregard whats in the other if statements its for schoolwork
std::cin is not going to help as it only reacts when you have data in your input. My bet would be on cin.get(), it detects an enter even if there's no data. For example:
1 2 3
char line[64];
cin.get(line,64);
cout << strlen(line) << endl; // If the input was empty length will be 0
int i;
char line[64];
while (1) { // 1 means it goes around for ever until you decide to break it
cout << "Enter total number of registrants: " << endl;
cin.get(line,64);
i = atoi(line); // Converts the string into an integer
if (strlen(line) == 0) break; // Breaks the loop
elseif (i < 0 && i <= 4) {
cout << "for " << i << " the total price is: " << i*100 << endl;
cin.get(); // You shouldn't use system("pause") it's OS dependable and doesn't work with this code due to it's nature
}
elseif (i == 0) {
cout << "Please enter a number greater than 0" <<endl;
}
elseif (i >= 5 && i <= 10) {
cout <<"for "<< i << " the total price is: " << i*80 << endl;
cin.get();
}
elseif (i>10) {
cout <<"for " << i << " the total price is: "<< i*60 << endl;
cin.get();
}
}