English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I'm not used to using character values in my loops. For some reason it goes a little while. It may not be infinite, I never waited long.

#include
#include

using namespace std;

char builder_name[35];
char choice;

void add_builder()
{

while(choice != 'N' || choice != 'n')
{

ofstream builders;
builders.open ("builders.txt", ios::app);

cout << " Enter the name of a builder: ";
cin.get(builder_name, 35);
cin.ignore();

builders << builder_name << endl;

builders.close();

cout << " Would you like to add another builder? (enter (Y) or (N): ";
cin >> choice;

};

}

int main ()
{

add_builder();

return 0;

}

2007-03-14 17:40:54 · 4 answers · asked by blueice_1820 2 in Computers & Internet Programming & Design

I changed it to:
while(choice != 'N' && choice != 'n' )

I also removed the uneccessary semicolon.

It still went infinite. I then changed it to while(choice != 'N') to eliminate the two options altogether. It still went infinite. Any other ideas. It seems like a simple problem. I'm just thinking too complicated probably.

2007-03-15 06:39:47 · update #1

4 answers

while(choice != 'N' || choice !='n')
In this line and (&&) should be used instead of or(||).
Because if or( || ) is used, atleast one condition will be true for any input (n or N). so loop will go infinite. Hence we should use and(&&) to make the loop to terminate for either of the input n or N.
Semicolon should not be used at end of while loop.

2007-03-14 18:33:47 · answer #1 · answered by Hema 1 · 0 0

The together as loop in no way breaks. and located the errors 1st U suggested together as(selection != 'N' && selection != 'n') that's impossible instead attempt this together as(selection != 'N' || selection != 'n') 2nd cout << " would you pick to function yet another builder? (enter (Y) or (N): "; cin >> selection; write this if(selection == 'N' || selection == 'n'){ wreck(); }else{ call of that function or declartion

2016-12-18 14:01:10 · answer #2 · answered by Anonymous · 0 0

Here's your problem - while(choice != 'N' || choice != 'n').
It's that ol' debbil double negative, in a different context. As long as choice isn't both N and n, it loops. If choice ==N, choice !=n is true and vice versa.

2007-03-14 17:51:52 · answer #3 · answered by injanier 7 · 1 0

Change the line:
while(choice != 'N' || choice != 'n')
Into:
while(choice != 'N' && choice != 'n')

2007-03-14 17:59:50 · answer #4 · answered by Coosa 2 · 1 0

fedest.com, questions and answers