question about NULL

while(x){
int registered;
cout<<"Enter total number of registrants: "<<endl;
cin>>registered;
if(registered<0&&registered <=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&&registered<=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
1
2
if(registered == NULL)
   break;

closed account (DGvMDjzh)
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  



So your code would be...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 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

  else if (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
  }

  else if (i == 0) {
   cout << "Please enter a number greater than 0" <<endl;
  }

  else if (i >= 5 && i <= 10) {
   cout <<"for "<< i << " the total price is: " << i*80 << endl;
   cin.get();
  }

  else if (i>10) {
   cout <<"for " << i << " the total price is: "<< i*60 << endl;
   cin.get();
  }
}
Last edited on
Topic archived. No new replies allowed.