Alrighty, just wondering if anyone can spare a few minutes to help me out a little. Trying to do the finishing touches on a project I've got for tomorrow. My problem is this -

I'm trying to make the user interface a little more appealing instead of it just asking for details. The way I'm going about this is by letting the user choose what they would like the program to do via a number system. I've got a small piece of code I used before to get the user to choose between Yes and No for a random Number generator, and I've tried to alter that code to my needs. I've got two questions here, the first concerning the first piece of code and the second is to why I'm getting these errors lol.

First off heres the start of the Random Number Generator -

for (;;) 
     {
     cout << "Would you like to input a randomly generated input range? Please enter Y or N" << endl;
     cin >> choice;
	
     if (choice.at(0) == 'y' || choice.at(0) =='Y')
          { ect ect ect }
         break; 
}

My first question is regarding the ;; inside the for statement. I can't for the life of me remember why its there, and can't really find anywhere which explains it from searching notes and textbooks.

Secondly, I altered the above code, albeit wrongly haha, and this is what I've currently got -

cout << "Welcome to An Object Oriented Wind Farm Simulator!" << endl;
cout << "What would you like to do out of the following?" << endl;
cout << "1. Run the Simulation only" << endl;
cout << "2. Run the Simulation and generate a report, which can be found in a specific word document?" << endl;
cout << "3. Enter the price of a kilowatt hour" << endl;
cout << "4. End the Program!" < endl;

cin >> choice;

if (choice.at(0) == '1')
	{ectectect}
break;

Im battering on with it, but can't honestly figure out what I've done wrong. Any help would be greatly appreciated!
Ryan.

for(;;) is just one way to create an infinite loop. Another way is while(true) or while(1)

After cin >> choice; you might want to add a series of if statements or a switch statement and a series of cases.

cin >> choice;
if( choice == '4')
   break;
else if( choice == '1')
   do_something();
else if( choice == '2')
   do_something_else();

The break statement where you have it will cause the infinite loop to exit regardless of the value of choice.

Edited 6 Years Ago by Ancient Dragon: n/a

Also for your program it would make sense to change the data type of choice to int as opposed to what you have now

Ah right, I get it. I'll just swap them all around and try it out.

Thats it working! Thats twice you've helped me out of a pickle haha. What I've got inside the loop isn't working just now, but I haven't moved the break from where you said it would cause problems. I'll fiddle about with it a bit more and see whats going on.
Your a legend mate :D!

Sorry about the triple post but I can't figure this out now. The start of the program itself is working, but its going into an infinite loop for the list of couts i have above. I've tried moving the break to where you said it was suitable Ancient, but that doesnt work either. Anyone got any ideas?

Oh and heres the code I'm using which is giving the infinite loop -

for(;;)
{

cout << "Welcome to An Object Oriented Wind Farm Simulator!" << endl;
cout << "What would you like to do out of the following?" << endl;
cout << "1. Run the Simulation only" << endl;
cout << "2. Run the Simulation and generate a report, which can be found in a specific word document?" << endl;
cout << "3. Enter the price of a kilowatt hour" << endl;
cout << "4. End the Program!" << endl;

cin >> choice;

if (choice == '1')
	{Quite alot of code, all works independatly of this though}
break;}
else if ... ect ect

Any thoughts anyone? I'm having no luck

All the choice should be inside the for loop. From the code that you have posted, your for loop ends after the first if statement.

Edited 6 Years Ago by abhimanipal: n/a

Ah yeah I see. I've changed it about to go with what you said and its still caught in an inifinite loop. I'm fiddling about with it but I'm having no luck atall haha

Can you post little more of your code ... These are the things that I want to see ..... The way you have written the for loop and the way you have written the conditions in the if else statements ....

PS are you sure there is no infinite loops any where else ?

This question has already been answered. Start a new discussion instead.