GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Oct-2009, 18:54
anonymous91 anonymous91 is offline
New Member
 
Join Date: Oct 2009
Posts: 7
anonymous91 is on a distinguished road

Loop just doesn't want to stop looping when I tell it to...


Obviously the problem is me and not the loop.

But here's a watered down version of my code with the cout strings changed so there's no confusion.


CPP / C++ / C Code:
int main()
{
    int a;
    a=5;
    string x;
    
    for (;a=5;)
    {
          cout << "A yes or no answer is needed. Y/N:";
          cin >> x;
          cout << a;
          if (x=="y") { cout << "Your answer was Yes."; a=6;}
          else if (x=="n") { cout << "Your answer was No."; a=6;}
          else if (a=5) { cout << "Your answer was invalid.\n"; }
    }
    return 0;
} 

also.....the second "else if" i tried "else" but then i get the error - " expected `;' before '{' token "

apparently the "int a" isn't changing its value to 6 when the if statement is being used.

say i enter "y" or "n", it will give me the respective cout, but then it will say "your answer is invalid" and keep looping.
  #2  
Old 27-Oct-2009, 18:56
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Loop just doesn't want to stop looping when I tell it to...


Hint: In C/C++, you test for equality with the "==" operator; "=" represents assignment.

Best regards,
Lucian

Edit: If you copied some of the code from somewhere else, I recommend reading up on the structure of the for loop.
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 27-Oct-2009, 18:59
anonymous91 anonymous91 is offline
New Member
 
Join Date: Oct 2009
Posts: 7
anonymous91 is on a distinguished road

Re: Loop just doesn't want to stop looping when I tell it to...


one more thing.

the "cout << a;" that i have there doesn't "cout" when i run the program.

and that's the only reason i put it there, to see if it worked or not.
which is what helped me determine it must be something with the value of "int a".
  #4  
Old 27-Oct-2009, 19:02
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Loop just doesn't want to stop looping when I tell it to...


Did you input a value when "cin" asked for one, so the program flow could continue? If not, read up on what "cin" does.

Btw, I am going to sleep
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #5  
Old 27-Oct-2009, 19:07
anonymous91 anonymous91 is offline
New Member
 
Join Date: Oct 2009
Posts: 7
anonymous91 is on a distinguished road

Re: Loop just doesn't want to stop looping when I tell it to...


Quote:
Originally Posted by LuciWiz
Hint: In C/C++, you test for equality with the "==" operator; "=" represents assignment.

Best regards,
Lucian

Edit: If you copied some of the code from somewhere else, I recommend reading up on the structure of the for loop.

okay so i edited this part.

CPP / C++ / C Code:
else if (a==5) { cout << "Your answer was invalid.\n"; }

and it still loops.
  #6  
Old 27-Oct-2009, 19:18
anonymous91 anonymous91 is offline
New Member
 
Join Date: Oct 2009
Posts: 7
anonymous91 is on a distinguished road

Re: Loop just doesn't want to stop looping when I tell it to...


Quote:
Originally Posted by LuciWiz
Did you input a value when "cin" asked for one, so the program flow could continue? If not, read up on what "cin" does.

the line:
CPP / C++ / C Code:
cin >> x;
asks for an input and assigns the input that value.

the sentence says "A yes or no answer is needed. Y/N:"
and when i enter y or n, 'x' gets assigned "y" or "n".

which is then what i have the if and else statements for.
so depending on what value is given to 'x', the program will output a different sentence...and then re-assign 'a' to 6, so the loop stops looping.

it's only supposed to loop if 'x' was assigned something other then "y" or "n".

but it's looping regardless.

and not cout'ing 'a' when i ask it to.

thanks for the help, but im pretty sure the problem isn't the "cin >> x;" line.
  #7  
Old 27-Oct-2009, 21:16
TurboPT's Avatar
TurboPT TurboPT is online now
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,140
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Loop just doesn't want to stop looping when I tell it to...


Although you changed a=5 to a==5 in the else if condition, the loop's condition has the same problem.
(unless you've fixed it since then, it would probably be a good idea to post your latest working code in your next post)
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #8  
Old 27-Oct-2009, 22:29
anonymous91 anonymous91 is offline
New Member
 
Join Date: Oct 2009
Posts: 7
anonymous91 is on a distinguished road

Re: Loop just doesn't want to stop looping when I tell it to...


this is what i have so far:

CPP / C++ / C Code:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    int a;
    a=5;
    string x;
    
    for (;a=5;)
    {
          cout << "A yes or no answer is needed. Y/N:";
          cin >> x;
          cout << a;
          if (x=="y") { cout << "Your answer was Yes."; a=6;}
          else if (x=="n") { cout << "Your answer was No."; a=6;}
          
          if (a==5) { cout << "Your answer was invalid.\n"; }
    }
    return 0;
} 

  #9  
Old 27-Oct-2009, 22:53
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Loop just doesn't want to stop looping when I tell it to...


Your problem is caused by a mistake in the condition test in this statement:
CPP / C++ / C Code:
for (;a=5;)
Read up on condition testing and loops here: cplusplus.com/doc/tutorial/control.html
  #10  
Old 28-Oct-2009, 00:51
jnCPP jnCPP is offline
New Member
 
Join Date: Oct 2009
Posts: 9
jnCPP will become famous soon enough

Re: Loop just doesn't want to stop looping when I tell it to...


I'm wondering why you're using a string for one character?
Also, you might run into errors because your (posted) code doesn't prepare for the possibility of a 'Y' or 'N'.
I suggest using toupper (if you're allowed to)
or alternatively using the || (or)

CPP / C++ / C Code:
if (x == 'Y' || x == 'y')
{code}
else if (x == 'N' || x == 'n')
{code}
else 
{code}

also, check as to why you want your loop to break after an invalid statement and not after 'no' or 'yes'.. unless you wanted the loop to stop after something other than no or yes was enterd.. but if that's the case you should state it.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Classic recursion game of 8 Queens errors BKizzle77 Java Forum 11 11-Aug-2008 01:35
breaking an infinite loop through stdin pisuke C Programming Language 9 03-Jul-2007 21:19
Text-Based Roulette Game mfm1983 C++ Forum 5 29-Nov-2006 13:20
coding a sentinel controlled loop tommy69 C++ Forum 2 10-Mar-2004 15:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:05.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.