943,778 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 481
  • C++ RSS
May 31st, 2009
0

the "while (condition) wont work.

Expand Post »
/*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
Similar Threads
apo
Reputation Points: 10
Solved Threads: 0
Newbie Poster
apo is offline Offline
13 posts
since May 2009
May 31st, 2009
0

Re: the "while (condition) wont work.

use == instead of = in while
Reputation Points: 188
Solved Threads: 44
Posting Pro
Majestics is offline Offline
554 posts
since Jul 2007
May 31st, 2009
0

Re: the "while (condition) wont work.

thanks i just notice like a minute ago after i posted, thanks majestics
apo
Reputation Points: 10
Solved Threads: 0
Newbie Poster
apo is offline Offline
13 posts
since May 2009
May 31st, 2009
1

Re: the "while (condition) wont work.

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:
C++ Syntax (Toggle Plain Text)
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #include<iostream.h>
would become:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 31st, 2009
0

Re: the "while (condition) wont work.

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')
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help on fatal error :S!!
Next Thread in C++ Forum Timeline: Ussue with getline





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC