Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,474 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,867 people online right now. Registration is fast and FREE... Join Now!




Idiot needs help stopping a while loop

2 Pages V  1 2 >  
Reply to this topicStart new topic

Idiot needs help stopping a while loop, see title

Re@p3rM4n
5 Feb, 2008 - 04:33 PM
Post #1

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 42


My Contributions
Hey. So, i wrote this:

CODE
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    char numc[81];
    int num, oldnum = 0, i=0, booleani = 0;
    //bool end = false;
    printf("Please enter some integers followed by a zero.");
    while(booleani = 1/*end=false*/){
                i++;
                gets(numc);
                 int num = atoi(numc);
                 if (num = 0){
                        booleani = 0;
                        break;}
                        
                 if(i != 1){
                        oldnum = num;}
                else{
                     if(oldnum <= num){
                                 oldnum = num;}
                     else{
                                 num = oldnum;}
                    }}
printf("\n%d", num);
    
system("PAUSE");    
return 0;
}

, right, but when i run it, the loop never stops ( never outputs, never PAUSEs). i know it's crappy, but i spent a while tweaking it to see if anything i did would make it work. so ignore the redundancies (unless they're the problem), and help me if you will.
Thanks,
-R
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Idiot Needs Help Stopping A While Loop
5 Feb, 2008 - 04:36 PM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
in the conditions of the while loop and if statement instaed of = which is the assignment operator use == the equality operator. See what happens.
User is offlineProfile CardPM
+Quote Post

Re@p3rM4n
RE: Idiot Needs Help Stopping A While Loop
5 Feb, 2008 - 04:48 PM
Post #3

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 42


My Contributions
QUOTE(PennyBoki @ 5 Feb, 2008 - 05:36 PM) *

in the conditions of the while loop and if statement instaed of = which is the assignment operator use == the equality operator. See what happens.


i tried that, and some freaky shit happened. here is a screenshot.
-R


Attached File(s)
Attached File  meh.bmp ( 853.93k ) Number of downloads: 28
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Idiot Needs Help Stopping A While Loop
5 Feb, 2008 - 04:51 PM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
I cannot open the screen shot, anyways what happen, did you check your logic is everything cool with the values of the variables? Please explain.
User is offlineProfile CardPM
+Quote Post

Tom9729
RE: Idiot Needs Help Stopping A While Loop
5 Feb, 2008 - 04:55 PM
Post #5

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,582



Thanked: 12 times
Dream Kudos: 325
My Contributions
Opens fine for me. I wouldn't call that "freaky" though.
User is online!Profile CardPM
+Quote Post

Re@p3rM4n
RE: Idiot Needs Help Stopping A While Loop
5 Feb, 2008 - 05:00 PM
Post #6

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 42


My Contributions
QUOTE(PennyBoki @ 5 Feb, 2008 - 05:51 PM) *

I cannot open the screen shot, anyways what happen, did you check your logic is everything cool with the values of the variables? Please explain.


bassically, the comp entered 3997696 for me and went to "PAUSE". I think my logix cool. by all reason, break; or changing booleani's value should stop the loop, but it doesn't. maybe it never gets there... hold on.
CODE

...
    if (numc == "0"){
                        booleani = 0;
                        break;}
...

nope, still doesn't work: loop again doesn't end.
-R
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Idiot Needs Help Stopping A While Loop
5 Feb, 2008 - 05:05 PM
Post #7

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
QUOTE(Tom9729 @ 5 Feb, 2008 - 05:55 PM) *

Opens fine for me. I wouldn't call that "freaky" though.

If I cannot open it doesn't imply that most of the members can't open the file wink2.gif

QUOTE(Re@p3rM4n @ 5 Feb, 2008 - 06:00 PM) *

QUOTE(PennyBoki @ 5 Feb, 2008 - 05:51 PM) *

I cannot open the screen shot, anyways what happen, did you check your logic is everything cool with the values of the variables? Please explain.


