the "while (condition) wont work.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2009
Posts: 4
Reputation: apo is an unknown quantity at this point 
Solved Threads: 0
apo apo is offline Offline
Newbie Poster

the "while (condition) wont work.

 
0
  #1
May 31st, 2009
/*hi I'm having problems with my "while ()" apperently it is not taking any action, and it keeps going endless. i need it to stop when the answer is 'n'*/
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #include<iostream.h>
  6.  
  7. //---------------------------------------------------------------------------
  8. void main()
  9. {
  10. char pais[20],r;
  11. int i=1,temp=0,ctemp=0,atemp=0,dia=0;
  12. float promt;
  13. clrscr();
  14. fflush(stdin);
  15. cout << "desea registrar paises S/N \n";
  16. cin >> r;
  17. cout << "indique dias anotados por el pais \n";
  18. cin >> dia;
  19. cout << endl;
  20.  
  21. while (r='s')
  22. {
  23. cout << "\n indique pais de procedencia \n";
  24. cin >> pais;
  25. for (i=1;i<=dia;i++)
  26. {
  27. fflush(stdin);
  28. cout << " indique la temperatura del dia: " << i << ":" ;
  29. cin >> temp;
  30.  
  31. ctemp++;
  32. atemp=atemp+temp;
  33. }//for i
  34.  
  35.  
  36. fflush(stdin);
  37. promt=(atemp/ctemp++);
  38. cout << "\n el promedio de temperatura en \t" << pais << "\t es:" ;
  39. cout << promt;
  40. cout << "\n desea ingresar otro pais? S/N \n";
  41. cin >> r;
  42.  
  43. };
  44. cout << "tenga un buen dia";
  45. cout << endl;
  46. getch();
  47. }
Last edited by Ancient Dragon; May 31st, 2009 at 12:18 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 108
Reputation: Majestics is an unknown quantity at this point 
Solved Threads: 7
Majestics Majestics is offline Offline
Junior Poster

Re: the "while (condition) wont work.

 
0
  #2
May 31st, 2009
use == instead of = in while
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 4
Reputation: apo is an unknown quantity at this point 
Solved Threads: 0
apo apo is offline Offline
Newbie Poster

Re: the "while (condition) wont work.

 
0
  #3
May 31st, 2009
thanks i just notice like a minute ago after i posted, thanks majestics
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: the "while (condition) wont work.

 
1
  #4
May 31st, 2009
Unportable code alarm!
You're including conio.h, this isn't a standard library file
iostream.h is not a C header, it's a C++ header, so you posted your code in the wrong forum
Aaargh, the terrible void main() , it's evil, replace it with int main() (read this).
This is not C code, this is C++ code (this means that you've posted in the wrong forum), consider using new style header files (if your compiler supports it), so:
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #include<iostream.h>
would become:
  1. #include<cstdio>
  2. #include<conio.h>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<iostream>


Note: This is definitely (rubbish) C++ code (mixed with C), cout is an object, and C isn't an object oriented language
Last edited by tux4life; May 31st, 2009 at 6:03 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: the "while (condition) wont work.

 
0
  #5
May 31st, 2009
It's pretty much logical your while statement isn't working correctly, if you use the '=' operator in an expression, this means that you're assigning something, it means not that you're comparing something, if you want to compare, you should use the '==' operator so while (r='s') would become while (r=='s')
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC