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

This program keeps going into an infinite loop for some reason. When I delete everything from ofstream builders; to builders.close(); cin.ignore(); it doesn't go into the infinite loop. So the eroor is somewhere in there.

#include
#include

using namespace std;

char builder_name[35];
char choice;

void add_builder()
{

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

// error is somewhere between here

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();
cin.ignore();

// and here

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

}

}

int main ()
{

add_builder();

return 0;

}

2007-03-15 12:01:32 · 4 answers · asked by blueice_1820 2 in Computers & Internet Programming & Design

4 answers

Move the 2nd cin.ignore() to after cin >> choice;

In response to the other answerer, your logic in the while loop is fine.

(choice != 'N' && choice != 'n')

If choice is Q, y or anything then it is both != 'N' and !='n' (true && true : true)

If choice is N then the conditions is true && false which is false, and the condition fails, ending the loop.

If the choice is n, then the condition is false && true which is false, and the condition fails, ending the loop.

I think the third answerer is confusing "&& and" with "|| or"

while(choice != 'N' || choice != 'n') (this will be an infinite loop)

2007-03-15 12:21:45 · answer #1 · answered by Vegan 7 · 1 1

it really is your difficulty - even as(selection != 'N' || selection != 'n'). it really is that ol' debbil double detrimental, in a distinct context. as long as selection isn't both N and n, it loops. If selection ==N, selection !=n is actual and vice versa.

2016-11-25 22:37:53 · answer #2 · answered by farlow 4 · 0 0

The while loop never breaks.
and found the mistake 1st U said
while(choice != 'N' && choice != 'n')
This is impossible instead try this
while(choice != 'N' || choice != 'n')
2nd
cout << " Would you like to add another builder? (enter (Y) or (N): ";
cin >> choice;
\\write this
if(choice == 'N' || choice == 'n'){
break();
}else{
\\name of that Function or declartion

2007-03-15 12:41:41 · answer #3 · answered by Best Helper 4 · 1 4

because you put "return 0 ;" that makes it return to the begining of the code. and the semi colon makes it a command. at least i think thats what it is.

2007-03-15 12:07:11 · answer #4 · answered by Nick the Nerd 3 · 0 6

fedest.com, questions and answers