bassically, the comp entered 3997696 for me and went to "PAUSE". I think my logix cool. by all reason, break; or changing booleani's value should stop the loop, but it doesn't. maybe it never gets there... hold on.
CODE

...
    if (numc == "0"){
                        booleani = 0;
                        break;}
...

nope, still doesn't work: loop again doesn't end.
-R

The logic is bad because booleani is set to 0 and in the while condition is while booleani is 1? That is bad logic so the program just skips the while and goes directly to printing the value of num, which is some "garbage".


User is offlineProfile CardPM
+Quote Post

Re@p3rM4n
RE: Idiot Needs Help Stopping A While Loop
5 Feb, 2008 - 05:08 PM
Post #8

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 42


My Contributions
QUOTE(PennyBoki @ 5 Feb, 2008 - 06:05 PM) *

QUOTE(Tom9729 @ 5 Feb, 2008 - 05:55 PM) *

Opens fine for me. I wouldn't call that "freaky" though.

If I cannot open it doesn't imply that most of the members can't open the file wink2.gif

QUOTE(Re@p3rM4n @ 5 Feb, 2008 - 06:00 PM) *

QUOTE(PennyBoki @ 5 Feb, 2008 - 05:51 PM) *

I cannot open the screen shot, anyways what happen, did you check your logic is everything cool with the values of the variables? Please explain.


bassically, the comp entered 3997696 for me and went to "PAUSE". I think my logix cool. by all reason, break; or changing booleani's value should stop the loop, but it doesn't. maybe it never gets there... hold on.
CODE

...
    if (numc == "0"){
                        booleani = 0;
                        break;}
...

nope, still doesn't work: loop again doesn't end.
-R

The logic is bad because booleani is set to 0 and in the while condition is while booleani is 1? That is bad logic so the program just skips the while and goes directly to printing the value of num, which is some "garbage".


i just changed it.
ya'know, i was going to say 'i knew it was sumfin stupid like that' except the loop is still not ending.
-R

This post has been edited by Re@p3rM4n: 5 Feb, 2008 - 05:10 PM
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Idiot Needs Help Stopping A While Loop
5 Feb, 2008 - 05:17 PM
Post #9

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
can you post the updated code?

User is offlineProfile CardPM
+Quote Post

Re@p3rM4n
RE: Idiot Needs Help Stopping A While Loop
5 Feb, 2008 - 05:21 PM
Post #10

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 42


My Contributions
QUOTE(PennyBoki @ 5 Feb, 2008 - 06:17 PM) *

can you post the updated code?


CODE

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    char numc[81];
    int num, oldnum = 0, i=0, booleani = 1;
    //bool end = false;
    printf("Please enter some integers followed by a zero.");
    while(booleani = 1/*end=false*/){
                i++;
                gets(numc);
                 int num = atoi(numc);
                 if (numc == "0"){
                        booleani = 0;
                        break;}
                        
                 if(i != 1){
                        oldnum = num;}
                else{
                     if(oldnum <= num){
                                 oldnum = num;}
                     else{
                                 num = oldnum;}
                    }}
printf("\n%d", num);
    
system("PAUSE");    
return 0;
}

yeah...
-R
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Idiot Needs Help Stopping A While Loop
5 Feb, 2008 - 05:26 PM
Post #11

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
In my first reply I also mentioned to use == in the while
like while(booleani==1)

also numc is char so you'll need if (numc == '0') not if (numc == "0")
User is offlineProfile CardPM
+Quote Post

Re@p3rM4n
RE: Idiot Needs Help Stopping A While Loop
5 Feb, 2008 - 05:30 PM
Post #12

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 42


My Contributions
QUOTE(PennyBoki @ 5 Feb, 2008 - 06:26 PM) *

In my first reply I also mentioned to use == in the while
like while(booleani==1)

also numc is char so you'll need if (numc == '0') not if (numc == "0")


cool, changed that, loop still not ending...
-R
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 1/7/09 02:42PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month